Dear list, I have a vector: my.vector <- c("A", "B", "C", "D", "E", "F", "G") and two other: vec1 <- c("p", "q", "r", "s", "t") vec2 <- c("x", "y", "z") I want to substitute elements "b" and "e" in my.vector with vectors vec1 and vec2 respectively so that the result becomes the vector: c("A", "p", "q", "r" , "s" , "t", "C" , "D", "x", "y", "z", "F", "G") The ordering of the elements is important. Knut Hansen
> On 17-02-2015, at 12:58, Knut Hansen <knut.hansen at uit.no> wrote: > > Dear list, > > I have a vector: > my.vector <- c("A", "B", "C", "D", "E", "F", "G") > > and two other: > vec1 <- c("p", "q", "r", "s", "t") > vec2 <- c("x", "y", "z") > > I want to substitute elements "b" and "e" in my.vector with vectors vec1 and > vec2 respectively so that the result becomes the vector: > c("A", "p", "q", "r" , "s" , "t", "C" , "D", "x", "y", "z", "F", "G") > > The ordering of the elements is important. >Something like this perhaps res <- c("A", "p", "q", "r" , "s" , "t", "C" , "D", "x", "y", "z", "F", "G?) rem <- function(vec, who, replace){ kW <- which(vec == who) z1 <- append(vec,replace,kW) z2 <- z1[ ! z1 %in% vec[kW] ] z2 } x1 <- rem(my.vector,"B",vec1) x2 <- rem(x1,"E",vec2) x2 all.equal(x2,res) Berend
Hi, Here is an implementation.> my.vector[1] "A" "B" "C" "D" "E" "F" "G"> vec1[1] "p" "q" "r" "s" "t"> vec2[1] "x" "y" "z"> final <- as.character(unlist(sapply(my.vector,function(x) > if(x=="B"){vec1}else{if(x=="E"){vec2}else{x}}))) > final[1] "A" "p" "q" "r" "s" "t" "C" "D" "x" "y" "z" "F" "G" -- View this message in context: http://r.789695.n4.nabble.com/Substituting-elements-in-vector-tp4703395p4703431.html Sent from the R help mailing list archive at Nabble.com.
On Feb 17, 2015, at 3:58 AM, Knut Hansen wrote:> Dear list, > > I have a vector: > my.vector <- c("A", "B", "C", "D", "E", "F", "G") > > and two other: > vec1 <- c("p", "q", "r", "s", "t") > vec2 <- c("x", "y", "z") > > I want to substitute elements "b" and "e" in my.vector with vectors vec1 and > vec2 respectively so that the result becomes the vector: > c("A", "p", "q", "r" , "s" , "t", "C" , "D", "x", "y", "z", "F", "G")> my.vlist <- setNames(as.list(my.vector),my.vector) > replist <- list(B=vec1, E=vec2) > my.vlist[c("B","E")] <- replist > unlist(my.vlist)A B1 B2 B3 B4 B5 C D E1 E2 E3 F G "A" "p" "q" "r" "s" "t" "C" "D" "x" "y" "z" "F" "G" Could also have used: my.vlist[ names(replist) ] <- replist .... which I think illustrates the value of character indexing of lists for assignment even better.> The ordering of the elements is important. > > Knut Hansen > >David Winsemius Alameda, CA, USA
Tirsdag 17. februar 2015 15.50.27 skrev David Winsemius:> On Feb 17, 2015, at 3:58 AM, Knut Hansen wrote: > > Dear list, > > > > I have a vector: > > my.vector <- c("A", "B", "C", "D", "E", "F", "G") > > > > and two other: > > vec1 <- c("p", "q", "r", "s", "t") > > vec2 <- c("x", "y", "z") > > > > I want to substitute elements "b" and "e" in my.vector with vectors vec1 > > and vec2 respectively so that the result becomes the vector: > > c("A", "p", "q", "r" , "s" , "t", "C" , "D", "x", "y", "z", "F", "G") > > > > my.vlist <- setNames(as.list(my.vector),my.vector) > > replist <- list(B=vec1, E=vec2) > > my.vlist[c("B","E")] <- replist > > unlist(my.vlist) > > A B1 B2 B3 B4 B5 C D E1 E2 E3 F G > "A" "p" "q" "r" "s" "t" "C" "D" "x" "y" "z" "F" "G" > > Could also have used: > > my.vlist[ names(replist) ] <- replist > > .... which I think illustrates the value of character indexing of lists for > assignment even better. > > The ordering of the elements is important. > > > > Knut Hansen > > David Winsemius > Alameda, CA, USAThank you. This was what I was looking for. I was not aware of named lists in R, and I was about to write a script in Python to fix this. Knut Hansen