查找和替换,无需回溯搜索



R 新手在这里可能是一个愚蠢/简单的问题。

我正在尝试使用 R 在向量"census.new"中查找和替换(基本上是尝试使用 R 来执行 excel 对查找/替换所做的工作(。但是,由于原始数据(我正在搜索的数据(与我试图替换的数据部分重叠,因此我最终会得到一些时髦的结果。

我正在尝试将 2 替换为 2008b,将 3 替换为 2009a,依此类推。

census.new<-sub("2","2008b", census.new, fixed=TRUE)
census.new<-sub("3","2009a", census.new, fixed=TRUE)
census.new<-sub("4","2009b", census.new, fixed=TRUE)
census.new<-sub("5","2010a", census.new, fixed=TRUE)
census.new<-sub("8","2011b", census.new, fixed=TRUE)
census.new<-sub("10","2012a", census.new, fixed=TRUE)
census.new<-sub("11","2012b", census.new, fixed=TRUE)
census.new<-sub("12","2013a", census.new, fixed=TRUE)
census.new<-sub("13","2013b", census.new, fixed=TRUE)
table(census.new)

生成的表为:

2002020202013babbb    2009a     2009b     202013ba00202012bbb    202013ba009a
              6268     3129      3129                    3129            3129
20202013baa         20202013bab      2020202013baaa   2020202013babb
       3129                3129                3129             3129 

提前感谢!

你选择使用 sub 会受到正则表达式使用的限制:例如,它将"2"的每个实例替换为"2008b",因此"2012"变为"2008b012008b"。由于您随后会寻找其他(类似的(字符串,因此它会快速级联(雪球(。举个例子:

gsub('2', '2008b', c('2', '22'), fixed = TRUE)
## [1] "2008b"      "2008b2008b"

有几种方法可以绕过它,有些方法比其他方法更优雅。首先,我将生成一些独特的数据,因为我不知道你的确切外观:

set.seed(42)
dat <- sample(as.character(c(2, 3, 4, 5, 8, 10, 11, 12, 13)),
              size = 31300, replace = TRUE)
table(dat)
## dat
##    2    3    4    5    8   10   11   12   13 
## 3577 3573 3275 3499 3453 3481 3487 3439 3516 

使用正则表达式sub

到目前为止,这不是最佳或最佳解决方案。如果你想/需要继续使用sub(或gsub(的道路,你应该阅读正则表达式(还有更多的正则表达式网站和教程(。

dat1 <- sub('^2$', '2008b', dat)
dat1 <- sub('^3$', '2009a', dat1)
dat1 <- sub('^4$', '2009b', dat1)
dat1 <- sub('^5$', '2010a', dat1)
dat1 <- sub('^8$', '2011b', dat1)
dat1 <- sub('^10$', '2012a', dat1)
dat1 <- sub('^11$', '2012b', dat1)
dat1 <- sub('^12$', '2013a', dat1)
dat1 <- sub('^13$', '2013b', dat1)
table(dat1)
## dat1
## 2008b 2009a 2009b 2010a 2010b 2012a 2012b 2013a 2013b 
##  3577  3573  3275  3499  3453  3481  3487  3439  3516 

你可以嵌套这些(sub('^2$', '2008b', sub('^3$', '2009a', dat))(,但它不会使其更具可读性或可扩展性。它也很丑陋(恕我直言(。

汽车::重新编码

这个函数是为了(我推断的是(你的目的。

library(car)
dat2 <- recode(dat, "
    2 = '2008b'; 3 = '2009a'; 4 = '2009b';
    5 = '2010a'; 8 = '2011b'; 10 = '2012a';
    11 = '2012b'; 12 = '2013a'; 13 = '2013b'")
table(dat2)
## dat2
## 2008b 2009a 2009b 2010a 2011b 2012a 2012b 2013a 2013b 
##  3577  3573  3275  3499  3453  3481  3487  3439  3516 
identical(dat1, dat2)
## [1] TRUE

相对性能

而且car::recode的表现还不错:

library(microbenchmark)
microbenchmark(
  re = {
    dat1 <- sub('^2$', '2008b', dat)
    dat1 <- sub('^3$', '2009a', dat1)
    dat1 <- sub('^4$', '2009b', dat1)
    dat1 <- sub('^5$', '2010a', dat1)
    dat1 <- sub('^8$', '2011b', dat1)
    dat1 <- sub('^10$', '2012a', dat1)
    dat1 <- sub('^11$', '2012b', dat1)
    dat1 <- sub('^12$', '2013a', dat1)
    dat1 <- sub('^13$', '2013b', dat1)
  },
  car = {
    recode(dat, "
        2 = '2008b'; 3 = '2009a'; 4 = '2009b';
        5 = '2010a'; 8 = '2011b'; 10 = '2012a';
        11 = '2012b'; 12 = '2013a'; 13 = '2013b'")
  }, times = 50)
## Unit: milliseconds
##  expr      min       lq     mean   median       uq       max neval cld
##    re 74.44709 75.97057 78.10516 77.20569 78.43732 124.42665   100   b
##   car 23.21131 24.11697 26.08724 25.74997 26.25260  77.97541   100  a 

最新更新