在for循环data.table中创建新列

时间:2016-03-02 10:17:25

标签: r data.table

我想在for循环中创建新列。

impute.sum <- function(x) replace(x, is.na(x), -sum(x, na.rm = TRUE))
df = data.table(user = c(1,1,2,2,3,3,3), x1 = c(NA, 2, 4, NA, NA, 1, 1), x2 = c(1, NA, NA, 3, 4, NA, NA))
df[, x1_1 := impute.sum(x1), by = user]
df[, x2_1 := impute.sum(x2), by = user]

我不确切知道我将拥有多少列,所以我需要使用for循环。

1 个答案:

答案 0 :(得分:2)

即使不使用for循环

也有答案
impute.sum <- function(x) replace(x, is.na(x), -sum(x, na.rm = TRUE))
df = data.table(user = c(1,1,2,2,3,3,3), x1 = c(NA, 2, 4, NA, NA, 1, 1), x2 = c(1, NA, NA, 3, 4, NA, NA))
in_cols  = c("x1", "x2")
out_cols = c("x1_1", "x2_1")
df[, c(out_cols) := lapply(.SD, impute.sum), by = user, .SDcols = in_cols]