我有以下数据框:
> res5
A B
1 (75,89.3] (4,14.9]
2 (96.4,100] (-Inf,0]
3 <NA> <NA>
4 (0,75] (4,14.9]
5 <NA> <NA>
6 (89.3,96.4] (4,14.9]
> dput(res5)
structure(list(A = structure(c(3L, 5L, NA, 2L, NA, 4L), .Label = c("(-Inf,0]",
"(0,75]", "(75,89.3]", "(89.3,96.4]", "(96.4,100]", "(100, Inf]"
), class = "factor"), B = structure(c(3L, 1L, NA, 3L, NA, 3L), .Label = c("(-Inf,0]",
"(0,4]", "(4,14.9]", "(14.9,68]", "(68, Inf]"), class = "factor")), .Names = c("A",
"B"), row.names = c(NA, 6L), class = "data.frame")
当我将我的函数应用于此数据帧时,我得到以下结果:
res6<-lapply (names(res5), function (x){as.numeric(!res5[,x] %in% head(levels(res5[,x]), 2))})
> res6
[[1]]
[1] 1 1 1 0 1 1
[[2]]
[1] 1 0 1 1 1 1
除了将所有NA值编码为“1”外,我的功能运行良好。如何阻止我的功能这样做并将NA作为NA? 我怀疑这一点是将“NA”转换为NA(没有引号),我试过
within (res5 , "NA"==NA)
并没有帮助。 非常感谢您提前
答案 0 :(得分:3)
res6 <- lapply (names(res5), function (x){
res <- as.numeric(!res5[,x] %in% head(levels(res5[,x]), 2))
ifelse(is.na(res5[,x]),NA,res)
})
替代方案:
res6 <- lapply (names(res5), function (x){
as.integer(as.logical(as.integer(!res5[,x] %in% head(levels(res5[,x]), 2)) * as.integer(res5[,x])))
})