How to select values from data.table
based on a vector of column indexes.
I have an integer vector of the same length as the number of rows in a data.table:
set.seed(100)
col.indexes <- sample(c(1:4), 150, replace = TRUE)
How to create a vector of values based on it? e.g. this without for
loop:
iris <- setDT(iris)
res <- c()
for(i in 1:150) {
res[i] <- iris[i, .SD, .SDcols = col.indexes[i]]
}
res <- unlist(res)
this is loosely based on this question: How to subset the next column in R
We can do a group by sequence of rows and extract the values
res <- iris[, col1 := col.indexes][, .SD[[col1[1]]], 1:nrow(iris)]$V1
Or in base R
, it can be done in a vectorized way
iris <- setDF(iris)
iris[1:4][cbind(seq_len(nrow(iris)), col.indexes)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With