简体   繁体   中英

Is there a way to simplify this dplyr (summarise) code - summarize multiple variables in one statement

I have the following working code (I have not included the data as I don't think it is necessary to anyone able to answer this question)...

I have a feeling this should be possible within one pipe/ one statement. Does anyone know a cleaner way?

df1 <- df %>%
  group_by(x,y,z) %>%
  summarize(mean = mean(a))

df2 <- df %>%
  group_by(x,y,z) %>%
  summarize(count = n())

df_merged <- merge(df1,df2, all=TRUE)

Just do multiple variables like count, mean or sd in summarize . You can use this:

df %>%
  group_by(x,y,z) %>%
  summarize(count = n(),
            mean = mean(a))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM