【问题标题】:add column to df according to rule根据规则将列添加到df
【发布时间】:2020-10-13 13:35:24
【问题描述】:

我的数据集是:

unit      date      total
1     2019-04-02      7
1     2020-01-01      5
2     2019-12-01      10
2     2020-01-03      2
3     2019-09-01      3
3     2020-03-03      3

如果每个“单位”的“总计”中的任何值大于或等于 10,我想添加“类别”列:

unit      date      total     category
1     2019-04-02      7          low
1     2020-01-01      5          low
2     2019-12-01      10         high
2     2020-01-03      2          high
3     2019-09-01      3          low
3     2020-03-03      3          low

我尝试了很多方法,例如:

df$category <- "low"
for (i in df$unit){
  if (rowSums(df$total >= 10) > 0){
    df$category <- "high"
  }
}

但没有一个有效。可以请教吗?

【问题讨论】:

    标签: r for-loop if-statement


    【解决方案1】:

    尝试解决每个组中的最大值,然后分配类别。代码如下:

    library(dplyr)
    #Code
    dfnew <- df %>% group_by(unit) %>% mutate(category=ifelse(max(total,na.rm=T)>=10,'High','Low'))
    

    输出:

    # A tibble: 6 x 4
    # Groups:   unit [3]
       unit date       total category
      <int> <chr>      <int> <chr>   
    1     1 2019-04-02     7 Low     
    2     1 2020-01-01     5 Low     
    3     2 2019-12-01    10 High    
    4     2 2020-01-03     2 High    
    5     3 2019-09-01     3 Low     
    6     3 2020-03-03     3 Low   
    

    使用的一些数据:

    #Data
    df <- structure(list(unit = c(1L, 1L, 2L, 2L, 3L, 3L), date = c("2019-04-02", 
    "2020-01-01", "2019-12-01", "2020-01-03", "2019-09-01", "2020-03-03"
    ), total = c(7L, 5L, 10L, 2L, 3L, 3L)), class = "data.frame", row.names = c(NA, 
    -6L))
    

    【讨论】:

    • 非常感谢您建议的方法 - 我恐怕只能选择 1 个正确答案
    • @DanielaRodrigues 很好,没问题!但你也可以投票:)
    【解决方案2】:

    这行得通吗:

    > library(dplyr)
    > df %>% group_by(unit) %>% mutate(category = case_when(max(total) >= 10 ~ 'high', TRUE ~ 'low'))
    # A tibble: 6 x 4
    # Groups:   unit [3]
       unit date                    total category
      <dbl> <dttm>                  <dbl> <chr>   
    1     1 2019-04-02 00:00:00.000     7 low     
    2     1 2020-01-01 00:00:00.000     5 low     
    3     2 2019-12-01 00:00:00.000    10 high    
    4     2 2020-01-03 00:00:00.000     2 high    
    5     3 2019-09-01 00:00:00.000     3 low     
    6     3 2020-03-03 00:00:00.000     3 low     
    > 
    

    【讨论】:

    • 非常感谢您提出的方法 - 我恐怕只能选择 1 个正确答案
    【解决方案3】:

    一个使用 ave 的基本 R 选项,例如,

    transform(
      df,
      category = c("Low","High")[ave(total>=10,unit,FUN = any)+1]
    )
    

    给了

      unit       date total category
    1    1 2019-04-02     7      Low
    2    1 2020-01-01     5      Low
    3    2 2019-12-01    10     High
    4    2 2020-01-03     2     High
    5    3 2019-09-01     3      Low
    6    3 2020-03-03     3      Low
    

    数据

    > dput(df)
    structure(list(unit = c(1L, 1L, 2L, 2L, 3L, 3L), date = c("2019-04-02", 
    "2020-01-01", "2019-12-01", "2020-01-03", "2019-09-01", "2020-03-03"
    ), total = c(7L, 5L, 10L, 2L, 3L, 3L)), class = "data.frame", row.names = c(NA, 
    -6L))
    

    【讨论】:

    • 伟大的base R 男人回来了!!!你是没有tidyverse的世界上唯一的幸存者!
    • @Duck 哈哈,我住在基地 R 星球上 :)
    • 非常感谢您建议的方法 - 我恐怕只能选择 1 个正确答案
    【解决方案4】:

    对于每个unit,您可以检查any 值是否大于等于10,并相应地分配category 值。

    library(dplyr)
    df %>%
      group_by(unit) %>%
      mutate(category = if(any(total >= 10)) 'high' else 'low')
    
    #   unit date       total category
    #  <int> <chr>      <int> <chr>   
    #1     1 2019-04-02     7 low     
    #2     1 2020-01-01     5 low     
    #3     2 2019-12-01    10 high    
    #4     2 2020-01-03     2 high    
    #5     3 2019-09-01     3 low     
    #6     3 2020-03-03     3 low     
    

    同样的逻辑可以在base R中实现

    df$category <- with(df, ave(total, unit, FUN = function(x) 
                            if(any(x >= 10)) 'high' else 'low'))
    

    data.table

    library(data.table)
    setDT(df)[, category := if(any(total >= 10)) 'high' else 'low', unit]
    

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2020-06-09
      • 1970-01-01
      • 2021-09-22
      相关资源
      最近更新 更多