Reputation: 210
How do I group columns of a dataframe for each dataframe within a list of dataframes and apply group_by() and summarise()?
Currently getting the error as show in the title. Here is what I have:
I would like to group_by()
and then summarise()
as tried here:
trial <- dataset %>% group_by(Year) %>% summarise(Mean_Max_Temp = mean(Max.Temp), Mean_Min_Temp = mean(Min.temp)
+ ,Monthly_Precip = sum(Precipitation))
Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "list"
Upvotes: 1
Views: 20152
Reputation: 3901
As the data frames appear to all have the same structure, you could bind them into one, then also group by the ID variable. I'm guessing your list names are just the longitude and latitude where your meteorological data was collected, so this shouldn't be a problem.
dataset %>%
bind_rows(.id = "location") %>%
group_by(frame_name, year) %>%
summarize(Mean_Max_Temp = mean(Max.Temp), Mean_Min_Temp = mean(Min.temp),
Monthly_Precip = sum(Precipitation))
Alternatively, you could use map()
to apply the same function to each element of your list.
dataset %>%
map(~summarise(group_by(., Year),
Mean_Max_Temp = mean(Max.Temp),
Mean_Min_Temp = mean(Min.temp),
Monthly_Precip = sum(Precipitation)))
Upvotes: 1