【问题标题】:replace with perfect matching using str_replace_all()使用 str_replace_all() 替换为完美匹配
【发布时间】:2021-06-22 01:27:34
【问题描述】:

我想替换字符,但它们在一列中重复。 我可以用完美匹配替换它们吗?

time <-data.frame(c(0,0,1,1,2,2,9,9,10,10,11,11))
names(time)<-c("time")
 time %>%
  dplyr::mutate(year = stringr::str_replace_all(time, pattern = c("0" = "1994",
                                                         "1" = "1995",
                                                         "2" = "1996",
                                                         "9" = "2003",
                                                         "10" = "2004",
                                                         "11" = "2005")))

   time                          year
1     0           1200320035200320034
2     0           1200320035200320034
3     1                    1200320035
4     1                    1200320035
5     2                    1200320036
6     2                    1200320036
7     9                          2003
8     9                          2003
9    10 12003200351200320035200320034
10   10 12003200351200320035200320034
11   11          12003200351200320035
12   11          12003200351200320035

【问题讨论】:

  • 只需做time + 1994
  • 同意前面的回答。但是,如果您确实需要进行查找,请尝试case_when 或left_join。它们对于查找值更有效。

标签: r str-replace


【解决方案1】:
df1 <- transform(time, year = time + 1994)
df1
   time year
1     0 1994
2     0 1994
3     1 1995
4     1 1995
5     2 1996
6     2 1996
7     9 2003
8     9 2003
9    10 2004
10   10 2004
11   11 2005
12   11 2005

【讨论】:

    【解决方案2】:

    替代方法:

    library(tidyverse)
    
    time_df <-data.frame(c(0,0,1,1,2,2,9,9,10,10,11,11))
    names(time_df)<-c("time")
    
    replacement_df <- tribble(
        ~key, ~time,    
        "0" , "1994",
        "1" , "1995",
        "2" , "1996",
        "9" , "2003",
        "10" , "2004",
        "11" , "2005")
    
    time_df <- mutate(time_df, time = as.character(time))
    
    # Using join --------------------------------------------------------------
    
    
    #both columns will be type character after join
    inner_join(time_df, replacement_df, by = c('time' = 'key')) %>% 
        mutate_all(as.numeric) #set all variables to numeric (optional)
    #>    time time.y
    #> 1     0   1994
    #> 2     0   1994
    #> 3     1   1995
    #> 4     1   1995
    #> 5     2   1996
    #> 6     2   1996
    #> 7     9   2003
    #> 8     9   2003
    #> 9    10   2004
    #> 10   10   2004
    #> 11   11   2005
    #> 12   11   2005
    
    
    # using str_replace with purrr:map---------------------------------------------------
    
    replacement <- c("0" = "1994",
                     "1" = "1995",
                     "2" = "1996",
                     "9" = "2003",
                     "10" = "2004",
                     "11" = "2005")
    time_df %>% 
        mutate(time = map_chr(time_df$time, ~str_replace(string = .x, .x, replacement[.x])))
    #>    time
    #> 1  1994
    #> 2  1994
    #> 3  1995
    #> 4  1995
    #> 5  1996
    #> 6  1996
    #> 7  2003
    #> 8  2003
    #> 9  2004
    #> 10 2004
    #> 11 2005
    #> 12 2005
    

    由reprex package (v2.0.0) 于 2021-06-21 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多