Reputation: 35
R novice here with what is probably a silly/ simple question.
I am trying to use R to find and replace within a vector "census.new" (basically trying to use R to do what excel does with find/ replace). However, because the original data (what I am searching for) overlaps partially with what I am trying to replace, I am ending up with some funky results.
I am trying to replace 2 with 2008b, 3 with 2009a, etc.
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)
The resulting table is:
2002020202013babbb 2009a 2009b 202013ba00202012bbb 202013ba009a
6268 3129 3129 3129 3129
20202013baa 20202013bab 2020202013baaa 2020202013babb
3129 3129 3129 3129
Thanks in advance!
Upvotes: 1
Views: 65
Reputation: 160577
Your choice to use sub
is handicapped by its use of regular expressions: for example, it is replacing every instance of "2" by "2008b", so "2012" becomes "2008b012008b". Since you subsequently look for other (similar) strings, it cascades (snow-balls) quickly. As an example:
gsub('2', '2008b', c('2', '22'), fixed = TRUE)
## [1] "2008b" "2008b2008b"
There are several ways around it, some more elegant than others. First, I'll generate some unique data, since I don't know exactly how yours looks:
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
with regular expressionsThis is by far not the best or optimal solution. If you want/need to continue down the road of using sub
(or gsub
), you should read up on regular expressions (many many more websites and tutorials exist for regexps).
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
You can nest these (sub('^2$', '2008b', sub('^3$', '2009a', dat))
), but it doesn't make it more readable or extensible. It's ugly, too (IMHO).
This function is meant for (what I'm inferring is) your purpose.
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
And the performance of car::recode
isn't bad:
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
Upvotes: 2