How do I aggregate certain columns from data frame by a Unique ID?
I have a list of statcast data, per day dating back to 2016. I am attempting to aggregate this data for finding the mean for each pitching ID.
I have the following code:
aggpitch <- aggregate(pitchingstat, by=list(pitchingstat$PitcherID),
FUN=mean, na.rm = TRUE)
This function aggregates every single column. I am looking to only aggregate a certain amount of columns.
How would I include only certain columns?
r aggregate rscript
add a comment |
I have a list of statcast data, per day dating back to 2016. I am attempting to aggregate this data for finding the mean for each pitching ID.
I have the following code:
aggpitch <- aggregate(pitchingstat, by=list(pitchingstat$PitcherID),
FUN=mean, na.rm = TRUE)
This function aggregates every single column. I am looking to only aggregate a certain amount of columns.
How would I include only certain columns?
r aggregate rscript
You want to specify a variable to aggregate -aggregate(pitchingstat[c("var1","var2")], pitchingstat["PitcherID"], FUN=mean, na.rm=TRUE)
. Alternatively, use the formula interfaceaggregate(cbind(var1,var2) ~ PitcherID, data=pitchingstat, FUN=mean, na.rm=TRUE)
. See this old answer - stackoverflow.com/a/9723314/496803
– thelatemail
Nov 13 '18 at 1:30
add a comment |
I have a list of statcast data, per day dating back to 2016. I am attempting to aggregate this data for finding the mean for each pitching ID.
I have the following code:
aggpitch <- aggregate(pitchingstat, by=list(pitchingstat$PitcherID),
FUN=mean, na.rm = TRUE)
This function aggregates every single column. I am looking to only aggregate a certain amount of columns.
How would I include only certain columns?
r aggregate rscript
I have a list of statcast data, per day dating back to 2016. I am attempting to aggregate this data for finding the mean for each pitching ID.
I have the following code:
aggpitch <- aggregate(pitchingstat, by=list(pitchingstat$PitcherID),
FUN=mean, na.rm = TRUE)
This function aggregates every single column. I am looking to only aggregate a certain amount of columns.
How would I include only certain columns?
r aggregate rscript
r aggregate rscript
asked Nov 13 '18 at 1:28
gracergracer
92
92
You want to specify a variable to aggregate -aggregate(pitchingstat[c("var1","var2")], pitchingstat["PitcherID"], FUN=mean, na.rm=TRUE)
. Alternatively, use the formula interfaceaggregate(cbind(var1,var2) ~ PitcherID, data=pitchingstat, FUN=mean, na.rm=TRUE)
. See this old answer - stackoverflow.com/a/9723314/496803
– thelatemail
Nov 13 '18 at 1:30
add a comment |
You want to specify a variable to aggregate -aggregate(pitchingstat[c("var1","var2")], pitchingstat["PitcherID"], FUN=mean, na.rm=TRUE)
. Alternatively, use the formula interfaceaggregate(cbind(var1,var2) ~ PitcherID, data=pitchingstat, FUN=mean, na.rm=TRUE)
. See this old answer - stackoverflow.com/a/9723314/496803
– thelatemail
Nov 13 '18 at 1:30
You want to specify a variable to aggregate -
aggregate(pitchingstat[c("var1","var2")], pitchingstat["PitcherID"], FUN=mean, na.rm=TRUE)
. Alternatively, use the formula interface aggregate(cbind(var1,var2) ~ PitcherID, data=pitchingstat, FUN=mean, na.rm=TRUE)
. See this old answer - stackoverflow.com/a/9723314/496803– thelatemail
Nov 13 '18 at 1:30
You want to specify a variable to aggregate -
aggregate(pitchingstat[c("var1","var2")], pitchingstat["PitcherID"], FUN=mean, na.rm=TRUE)
. Alternatively, use the formula interface aggregate(cbind(var1,var2) ~ PitcherID, data=pitchingstat, FUN=mean, na.rm=TRUE)
. See this old answer - stackoverflow.com/a/9723314/496803– thelatemail
Nov 13 '18 at 1:30
add a comment |
3 Answers
3
active
oldest
votes
If you have more than one column that you'd like to summarize, you can use QAsena's approach and add summarise_at
function like so:
pitchingstat %>%
group_by(PitcherID) %>%
summarise_at(vars(col1:coln), mean, na.rm = TRUE)
Check out link below for more examples:
https://dplyr.tidyverse.org/reference/summarise_all.html
add a comment |
Replace the first argument (pitchingstat
) with the name of the column you want to aggregate (or a vector thereof)
add a comment |
How about?:
library(tidyverse)
aggpitch <- pitchingstat %>%
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(variable)) #replace 'variable' with your variable of interest here
or
library(tidyverse)
aggpitch <- pitchingstat %>%
select(var_1, var_2)
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(var_1),
pitcher_mean2 = mean(var_2))
I think this works but could use a dummy example of your data to play with.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272463%2fhow-do-i-aggregate-certain-columns-from-data-frame-by-a-unique-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have more than one column that you'd like to summarize, you can use QAsena's approach and add summarise_at
function like so:
pitchingstat %>%
group_by(PitcherID) %>%
summarise_at(vars(col1:coln), mean, na.rm = TRUE)
Check out link below for more examples:
https://dplyr.tidyverse.org/reference/summarise_all.html
add a comment |
If you have more than one column that you'd like to summarize, you can use QAsena's approach and add summarise_at
function like so:
pitchingstat %>%
group_by(PitcherID) %>%
summarise_at(vars(col1:coln), mean, na.rm = TRUE)
Check out link below for more examples:
https://dplyr.tidyverse.org/reference/summarise_all.html
add a comment |
If you have more than one column that you'd like to summarize, you can use QAsena's approach and add summarise_at
function like so:
pitchingstat %>%
group_by(PitcherID) %>%
summarise_at(vars(col1:coln), mean, na.rm = TRUE)
Check out link below for more examples:
https://dplyr.tidyverse.org/reference/summarise_all.html
If you have more than one column that you'd like to summarize, you can use QAsena's approach and add summarise_at
function like so:
pitchingstat %>%
group_by(PitcherID) %>%
summarise_at(vars(col1:coln), mean, na.rm = TRUE)
Check out link below for more examples:
https://dplyr.tidyverse.org/reference/summarise_all.html
answered Nov 13 '18 at 5:17
On_an_islandOn_an_island
758
758
add a comment |
add a comment |
Replace the first argument (pitchingstat
) with the name of the column you want to aggregate (or a vector thereof)
add a comment |
Replace the first argument (pitchingstat
) with the name of the column you want to aggregate (or a vector thereof)
add a comment |
Replace the first argument (pitchingstat
) with the name of the column you want to aggregate (or a vector thereof)
Replace the first argument (pitchingstat
) with the name of the column you want to aggregate (or a vector thereof)
answered Nov 13 '18 at 1:30
12b345b6b7812b345b6b78
782115
782115
add a comment |
add a comment |
How about?:
library(tidyverse)
aggpitch <- pitchingstat %>%
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(variable)) #replace 'variable' with your variable of interest here
or
library(tidyverse)
aggpitch <- pitchingstat %>%
select(var_1, var_2)
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(var_1),
pitcher_mean2 = mean(var_2))
I think this works but could use a dummy example of your data to play with.
add a comment |
How about?:
library(tidyverse)
aggpitch <- pitchingstat %>%
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(variable)) #replace 'variable' with your variable of interest here
or
library(tidyverse)
aggpitch <- pitchingstat %>%
select(var_1, var_2)
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(var_1),
pitcher_mean2 = mean(var_2))
I think this works but could use a dummy example of your data to play with.
add a comment |
How about?:
library(tidyverse)
aggpitch <- pitchingstat %>%
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(variable)) #replace 'variable' with your variable of interest here
or
library(tidyverse)
aggpitch <- pitchingstat %>%
select(var_1, var_2)
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(var_1),
pitcher_mean2 = mean(var_2))
I think this works but could use a dummy example of your data to play with.
How about?:
library(tidyverse)
aggpitch <- pitchingstat %>%
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(variable)) #replace 'variable' with your variable of interest here
or
library(tidyverse)
aggpitch <- pitchingstat %>%
select(var_1, var_2)
group_by(PitcherID) %>%
summarise(pitcher_mean = mean(var_1),
pitcher_mean2 = mean(var_2))
I think this works but could use a dummy example of your data to play with.
edited Nov 13 '18 at 4:49
answered Nov 13 '18 at 4:43
QAsenaQAsena
404
404
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272463%2fhow-do-i-aggregate-certain-columns-from-data-frame-by-a-unique-id%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You want to specify a variable to aggregate -
aggregate(pitchingstat[c("var1","var2")], pitchingstat["PitcherID"], FUN=mean, na.rm=TRUE)
. Alternatively, use the formula interfaceaggregate(cbind(var1,var2) ~ PitcherID, data=pitchingstat, FUN=mean, na.rm=TRUE)
. See this old answer - stackoverflow.com/a/9723314/496803– thelatemail
Nov 13 '18 at 1:30