There is no need to use an apply()
loop here. You could use max.col()
in combination with a negated call to is.na()
.
max.col(!is.na(df))
# [1] 1 3 2 5
That gives us the column numbers where the 1s are. To get the column names, we can use that in a vector subset of the names()
of the data frame.
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")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…