我希望在 R 中为我的 df 中的每一列创建一个唯一的字符向量(所以我的 df 有 20 列,所以我会得到 20 个向量)。我希望每个向量报告值大于零的行名的字符值。
> dput(ex.df)
structure(list(NASQAN2015.147.348 = c(0L, 87L, 0L, 105L, 0L),
NASQAN2015.148.348 = c(0L, 57L, 0L, 23L, 21L), NASQAN2015.161.348 = c(17L,
77L, 0L, 146L, 0L)), row.names = c("ee866b92e722c35819b112aadc4ac885",
"afc0eeec83a181be740331928d883362", "294c12d6881a2ed67aa1557cda9889ff",
"466c2c4cb06ba39543c40a74c027008a", "fd0b270adb11f2781450c2e057e50f07"
), class = "data.frame")
在 ex.df 中,我期望三个字符向量,如下所示:
NASQAN2015.147.348 ch [1:2] "afc0eeec83a181be740331928d883362" "466c2c4cb06ba39543c40a74c027008a"
NASQAN2015.148.348 ch [1:3] "afc0eeec83a181be740331928d883362" "466c2c4cb06ba39543c40a74c027008a" "fd0b270adb11f2781450c2e057e50f07"
NASQAN2015.161.348 ch [1:3] "ee866b92e722c35819b112aadc4ac885" "afc0eeec83a181be740331928d883362" "466c2c4cb06ba39543c40a74c027008a"
我知道选择一列我可以运行类似的操作:
ex.df %>% filter(NASQAN2015.147.348 > 0)
但这适用于所有列并输出 df (如果我保存对象)。
lapply(ex.df, function(z) rownames(ex.df)[z > 0])
# $NASQAN2015.147.348
# [1] "afc0eeec83a181be740331928d883362" "466c2c4cb06ba39543c40a74c027008a"
# $NASQAN2015.148.348
# [1] "afc0eeec83a181be740331928d883362" "466c2c4cb06ba39543c40a74c027008a" "fd0b270adb11f2781450c2e057e50f07"
# $NASQAN2015.161.348
# [1] "ee866b92e722c35819b112aadc4ac885" "afc0eeec83a181be740331928d883362" "466c2c4cb06ba39543c40a74c027008a"