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

喜夏-厌秋 提交于 2021-02-10 17:52:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!