简体   繁体   中英

can i apply filter in summarise_all?

I am doing sum up for my data frame. My data frame looks like this:

A   B   C   D   E   F

1   1   1   1   4   2
2   2   5   6   9   9
3   1   7   1   2   4
4   9   6   5   6   6
5   4   7   9   1   5
6   9   9   7   5   7
7   6   1   6   7   8
8   8   3   4   3   6
9   9   4   6   1   1

I would love to sum up for each column, based on Link type. I can make it work with code:

df>-datafram%>%
summaries_all(sum,na.rm=TRUE)

And the output is :

A   B   C   D   E   F

45  49  43  45  38  48

but when I apply with filter:

summaries_all(sum(B<9),na.rm=TRUE),

the R said:

Error in is_fun_list(.funs) : object 'B' not found

I would love to apply filter to my summaries_all so that only sum all the numbers that b<9

Can you guys give me some hints please

Many thanks

We can create a logical expression with 'B' to subset the values of each column and then do the sum

library(dplyr)
datafram %>% 
   summarise_at(vars(-B), ~ sum(.[B < 9], na.rm = TRUE))
#   A  C  D  E  F
#1 26 24 27 26 34

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