本文介绍了如何在 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
我想要做的是去掉值 < 的行.8 列中的任何一个.我试过了,但行名(例如 ATP1B1、CYCS 等)不见了:
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
如何将其保留在 dplyr 链中?
How can I preserve that in dplyr chain?
推荐答案
您可以将行名转换为列并在过滤后还原:
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
这篇关于如何在 dplyr 链中过滤时保留基本数据框行名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!