使用ifelse循环添加新列

7

我希望让我的代码更高效。我有一个调查,其中我的数据看起来像:

survey <- data.frame(
                     x = c(1, 6, 2, 60, 75, 40, 27, 10),
                     y = c(100, 340, 670, 700, 450, 200, 136, 145)) 

#Two lists:
A <- c(3, 6, 7, 27, 40, 41)
t <- c(0.10, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16)

我所做的是创建新列,如下所示:
z <- ifelse(survey$x %in% A), 0, min(t))

for (i in t) {
  survey[paste0("T",i)] <-z
  survey[paste0("T",i)] <-ifelse (z > 0, i, z)
}

但是用那段代码需要花费一些时间,有没有更好的方法来实现呢?


3
你可以直接执行 survey[paste0("T", t)] <- lapply(t, function(y) ifelse(survey$x %in% A, 0, y)) 这段代码。它的作用是将 survey$x 中属于集合 A 的元素设为0,并分别赋值给 survey 数据框中列名称为 T 后跟当前 t 值的列。 - akrun
2
你也可以检查一下 data.table ,它可能会更快,即 setDT(survey)[, paste0("T", t) := 0]; for(j in t) {set(survey, i = which(!survey$x %in% A), j = paste0("T", j), value = j)} - akrun
3个回答

8

正如OP提到的执行速度,data.table 选项会更快。

library(data.table)
i1 <- !survey$x %in% A

setDT(survey)[, paste0("T", t) := 0]
for(j in t) {
    set(survey2, i = which(i1), j = paste0("T", j), value = j) 
    }

基准测试

set.seed(24)
survey1 <- data.frame(x = sample(survey$x, 1e7, replace = TRUE),
       y = sample(survey$y, 1e7, replace = TRUE))

survey2 <- copy(survey1)

system.time({

survey1[paste0("T", t)] <- lapply(t, function(y) ifelse(survey1$x %in% A, 0, y))
})
# user  system elapsed 
#   8.20    2.75   11.03 

system.time({
i1 <- !survey2$x %in% A

setDT(survey2)[, paste0("T", t) := 0]
for(j in t) {
     set(survey2, i = which(i1), j = paste0("T", j), value = j) 
        }

})
# user  system elapsed 
#   0.97    0.31    1.28 

为什么在输入列时不需要逗号?例如,为什么不是 survey1[, paste0("T", t)] 而是 survey1[paste0("T", t)] - user63230
@user63230 setDT(survey)[, paste0("T", t) := 0] 逗号用于指定 i,即在 data.table 中它是 i, j, by 格式。 - akrun
抱歉,我的意思不是关于 data.table 的解决方案。我指的是 survey1[paste0("T", t)] <- lapply(t, function(y) ifelse(survey1$x %in% A, 0, y)) 这一段代码中,你通常必须引用你正在处理的列[, ]。 - user63230

5
您可以使用sapply来完成此操作:
#just make your new cols with sapply
newcols <- sapply(t, function(i) ifelse (z > 0, i, z))
#add the names you wanted
colnames(newcols) <- paste0("T", seq_along(t))
#merge to your original survey data set
cbind(survey, newcols)

#   x   y  T1   T2   T3   T4   T5   T6   T7
#1  1 100 0.1 0.11 0.12 0.13 0.14 0.15 0.16
#2  6 340 0.0 0.00 0.00 0.00 0.00 0.00 0.00
#3  2 670 0.1 0.11 0.12 0.13 0.14 0.15 0.16
#4 60 700 0.1 0.11 0.12 0.13 0.14 0.15 0.16
#5 75 450 0.1 0.11 0.12 0.13 0.14 0.15 0.16
#6 40 200 0.0 0.00 0.00 0.00 0.00 0.00 0.00
#7 27 136 0.0 0.00 0.00 0.00 0.00 0.00 0.00
#8 10 145 0.1 0.11 0.12 0.13 0.14 0.15 0.16

3

使用矩阵乘法可能会更快。

dat <- cbind(survey, matrix(!survey$x %in% A) %*% t)
   x   y   1    2    3    4    5    6    7
1  1 100 0.1 0.11 0.12 0.13 0.14 0.15 0.16
2  6 340 0.0 0.00 0.00 0.00 0.00 0.00 0.00
3  2 670 0.1 0.11 0.12 0.13 0.14 0.15 0.16
4 60 700 0.1 0.11 0.12 0.13 0.14 0.15 0.16
5 75 450 0.1 0.11 0.12 0.13 0.14 0.15 0.16
6 40 200 0.0 0.00 0.00 0.00 0.00 0.00 0.00
7 27 136 0.0 0.00 0.00 0.00 0.00 0.00 0.00
8 10 145 0.1 0.11 0.12 0.13 0.14 0.15 0.16

在这里,matrix(!survey$x %in% A)根据survey$x的值是否存在于A中构建了一个nX1的TRUE和FALSE矩阵。这个结果被矩阵乘法(%*%)与t作为1Xn矩阵相乘。然后得到所需的输出结果。
如果需要,您可以使用lyzander答案中的代码添加列名。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接