【问题标题】:Extract values from column under given conditions of other column [duplicate]在其他列的给定条件下从列中提取值[重复]
【发布时间】:2015-03-31 10:52:55
【问题描述】:

我有以下给出:

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 值 - 我该怎么做?

【问题讨论】:

    标签: r


    【解决方案1】:

    您已经从大卫的评论中得到了答案。但是,出于兴趣,以下是一些额外的方法。

    代码:

    # 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')
    

    【讨论】:

    • 如果我有两个条件TR和QED怎么办?我尝试了子集(df$RE,df$TR == 1,df$QED == 1)。抱歉,如果这听起来很懒惰..我搜索但到目前为止一无所获
    • 使用& 运算符,即subset (df$RE, df$TR ==1 & df$QED ==1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2020-07-14
    • 2020-11-01
    • 2021-05-12
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多