简体   繁体   中英

How to select columns that were not summarise after group_by?

df <- data.frame(a=1:100, b=1:100, c='categorical')

df %>% summarise(new_a=sum(a), new_b=sum(b)) %>% select(new_a, new_b, c)


Error: `c` must evaluate to column positions or names, not a function

How can I make the above code do what I want, which is to keep c. The value for c will be the same for each group.

If you don't specify it by using group_by , the default behaviour is to summarize and include only what you specify within summarize , so in your code column c doesn't exist. If you specify it as a group, then that column will be included.

library(dplyr)

df <- data.frame(a=1:100, b=1:100, c='categorical')

df %>%
  group_by(c) %>% 
  summarise(new_a=sum(a), new_b=sum(b)) %>% select(new_a, new_b, c)

#> # A tibble: 1 x 3
#>   new_a new_b c          
#>   <int> <int> <fct>      
#> 1  5050  5050 categorical

Created on 2019-01-18 by the reprex package (v0.2.1)

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