Linked Questions

Popular Questions

How to use column value as column name?

Asked by At

I have a dataframe with some columns with data plus a column containing column names:

mydf <- data.frame(
  "aa" = 11:15,
  "ab" = 21:25,
  "ac" = 31:35,
  "nn" = c("aa", "aa", "ac", "ab", "aa"))

I would like to get a vector from columns "aa", "ab", or "ac". That column name for every row is specified in the "nn" column.

So I should get 11, 12, 33, 24, 15.

Can this be done without loops? mydf[mydf$nn] does not work.

Related Questions