问题描述
我正在使用data.frame:
I'm using a data.frame:
data.frame("A"=c(NA,5,NA,NA,NA),
"B"=c(1,2,3,4,NA),
"C"=c(NA,NA,NA,2,3),
"D"=c(NA,NA,NA,7,NA))
此以以下形式提供data.frame:
This delivers a data.frame in this form:
A B C D
1 NA 1 NA NA
2 5 2 NA NA
3 NA 3 NA NA
4 NA 4 2 7
5 NA NA 3 NA
我的目的是检查data.frame的每一行,如果值大于特定值(假设为2)
My aim is to check each row of the data.frame, if there is a value greater than a specific one (let's assume 2) and to get the name of the columns where this is the case.
所需的输出(值大于2)应为:
The desired output (value greater 2) should be:
for row 1 of the data.frame
x[1,]: c()
for row 2
x[2,]: c("A")
for row3
x[3,]: c("B")
for row4
x[4,]: c("B","D")
and for row5 of the data.frame
x[5,]: c("C")
感谢您的帮助!
推荐答案
您可以使用 which
:
lapply(apply(dat, 1, function(x)which(x>2)), names)
以 dat
作为数据框。
[[1]]
character(0)
[[2]]
[1] "A"
[[3]]
[1] "B"
[[4]]
[1] "B" "D"
[[5]]
[1] "C"
EDIT
flodel建议的简称:
EDITShorter version suggested by flodel:
lapply(apply(dat > 2, 1, which), names)
编辑:(来自Arun)
首先,不需要 lapply
和应用
。您可以使用 apply
来获得相同的效果:
First, there's no need for lapply
and apply
. You can get the same just with apply
:
apply(dat > 2, 1, function(x) names(which(x)))
但是,使用 data.frame 上使用code> apply 会将其强制转换为矩阵,如果data.frame很大,可能不明智。
But, using apply
on a data.frame
will coerce it into a matrix, which may not be wise if the data.frame is huge.
这篇关于选择行中包含特定值的列的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!