如何在dplyr链中过滤时保留基础数据框rownames

sca*_*der 23 r dplyr

我有以下数据框:


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
Run Code Online (Sandbox Code Playgroud)

我想要做的是摆脱任何列中值<8的行.我尝试了这个,但行名称(例如ATP1B1,CYCS等)消失了:

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

我怎样才能在dplyr链中保留它?

mt1*_*022 44

您可以将rownames转换为列并在过滤后恢复:

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
Run Code Online (Sandbox Code Playgroud)

  • 这个答案很好 - 但我一直想知道为什么 dplyr 首先要剪切行名? (4认同)