问题
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
回答1:
Try
df[x,]$Species
or
df[,"Species"][x]
or indeed
df[x,"Species"]
来源:https://stackoverflow.com/questions/17439396/extract-values-from-a-data-frame-based-on-a-vector-of-row-numbers-in-r