【问题标题】:mutate string sequence using values from other columns使用来自其他列的值改变字符串序列
【发布时间】:2021-02-28 23:07:32
【问题描述】:

我有一堆列,有开始年份和结束年份。我需要根据“每年的样本”列将它们转换为序列,但并非每一列都有这个值,有些只有起始年份而没有结束,因此涉及很多“ifs”。

这是我到目前为止所做的:

df <- data_frame(first_year = c(1990, 2000, 1987, 1970, 1988),
           last_year = c(2010, 2020, 2004, 2018, NA),
           samples_per_year = c(NA, NA, 4, 2, NA)) 

df %>%
  mutate(middle_years = case_when(is.na(samples_per_year) ~ map2_chr(first_year, last_year, ~ toString(if(!is.na(.y)) .x:.y else .x))))

这适用于每年仅采样一次的所有行,但不适用于多次采样的行。

我尝试使用length_outseq() 从第一个样本年到最后一个样本年添加另一个case_when 并使用toString 来延长序列,但它不起作用。

df %>%
  mutate(middle_years = case_when(is.na(samples_per_year) ~ map2_chr(first_year, last_year, ~ toString(if(!is.na(.y)) .x:.y else .x)),
                                  !is.na(samples_per_year) ~ map2_chr(first_year, last_year, ~ toString(seq(from=.y,to=.x, length.out = (.y-.x)*samples_per_year)))))

期望的输出:

# A tibble: 5 x 4
  first_year last_year samples_per_year middle_years                                            
       <dbl>     <dbl>            <dbl> <chr>                                                   
1       1990      2010             NA 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1…
2       2000      2020             NA 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2…
3       1987      2004              4 1987, 1987.25, 1987.50, 1987.75, 1988, 1988.25, 1988.50...
4       1970      2018              2 1970, 1970.5, 1971, 1971.5, 1972, 1972.5, 1973, 1973.5...                                                      
5       1988        NA             NA 1988  

【问题讨论】:

    标签: r dictionary dplyr tostring


    【解决方案1】:

    您可以编写一个自定义序列函数来执行此操作:

      my_seq<- function(from, to, mid_len){
          if(is.na(from) & is.na(to)) NA
          else if(is.na(from)) to
          else if(is.na(to)) from 
          else {
             by <- if(is.na(mid_len)) 1 else 1/mid_len
            seq(from, to,by)
          }
      }
    
    
    df %>% 
       rowwise() %>%
       mutate(mid_years = toString(my_seq(first_year, last_year, samples_per_year)))
    
    # A tibble: 5 x 4
    # Rowwise: 
      first_year last_year samples_per_year mid_years                                                          
           <dbl>     <dbl>            <dbl> <chr>                                                              
    1       1990      2010               NA 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, ~
    2       2000      2020               NA 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, ~
    3       1987      2004                4 1987, 1987.25, 1987.5, 1987.75, 1988, 1988.25, 1988.5, 1988.75, 19~
    4       1970      2018                2 1970, 1970.5, 1971, 1971.5, 1972, 1972.5, 1973, 1973.5, 1974, 1974~
    5       1988        NA               NA 1988   
    

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多