数据框test1:如下
<br />
id Freq<br />
1 A 1<br />
2 B 3<br />
3 C 2<br />
如何获取id为B的Freq到Freq_B中呢?
代码怎么写?
</p>
数据框test1:如下
<br />
id Freq<br />
1 A 1<br />
2 B 3<br />
3 C 2<br />
回复 第1楼 的 G_will:看看subset()函数: subset(test1,id==B)
谢谢啊。
<br />
test1 = data.frame(id = c("A", "B", "C"), Freq = c(1, 3, 2))<br />
Freq_B = test1[test1$id == "B", ]<br />
</p>
我更青睐直接一点的 ..
似乎较subset
快一点点:
<br />
rm(list=ls())<br />
test1 = data.frame(id = c("A", "B", "C", "C", "A", "B", "A", "B", "B", "C", "C", "A"),<br />
Freq = c(1, 3, 2, 5, 3, 2, 1, 6, 3, 2, 2, 4))<br />
tmp = list()<br />
system.time(<br />
for (i in 1:20000){<br />
tmp[[i]] = test1[test1$id == "B", ]<br />
}<br />
)</p>
<p># user system elapsed<br />
# 12.30 0.00 12.29 </p>
<p>rm(list=ls())<br />
test1 = data.frame(id = c("A", "B", "C", "C", "A", "B", "A", "B", "B", "C", "C", "A"),<br />
Freq = c(1, 3, 2, 5, 3, 2, 1, 6, 3, 2, 2, 4))<br />
tmp = list()<br />
system.time(<br />
for (i in 1:20000){<br />
tmp[[i]] = subset(test1, id == "B")<br />
}<br />
)</p>
<p># user system elapsed<br />
# 15.75 0.00 15.75<br />
</p>很受启发。