Vijay Ramesh
Vijay Ramesh

Reputation: 210

Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "list"

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:

enter image description here

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

Answers (1)

shs
shs

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

Related Questions