Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select data.table values based on a vector of column indexes

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

like image 518
Bulat Avatar asked Jan 26 '23 17:01

Bulat


1 Answers

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)]
like image 50
akrun Avatar answered Feb 23 '23 16:02

akrun