Reputation: 3393
I have a list:
my.list
[[1]]
[1] 1899 1899 1899 1899 1899 1899 1899 1899
[[2]]
[1] 86 86 86 86 86 86 86
[[3]]
[1] 97 97 97 97 97 97 97 97 97 97
[[4]]
[1] 3 3 3 3
[[5]]
[1] 83 83 83 83 83 83 83 83 83 83
[[6]]
[1] 2 2 2 2 2 2 2 2
[[7]]
[1] 10 10 10 10 10 10 10 10
Dput:
list(c(1899L, 1899L, 1899L, 1899L, 1899L, 1899L, 1899L, 1899L
), c(86L, 86L, 86L, 86L, 86L, 86L, 86L), c(97L, 97L, 97L, 97L,
97L, 97L, 97L, 97L, 97L, 97L), c(3L, 3L, 3L, 3L), c(83L, 83L,
83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L), c(2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L),c(10L, 10L, 10L, 10L, 10L, 10L ))
I would like to modify the values in each list element so I get:
my.listfinal
[[1]]
[1] 1899 1899 1899 1899 1899 1899 1899 1899
[[2]]
[1] 1886 1886 1886 1886 1886 1886 1886
[[3]]
[1] 1897 1897 1897 1897 1897 1897 1897 1897 1897 1897
[[4]]
[1] 1903 1903 1903 1903
[[5]]
[1] 1883 1883 1883 1883 1883 1883 1883 1883 1883 1883
[[6]]
[1] 1902 1902 1902 1902 1902 1902 1902 1902
[[7]]
[1] 1910 1910 1910 1910 1910 1910 1910 1910
With other words, I would like to paste values with either 1901, 18, or leave them alone if nchar==4
I tried:
xxfinal=lapply(xxxm,function(x) {
ifelse(unique(x) <10,paste0("190", x),ifelse(unique(x)==10,paste0("19",x),
ifelse(nchar(unique(x))==2 & unique(x)>10,paste0("18",x),x)))
}
)
But that gives me an error
:
Error in ifelse(unique(x) == 10, paste0("19", x), ifelse(nchar(unique(x) == :
unused argument (x)
What am I doing wrong?
EDIT:
That works:
my.listfinal=lapply(my.list,function(x) {
as.numeric(ifelse(x<10,paste0("190", x),ifelse(x==10,paste0("19",x),
ifelse(nchar(x)==2 & x>10,paste0("18",x),x))))
} )
Thanks for you comments!
Upvotes: 0
Views: 167
Reputation: 81743
Here's a way to do it:
lapply(my.list, function(x) x %% 1800 + 1800 + 100 * (x <= 10))
The result:
[[1]]
[1] 1899 1899 1899 1899 1899 1899 1899 1899
[[2]]
[1] 1886 1886 1886 1886 1886 1886 1886
[[3]]
[1] 1897 1897 1897 1897 1897 1897 1897 1897 1897 1897
[[4]]
[1] 1903 1903 1903 1903
[[5]]
[1] 1883 1883 1883 1883 1883 1883 1883 1883 1883 1883
[[6]]
[1] 1902 1902 1902 1902 1902 1902 1902 1902
[[7]]
[1] 1910 1910 1910 1910 1910 1910
Upvotes: 3