我希望转换一个数据帧(或数据表),如
dt <- data.table(a = c(1,2,4), b = c(NA,3,5), d = c(NA,8,NA))
进入具有一列的东西,例如
dt <- data.table(combined = list(list(1,NA,NA),list(2,3,8),list(4,5,NA))
以下工作均不属于:
dt[,combined := as.list(a,b,d)] dt[,combined := do.call(list,list(a,b,d))] dt[,combined := cbind(a,b,d)] dt[,combined := lapply(list(a,b,d),list)]
请注意,这与此处的问题不同,data.frame行到列表,返回不同形状的对象(我认为它只是一个普通列表,每行作为列表中的项,而不是列表的向量)
您可以使用purrr::transpose()
,将向量列表转换为列表列表:
dt[, combined := purrr::transpose(.(a,b,d))] dt # a b d combined #1: 1 NA NA#2: 2 3 8
#3: 4 5 NA
combined = list(list(1,NA_real_,NA_real_),list(2,3,8),list(4,5,NA_real_)) identical(dt$combined, combined) # [1] TRUE
如果您不想使用额外的包,可以data.table::transpose
稍加努力地使用:
dt[, combined := lapply(transpose(.(a,b,d)), as.list)] identical(dt$combined, combined) # [1] TRUE
要使@ David的注释更加明确,并将data.table方法概括为SE版本,它允许您将列名作为字符向量传递并避免硬编码列名,您可以这样做,以了解有关SE与NSE的更多信息(您可以参考小插图("nse")):
dt[, combined := lapply(transpose(.SD), as.list), .SDcols = c("a","b","d")]
这会使所有子列表都命名,但值对应于组合列表:
identical(lapply(dt$combined, setNames, NULL), combined) # [1] TRUE
如果您不想使用任何功能:
dt[, combined := .(.(.SD)), by = 1:nrow(dt)] # because you want to transform each row to a list, normally you can group the data frame # by the row id, and turn each row into a list, and store the references in a new list # which will be a column in the resulted data.table dt$combined #[[1]] # a b d #1: 1 NA NA #[[2]] # a b d #1: 2 3 8 #[[3]] # a b d #1: 4 5 NA
或者:dt[, combined := .(.(.(a,b,d))), by = 1:nrow(dt)]
它使您更接近确切的期望输出.