【问题标题】:Add column by mutate using nested ifelse conditions使用嵌套的 ifelse 条件通过 mutate 添加列
【发布时间】:2019-08-05 07:44:00
【问题描述】:

我正在尝试根据使用mutate 和plyr 的多个条件在我的长格式数据集中创建一个新列source2。

为了获得新值,我正在寻找我的组中的因子数量。如果只有一个级别,请使用此级别。但是,如果有距离值的组合,请相应地设置:

distance == "b20" & distance == "b5"  =>  "buffer",
distance == "PA" & distance == "b5")  =>  "pa_buff20",
distance == "PA" & distance == "b20") =>  "pa_buff500"

我认为嵌套的ifelse 语句应该这样做,但似乎我的组合不起作用。是因为我首先检查了级别数吗? (组数的第一个ifelse也是我可能不能简单使用case_when的原因。

我的虚拟数据集:

# how to find a year when a lag value overpass the certain threshold
df<- data.frame(year = c(1,1,2,1,5,5,10),
                distance = c("b20", "b5", "b20", "b20", "PA", "b5", "PA"),
                site     = c("a", "a", "b", "c", "d", "d", "e"))


# Create new columns based on number of levels in `distance`
df %>% 
  group_by(site) %>% 
  mutate(source = ifelse(n_distinct(distance) == 1,   # create source column based of number of factors
                       as.character(distance[1]), 'unclear')) %>% 
  mutate(source2 = ifelse(n_distinct(distance) == 1,   # create source column based of number of factors
                          as.character(distance[1]), 
                          ifelse(distance == "b20" & distance == "b5"), "buffer",
                          ifelse(distance == "PA" & distance == "b5"), "pa_buff20",
                          ifelse(distance == "PA" & distance == "b20"), "pa_buff500")) %>% 
  print()

我有和Error in ifelse(n_distinct(distance) == 1, as.character(distance[1]), : unused arguments ("buffer", ifelse(distance == "PA" & distance == "b5"), "pa_buff20", ifelse(distance == "PA" & distance == "b20"), "pa_buff500")

如何更正此ifelse 声明?

预期输出:

   year distance site  source  source2
  <dbl> <fct>    <fct> <chr>   <chr> 
1     1 b20      a     unclear buffer
2     1 b5       a     unclear buffer
3     2 b20      b     b20     b20
4     1 b20      c     b20     b20
5     5 PA       d     unclear pa_buff20
6     5 b5       d     unclear pa_buff20
7    10 PA       e     PA      PA

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我们可以使用case_when 代替嵌套的ifelse,这非常适合这种情况。

    library(dplyr)
    
    df %>%
      mutate(distance = as.character(distance)) %>%
      group_by(site) %>%
      mutate(source2 = case_when(all(c("b20", "b5") %in% distance) ~ "buffer", 
                                 all(c("PA", "b5") %in% distance) ~ "pa_buff20",
                                 all(c("PA", "b20") %in% distance) ~ "pa_buff500",
                                 n_distinct(distance) == 1 ~ distance, 
                                 TRUE ~ NA_character_))
    
    
    #   year distance site  source2  
    #  <dbl> <chr>    <fct> <chr>    
    #1     1 b20      a     buffer   
    #2     1 b5       a     buffer   
    #3     2 b20      b     b20      
    #4     1 b20      c     b20      
    #5     5 PA       d     pa_buff20
    #6     5 b5       d     pa_buff20
    #7    10 PA       e     PA     
    

    如前所述,case_when 是多个嵌套 ifelse 语句的替代方案,其中 LHS 是我们正在检查的条件,RHS 是我们想要返回的值。条件是按顺序评估的。如果默认情况下没有任何条件匹配 NA 返回,则在此处使用 TRUE 条件明确提及。

    【讨论】:

    • 非常感谢,看来可以了!您能否添加更多解释?我想了解case_when,但最后没有得到TRUE statememnt? rdocumentation.org/packages/dplyr/versions/0.7.8/topics/…
    • @maycca 添加了一些解释。 TRUE 语句基本上是上面没有条件匹配时要取的值,这里的值是NA_character_。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多