Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve base data frame rownames upon filtering in dplyr chain

Tags:

r

dplyr

I have the following data frame:


df <- structure(list(BoneMarrow = c(30, 0, 0, 31138, 2703), Pulmonary = c(3380,  21223.3333333333, 0, 0, 27)), row.names = c("ATP1B1", "CYCS",  "DDX5", "GNB2L1", "PRR11"), class = "data.frame", .Names = c("BoneMarrow",  "Pulmonary"))  df  #>        BoneMarrow Pulmonary #> ATP1B1         30   3380.00 #> CYCS            0  21223.33 #> DDX5            0      0.00 #> GNB2L1      31138      0.00 #> PRR11        2703     27.00 

What I want to do is to get rid of rows with values < 8 in any of the columns. I tried this but the row names (e.g. ATP1B1, CYCS etc) are gone:

> df %>% filter(!apply(., 1, function(row) any(row <= 8 )))   BoneMarrow Pulmonary 1         30      3380 2       2703        27 

How can I preserve that in dplyr chain?

like image 489
scamander Avatar asked Aug 01 '17 01:08

scamander


2 Answers

you can convert rownames to a column and revert back after filtering:

library(dplyr) library(tibble)  # for `rownames_to_column` and `column_to_rownames`  df %>%     rownames_to_column('gene') %>%     filter_if(is.numeric, all_vars(. >= 8)) %>%     column_to_rownames('gene')  #        BoneMarrow Pulmonary # ATP1B1         30      3380 # PRR11        2703        27 
like image 158
mt1022 Avatar answered Sep 20 '22 11:09

mt1022


Here is another base R method with Reduce

df[Reduce(`&`, lapply(df, `>=`, 8)),] #       BoneMarrow Pulmonary #ATP1B1         30      3380 #PRR11        2703        27 
like image 44
akrun Avatar answered Sep 20 '22 11:09

akrun