1

我有以下数据框:

> 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)

它没有帮助。非常感谢您提前

4

1 回答 1

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])))  
})
于 2013-06-03T08:22:11.683 回答