I have a vector of different column names and I want to be able to loop over each of them to extract that column from a data.frame. For example, consider the data set mtcars and some variable names stored in a character vector cols. When I try to select a variable from mtcars using a dynamic subset of cols, nether of these work
cols <- c("mpg", "cyl", "am")
col <- cols[1]
col
# [1] "mpg"
mtcars$col
# NULL
mtcars$cols[1]
# NULL
how can I get these to return the same values as
mtcars$mpg
Furthermore how can I loop over all the columns in cols to get the values in some sort of loop.
for(x in seq_along(cols)) {
value <- mtcars[ order(mtcars$cols[x]), ]
}
You can't do that kind of subsetting with
$. In the source code (R/src/main/subset.c) it states:Second argument? What?! You have to realise that
$, like everything else in R, (including for instance(,+,^etc) is a function, that takes arguments and is evaluated.df$V1could be rewritten asor indeed
But...
...for instance will never work, nor will anything else that must first be evaluated in the second argument. You may only pass a string which is never evaluated.
Instead use
[(or[[if you want to extract only a single column as a vector).For example,
You can perform the ordering without loops, using
do.callto construct the call toorder. Here is a reproducible example below: