Home > front end >  multiple data.table columns to one column of vectors
multiple data.table columns to one column of vectors

Time:11-06

I have a data.table like this:

tab = data.table(V1 = c('a', 'b', 'c'),
           V2 = c('d', 'e', 'f'),
           V3 = c('g', 'h', 'i'),
           id = c(1,2,3))

From the columns V1,V2,V3 of this table, I'd like to get for row i a vector of c(V1[i],V2[i], V3[i])

I can get a list of the desired vectors like this:

lapply(1:tab[, .N], function(x) tab[x, c(V1, V2, V3)])

Which returns:

[[1]]
[1] "a" "d" "g"

[[2]]
[1] "b" "e" "h"

[[3]]
[1] "c" "f" "i"

But I think this is probably slow and not very data.table-like.

Also, I'd like to generalize it, do that I don't have explicitly type V1, V2, V3, but rather pass a vector of column names to be processed this way.

CodePudding user response:

Using split

split(as.matrix(tab[, V1:V3]), tab$id)
$`1`
[1] "a" "d" "g"

$`2`
[1] "b" "e" "h"

$`3`
[1] "c" "f" "i"

CodePudding user response:

Try this?

> asplit(unname(tab[, V1:V3]), 1)
[[1]]
"a" "d" "g"

[[2]]
"b" "e" "h"

[[3]]
"c" "f" "i"

CodePudding user response:

as.list(transpose(tab[, .(V1, V2, V3)]))

Or as a function

tdt <- function(DT, cols) as.list(transpose(DT[, .SD, .SDcols = cols]))

tdt(tab, c('V1', 'V2', 'V3'))

# $V1
# [1] "a" "d" "g"
# 
# $V2
# [1] "b" "e" "h"
# 
# $V3
# [1] "c" "f" "i"

CodePudding user response:

tab[, 1:3] |> transpose() |> as.list()

$V1
[1] "a" "d" "g"

$V2
[1] "b" "e" "h"

$V3
[1] "c" "f" "i"
  • Related