R - 在其他列的给定条件下从列中提取值

时间:2015-02-01 10:30:51

标签: r

我有以下给出:

TR  Avg RE
3   0
3   0
4   209.3524872
3   185.6542898
3   0
3   0
3   0
4   136.7522375
4   157.6887675
4   0
3   202.8994858
3   0
3   89.45242983
4   0
3   0
3   218.4987273
3   192.4212849

我想只提取TR等于4的RE值 - 我该怎么做?

1 个答案:

答案 0 :(得分:14)

你已经得到了大卫评论的回答。但是,对于下面的兴趣,很少有其他方法可以做到。

<强> 代码:

# Method 1:
df[df$TR == 4, "RE"]

# Method 2:
df[ which(df$TR == 4), "RE"]

# Method 3:
subset(df$RE, df$TR == 4)

# Method 4: You could also use the sqldf package to use sql
# install.packages("sqldf")
library(sqldf)
sqldf('select RE from df where TR = 4')