我有一个简单的数据框,其中包含两个向量“速度”和“ID”,如下所示:
mydata
ID Speed
1 1 6.031847
2 1 7.050654
3 1 7.769475
4 1 8.838968
5 1 9.956571
6 1 11.146864
7 1 11.967616
8 1 13.078422
9 1 14.214301
10 1 14.974159
11 2 16.048627
12 2 17.070484
.. . .........
我想使用以下方法制作速度值前 20% 的数据帧的子集:
subset0.20<-subset(mydata, Speed > quantile(Speed, prob = 1 - 20/100, na.rm=T))
但我不希望它用于整个数据集,因为这会给我返回每个 ID 的不相等数量的值。
因此,必须为每个 ID 计算前 20% 的值,然后将每个 ID 的结果组合到一个新的数据帧中。然后这个数据框将包含 8 行(这是我的原始数据集的 20%,它有 40 行)
所以我做了一些指甲咬伤并拔出一些头发并尝试了“for循环”,例如:
for(i in 1:length(ID)){
subset0.80<-subset(mydata[i], GForce > quantile(Speed, prob = 1 - 20/100, na.rm=T))
}
和应用之类的东西:
apply(mydata$Speed, 1 ,function(x) (subset(x > quantile(Speed, prob = 1 - 20/100, na.rm=T))))
但我只是对 R 缺乏经验才能让它发挥作用。任何可以帮助我并向我解释我在这里做错的事情的人吗?
dput(mydata)
structure(list(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4), Speed = c(6.03184705225504, 7.05065401832249,
7.76947483668907, 8.83896842017956, 9.95657139135043, 11.1468640558647,
11.9676155772803, 13.0784218506988, 14.2143010441769, 14.9741594881612,
16.0486271520862, 17.0704843261466, 17.9324808839116, 19.1169673939822,
20.0528330256269, 20.9320440815571, 22.0379467007031, 22.962355355126,
24.0764744246649, 25.1182530133201, 26.0456043859692, 26.9528777031822,
27.9414746553538, 29.129640434174, 29.9443040639644, 30.9226103003052,
31.9932286699133, 32.9925644101585, 33.9930708538141, 35.0124438238874,
35.9215486087666, 36.9015465999988, 38.1044534443389, 39.0368063088987,
40.272189714015, 40.8993100278334, 41.9790311160737, 43.1027190745506,
43.8575622361406, 45.0499599122387)), .Names = c("ID", "Speed"
), row.names = c(NA, -40L), class = "data.frame")