我有这个数据集
names age height weight
1 joe 23 170 65
2 jack 25 173 73
3 jim 27 168 71
4 james 23 172 70
我希望得到173岁以下人群的所有信息,所以我这样做
short <- c(t(subset(dfD, height < 173, select = names)))
short
会返回此
[1] "joe" "jim" "jack"
但是当我试图从该列表中获取所有数据时......
dfD[(dfD[1] == short),]
只返回此
names age height weight
1 joe 23 170 65
我怎样才能让它发挥作用?
我希望能够创建名单并以这种方式获取所有数据......怎么可能?
答案 0 :(得分:2)
subset()
和dlply()
将选择数据框的子集,并将数据框更改为列表。这是你想要的吗?
## Your data
df = read.table(text = " names age height weight
joe 23 170 65
jack 25 173 73
jim 27 168 71
james 23 172 70", header = TRUE, sep = "")
library(plyr)
short = subset(df, height < 173)
data.list = dlply(short, "names")
OR
short = subset(df, height < 173, "names")
subset(df, names %in% short[[1]])