[英]For each row return the column index and name of non-NA value
I have data frame where each row contain one non- NA
value.我有一个数据框,其中每一行都包含一个非
NA
值。
ED1 ED2 ED3 ED4 ED5
1 NA NA NA NA
NA NA 1 NA NA
NA 1 NA NA NA
NA NA NA NA 1
For each row, I want to get the index and name of the column containing the non- NA
value, ie:对于每一行,我想获取包含非
NA
值的列的索引和名称,即:
Indices: c(1, 3, 2, 5)
, and their corresponding column names: c("ED1" "ED3" "ED2" "ED5")
索引:
c(1, 3, 2, 5)
,及其对应的列名: c("ED1" "ED3" "ED2" "ED5")
There is no need to use an apply()
loop here.这里不需要使用
apply()
循环。 You could use max.col()
in combination with a negated call to is.na()
.您可以将
max.col()
与对is.na()
的否定调用结合使用。
max.col(!is.na(df))
# [1] 1 3 2 5
That gives us the column numbers where the 1s are.这给了我们 1 所在的列号。 To get the column names, we can use that in a vector subset of the
names()
of the data frame.要获取列名,我们可以在数据框的
names()
的向量子集中使用它。
names(df)[max.col(!is.na(df))]
# [1] "ED1" "ED3" "ED2" "ED5"
So we can get the desired data frame, with factor column, by doing所以我们可以通过做
data.frame(EDU = names(df)[max.col(!is.na(df))])
# EDU
# 1 ED1
# 2 ED3
# 3 ED2
# 4 ED5
Data:数据:
df <- structure(list(ED1 = c(1, NA, NA, NA), ED2 = c(NA, NA, 1, NA),
ED3 = c(NA, 1, NA, NA), ED4 = c(NA, NA, NA, NA), ED5 = c(NA,
NA, NA, 1)), .Names = c("ED1", "ED2", "ED3", "ED4", "ED5"
), row.names = c(NA, -4L), class = "data.frame")
df <- data.frame( ED1 = c( 1, NA, NA, NA),
ED2 = c( NA, NA, 1 , NA),
ED3 = c( NA, 1, NA, NA),
ED4 = c( NA, NA, NA, NA),
ED5 = c( NA, NA, NA, 1) )
df_new <- data.frame( EDU = as.factor(apply(df,1,which.min)) )
levels(df_new$EDU) <- paste0("ED",levels(df_new$EDU))
. .
> df
ED1 ED2 ED3 ED4 ED5
1 1 NA NA NA NA
2 NA NA 1 NA NA
3 NA 1 NA NA NA
4 NA NA NA NA 1
> df_new
EDU
1 ED1
2 ED3
3 ED2
4 ED5
Another option is另一种选择是
v1 <- names(df)[+(!is.na(df)) %*% seq_along(df)]
v1
#[1] "ED1" "ED3" "ED2" "ED5"
data.frame(EDU=v1)
Or using pmax
或者使用
pmax
names(df)[do.call(pmax, c(df *col(df), list(na.rm=TRUE)))]
#[1] "ED1" "ED3" "ED2" "ED5"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.