简体   繁体   中英

extract values from a data frame based on a vector of row numbers in R

I am trying to extract a vector of values from a data frame based on a vector of row numbers;

df<-iris
x<-c(1,5,15,8,7,2)

I want the corresponding Species of the row numbers listed in the vector x. I have tried something like df$Species[x,] and df$Species[list(x),] to no avail. I am sure the answer is something really simple but I just can't see it!

Thanks in advance

Try

df[x,]$Species

or

df[,"Species"][x]

or indeed

df[x,"Species"]

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