I have a dataset containing n observation and a column containing observation indices, e.g.
col1 col2 col3 ID
12 0 4 1
6 5 3 1
5 21 42 2
and want to create a new column based on my index like
col1 col2 col3 ID col_new
12 0 4 1 12
6 5 3 1 6
5 21 42 2 21
without for loops. Actually I'm doing
col_new <- rep(NA, length(ID))
for (i in 1:length(ID))
{
col_new[i] <- df[i, ID[i]]
}
Is there a better or (tidyverse
) way?