Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count non-NA values by group [duplicate]

Here is my example

mydf<-data.frame('col_1' = c('A','A','B','B'), 'col_2' = c(100,NA, 90,30))

I would like to group by col_1 and count non-NA elements in col_2

I would like to do it with dplyr. Here is what I tried:

mydf %>% group_by(col_1) %>% summarise_each(funs(!is.na(col_2)))
mydf %>% group_by(col_1) %>% mutate(non_na_count = length(col_2, na.rm=TRUE))
mydf %>% group_by(col_1) %>% mutate(non_na_count = count(col_2, na.rm=TRUE))

Nothing worked. Any suggestions?

like image 549
user1700890 Avatar asked Sep 01 '25 22:09

user1700890


1 Answers

You can use this

mydf %>% group_by(col_1) %>% summarise(non_na_count = sum(!is.na(col_2)))

# A tibble: 2 x 2
   col_1 non_na_count
  <fctr>        <int>
1      A            1
2      B            2
like image 139
Richard Telford Avatar answered Sep 03 '25 14:09

Richard Telford