查找R数据框每一行中包含大于指定值的值的列名。
为了找到R数据框每一行中包含大于指定值的值的列名,我们可以使用apply函数和lapply函数。
例如,如果我们有一个名为df的数据框,并且我们希望提取每一行中值大于5的列名,那么我们可以使用以下命令:
lapply(apply(df,1, function(x) which(x5)),names)
示例1
以下代码片段创建一个示例数据框:
x1<-rpois(20,5) x2<-rpois(20,2) x3<-rpois(20,1) df1<-data.frame(x1,x2,x3) df1
创建了以下数据框
x1 x2 x3 1 0 0 2 2 5 2 2 3 5 3 0 4 3 0 3 5 3 3 2 6 4 4 2 7 6 2 0 8 5 4 0 9 4 1 2 10 5 2 2 11 4 0 0 12 5 2 0 13 7 4 0 14 7 1 1 15 6 2 1 16 2 1 2 17 5 1 1 18 8 1 1 19 5 3 0 20 6 2 1
为了在上面创建的数据框的每一行中找到值大于1的列名,请将以下代码添加到上述代码片段中:
x1<-rpois(20,5) x2<-rpois(20,2) x3<-rpois(20,1) df1<-data.frame(x1,x2,x3) lapply(apply(df1,1, function(x) which(x1)),names)
输出
如果将以上所有代码片段作为一个程序执行,则会生成以下输出:
[[1]] [1] "x3" [[2]] [1] "x1" "x2" "x3" [[3]] [1] "x1" "x2" [[4]] [1] "x1" "x3" [[5]] [1] "x1" "x2" "x3" [[6]] [1] "x1" "x2" "x3" [[7]] [1] "x1" "x2" [[8]] [1] "x1" "x2" [[9]] [1] "x1" "x3" [[10]] [1] "x1" "x2" "x3" [[11]] [1] "x1" [[12]] [1] "x1" "x2" [[13]] [1] "x1" "x2" [[14]] [1] "x1" [[15]] [1] "x1" "x2" [[16]] [1] "x1" "x3" [[17]] [1] "x1" [[18]] [1] "x1" [[19]] [1] "x1" "x2" [[20]] [1] "x1" "x2"
示例2
以下代码片段创建一个示例数据框:
y1<-round(rnorm(20),1) y2<-round(rnorm(20),1) y3<-round(rnorm(20),1) y4<-round(rnorm(20),1) df2<-data.frame(y1,y2,y3,y4) df2
创建了以下数据框
y1 y2 y3 y4 1 -1.3 -0.7 -0.8 0.1 2 -0.2 -0.2 0.7 -0.7 3 0.8 -0.5 0.2 -0.1 4 -0.6 0.4 0.3 -0.8 5 -0.3 1.3 0.4 1.3 6 -0.3 1.0 -0.1 -1.2 7 -0.2 0.6 -2.1 0.5 8 1.0 1.4 0.2 -1.7 9 1.1 0.4 0.6 1.2 10 -1.0 -0.8 1.7 0.2 11 -0.6 -1.0 0.1 -0.2 12 0.6 0.3 0.0 -0.2 13 -0.3 0.6 -0.4 -1.0 14 0.9 0.0 -0.3 1.7 15 -0.2 3.3 0.7 -0.7 16 -0.6 0.1 -0.7 -0.6 17 -0.2 0.7 -0.6 1.9 18 1.0 -0.5 -0.8 0.6 19 -0.9 0.1 -0.6 -0.5 20 -1.5 -1.0 -0.6 1.2
为了在上面创建的数据框的每一行中找到值大于0.5的列名,请将以下代码添加到上述代码片段中:
y1<-round(rnorm(20),1) y2<-round(rnorm(20),1) y3<-round(rnorm(20),1) y4<-round(rnorm(20),1) df2<-data.frame(y1,y2,y3,y4) lapply(apply(df2,1, function(x) which(x0.5)),names)
输出
如果将以上所有代码片段作为一个程序执行,则会生成以下输出:
[[1]] character(0) [[2]] [1] "y3" [[3]] [1] "y1" [[4]] character(0) [[5]] [1] "y2" "y4" [[6]] [1] "y2" [[7]] [1] "y2" [[8]] [1] "y1" "y2" [[9]] [1] "y1" "y3" "y4" [[10]] [1] "y3" [[11]] character(0) [[12]] [1] "y1" [[13]] [1] "y2" [[14]] [1] "y1" "y4" [[15]] [1] "y2" "y3" [[16]] character(0) [[17]] [1] "y2" "y4" [[18]] [1] "y1" "y4" [[19]] character(0) [[20]] [1] "y4"
广告