简体   繁体   中英

How do I `group_by` and `summarise` while keeping all numeric columns without knowing those columns' names?

In the Indometh dataframe, I want to get the sum of time by Subject , but also keep all other columns.

My code thus far:

group_by(.data = Indometh, Subject) %>% summarise(TimeSum=sum(time))

This gives me a dataframe with only the columns Subject and TimeSum . How do I include all other columns in this dataframe (or any others) without having to know the names of them?

Use summarize_if . For example,

exd <- data.frame(g = rep(c('a', 'b'), 5),
                  notthisone = "nope!",
                  n1 = runif(10),
                  n2 = runif(10))
summarize_if(group_by(exd, g), is.numeric, mean)

You can use mutate() function to add a new column and keep all the others like below:

library(dplyr) Indometh %>% group_by(Subject) %>% mutate(total = sum(time))

# A tibble: 66 x 4
# Groups:   Subject [6]
   Subject  time  conc total
   <ord>   <dbl> <dbl> <dbl>
 1 1        0.25  1.5   31.8
 2 1        0.5   0.94  31.8
 3 1        0.75  0.78  31.8
 4 1        1     0.48  31.8
 5 1        1.25  0.37  31.8
 6 1        2     0.19  31.8
 7 1        3     0.12  31.8
 8 1        4     0.11  31.8
 9 1        5     0.08  31.8
10 1        6     0.07  31.8

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