简体   繁体   中英

Select column from data frame based on dynamic value in R

I've been banging my head against this problem and feel certain there must be an efficient way to do this in R that doesn't involve writing a for loop. Any suggestions much appreciated!

I'd like to create a new column in a data frame that contains values from existing columns in the dataframe, but where the column whose value is selected is dynamically specified. An example will help clarify:

> mydata <- head(mtcars)
> mydata
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
> myquery <- c("cyl","cyl","gear","gear","carb", "carb")

At this point, I'd like to know if there's a simple R function that will select the value of column myquery for each row of mydata , in other words:

f(mydata, myquery)
6 6 4 3 2 1

Thanks in advance if anyone knows of a simple and efficient version way to write f , thanks in advance for your time.

You can index a data.frame with a matrix to achieve that behavior

dd<-head(mtcars)
myquery <- c("cyl","cyl","gear","gear","carb", "carb")
dd[cbind(seq_along(myquery), match(myquery, names(dd)))]
# [1] 6 6 4 3 2 1

The first column of the matrix is the row, the second is the column (and note when using this method there is no comma in the brackets like when you do a normal [,] subset. Here i converted the myqeury values to their numeric column indices using match so both columns of the matrix are the same type (as they have to be). You could have also used a character matrix if you used the row names to index the rows. Thus

dd[cbind(rownames(dd), myquery)]
# [1] 6 6 4 3 2 1

also works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM