Dear R experts,

I have the following data.table:

dt<- data.table(A=rep(1:5), B=c(20:24), C=rep(30:34), target.name=c("A","B",
"C","B","A"), target.column=c(1,2,3,2,1), target.value=rep(NA,5))

Columns A, B and C are the variables of interest.
For each row, I want to get the value of the variable given in the column
target.column. For example, in the second row, I want to get the value B.
(the column target.column just gives the corresponding column index).

I plan to store the results in the column target value.

#### Solution 1: very slow

for (i in (1:5)){

dt$target.value[i]<- dt[i, c(dt[, target.column[i]]), with=FALSE]

}

dt


#### Solution 2: faster but still slow

df<- data.frame(dt)

aaa<- lapply(1:5, function(i) df[i, which(colnames(df)==df$target.name[i])])

aaa<- do.call("rbind",aaa)

df$target.value<- aaa
Not convenient on a big data frame because of rbind


#### Other tries:

dt[, c(dt$target.name), with=FALSE]

The solution appears on the diagonal term for some reason...


The original data.table is big (18 million rows), so time efficiency is a
primary concern.
Any advice is very welcome. Thanks for your help.

Aurelien

	[[alternative HTML version deleted]]

