【发布时间】: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