r - What is the right way to reference part of a dataframe after piping? -
this question has answer here:
what correct way this? trying colsums
of each group specific columns. .
syntax seems incorrect type of subsetting.
csv<-data.frame(id_num=c(1,1,1,2,2),c(1,2,3,4,5),c(1,2,3,3,3)) temp<-csv%>%group_by(id_num)%>%colsums(.[,2:3],na.rm=t)
this can done summarise_each
or in recent version additional functions summarise_at
, summarise_if
introduced convenient use.
csv %>% group_by(id_num) %>% summarise_each(funs(sum)) csv %>% group_by(id_num) %>% summarise_at(2:3, sum)
if using column names, wrap vars
in summarise_at
csv %>% group_by(id_num) %>% summarise_at(names(csv)[-1], sum)
note: in op's dataset, column names 2nd , 3rd columns not specified resulting in c.1..2..3..4..5.
using vars
apply function on selected column names
csv %>% group_by(id_num) %>% summarise_at(vars(c.1..2..3..4..5.), sum) # # tibble: 2 × 2 # id_num c.1..2..3..4..5. # <dbl> <dbl> #1 1 6 #2 2 9
Comments
Post a Comment