Reputation: 4180
Say I have two lists:
a = list(1,2)
b = list("x","y")
#a
#[[1]]
#[1] 1
#[[2]]
#[1] 2
#b
#[[1]]
#[1] "x"
#[[2]]
#[1] "y"
I would like the following result:
#[[1]]
#[1] "1x"
#[[2]]
#[1] "2y"
I tried the following:
lapply(a, paste, b)
But the result was not what I expected:
#[[1]]
#[1] "1 x" "1 y"
#[[2]]
#[1] "2 x" "2 y"
I wonder if there is any way to get the desired result - without resorting to any added package or loop.
Thank you in advance!!!
Upvotes: 0
Views: 1197
Reputation: 109994
jigr's answer is correct but I wanted to address your attempted solution. You were at the 1 yard line, run it in for the touch down. You didn't supply the separator to lapply so paste uses the default " "
. Either supply that separator (sep=""
) or even better use paste0
(available with R version <= 2.15) which defaults to the ""
separator.
a = list(1,2)
b = list("x","y")
lapply(a, paste, b, sep="")
lapply(a, paste0, b)
Upvotes: 0
Reputation: 14453
Here is one suggestion:
as.list(paste(a,b, sep=""))
[[1]]
[1] "1x"
[[2]]
[1] "2y"
Upvotes: 3