使用 str_replace_all() 替换为完美匹配
replace with perfect matching using str_replace_all()
我想替换字符,但它们在一列中重复出现。
我可以用完美匹配替换它们吗?
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
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
替代方法:
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
我想替换字符,但它们在一列中重复出现。 我可以用完美匹配替换它们吗?
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
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
替代方法:
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