dataframe - r subset rows by criteria and by factor group -


i have data.frame lot of nas:

df <- data.frame(a = rep(letters[1:3], each = 3),                   b = c(na, na, na, 1, na, 3, na, na, 7)) df > df    b 1 na 2 na 3 na 4 b  1 5 b na 6 b  3 7 c na 8 c na 9 c  7 

i subset dataframe obtain factor group rows have no less 2 values, such this:

   b 1 b 1 2 b na 3 b 3 

i have tried function doesn't work:

subset(df, sum(!is.na(b)) < 1, = a)  > [1] b <0 rows> (or 0-length row.names) 

any suggestion? (other packages solutions welcome)

we can use data.table. convert 'data.frame' 'data.table' (setdt(df)), grouped 'a', if sum of logical vector (i.e. non-na elements - !is.na(b)) greater 1, subset data.table.

library(data.table) setdt(df)[,if(sum(!is.na(b))>1) .sd , = a] #    b #1: b  1 #2: b na #3: b  3 

or using dplyr, same logic, after grouping 'a', filter rows.

library(dplyr) df %>%      group_by(a) %>%     filter(sum(!is.na(b))>1) #          b #  <fctr> <dbl> #1      b     1 #2      b    na #3      b     3 

or in base r ave

df[with(df, ave(b, a, fun = function(x) sum(!is.na(x))>1)!=0),] 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -