I would like to reserve the column names for each list element, so that column names are C1
, C2
and C3
instead of V1
. Is there a way to do it within lapply(samp, function(...) {})
?
library(data.table)
samp <- data.table("C1"=letters[1:3], "C2"=letters[4:6], "C3"=letters[7:9])
samp_list <- lapply(samp, data.table)
> samp_list
$C1
V1
1: a
2: b
3: c
$C2
V1
1: d
2: e
3: f
$C3
V1
1: g
2: h
3: i
Ideally, something like the following. I do not really care about the list name though, in case that is helpful.
> samp_list
$C1
C1
1: a
2: b
3: c
$C2
C2
1: d
2: e
3: f
$C3
C3
1: g
2: h
3: i
答案 0 :(得分:2)
Try looping over the colnames or the index of columns
lapply(seq_along(samp), function(i) samp[,i,with=FALSE])