【问题标题】:R apply formula and conditional logic to dataframeR将公式和条件逻辑应用于数据框
【发布时间】:2020-05-22 23:18:09
【问题描述】:

我有一个数据框,其中包含样品的湿重和干重的数值变量,比如土壤。在此数据帧中,一些值等于 0,而其他值大于零。我想将公式应用于变量以创建新变量,但仅适用于大于零的数据对。到目前为止,我已经尝试过dplyrfilter功能。

我想使用以下公式创建新变量:

水分含量=(湿重-干重)/湿重

这是我迄今为止尝试过的代码:

dry_weight <- c(0,1,0,2,0,3,4,5,6,7)
wet_weight <- c(1,0,2,4,0,1,4,0,5,0)
weights <- data.frame(dry_weight, wet_weight)
weights$moisture <- weights %>%
  filter(weights$wet_weight > 0, weights$dry_weight >0) %>%
  mutate((weights$wet_weight-weights$dry_weight)/weights$wet_weight)

我不确定mutate 是否是正确的方法,但是当我执行代码时,我得到:

"Error: Column `(weights$wet_weight - weights$dry_weight)/weights$wet_weight` must
 be length 4 (the number of rows) or one, not 10"

任何建议将不胜感激。

【问题讨论】:

    标签: r tidyverse dplyr


    【解决方案1】:

    另一种方法是简单地使用base R

    weights$moisture <- 
                  ifelse(weights$dry_weight*weights$wet_weight > 0
                         , 1-weights$dry_weight/weights$wet_weight
                         , NA)
    weights
       dry_weight wet_weight moisture
    1           0          1       NA
    2           1          0       NA
    3           0          2       NA
    4           2          4      0.5
    5           0          0       NA
    6           3          1     -2.0
    7           4          4      0.0
    8           5          0       NA
    9           6          5     -0.2
    10          7          0       NA
    

    ifelse 是矢量化的ififelse(condition, if true then this, if false then that)。在这里,我检查两个值是否严格大于零,在这种情况下我返回水分,否则我返回NA

    【讨论】:

      【解决方案2】:

      我希望这能让你开始。

      首先,当您使用管道 (%&gt;%) 时,无需每次都输入weights$

      其次,对于mutate,您需要有一个分配有= 的左侧。

      weights %>%
        dplyr::filter(wet_weight > 0 & dry_weight > 0) %>%
        mutate(moisture = (wet_weight - dry_weight)/wet_weight)
      #  dry_weight wet_weight moisture
      #1          2          4      0.5
      #2          3          1     -2.0
      #3          4          4      0.0
      #4          6          5     -0.2
      

      请记住,如果您想将此分配回weights,只需将weights &lt;- 添加到第一行的开头即可。

      【讨论】:

        【解决方案3】:

        矢量化方式:

        #Initialize column to NA
        weights$moisture <- NA
        #Get the index where dry_weight > 0 and wet_weight > 0
        inds <- with(weights, dry_weight > 0 & wet_weight >0)
        #Calculate using the formula and replace the value.
        weights$moisture[inds] <- with(weights, 
                                  (wet_weight[inds] - dry_weight[inds])/wet_weight[inds])
        
        
        weights
        #   dry_weight wet_weight moisture
        #1           0          1       NA
        #2           1          0       NA
        #3           0          2       NA
        #4           2          4      0.5
        #5           0          0       NA
        #6           3          1     -2.0
        #7           4          4      0.0
        #8           5          0       NA
        #9           6          5     -0.2
        #10          7          0       NA
        

        【讨论】:

          猜你喜欢
          • 2021-07-12
          • 2017-09-30
          • 1970-01-01
          • 2017-10-10
          • 2021-02-21
          • 2013-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多