Dear R People: Suppose I have the following two character vectors: xf [1] "W" NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA> xg[1] NA "k" "h" NA "g" "r" "j" NA "v" "d" NA "v" NA "z" "r" "r" "i">I want to end up with "W" "k" "h" ... What is the best way to achieve this, please? I was thinking that if there is an exclusive "or" that it might work. I've tried all kinds of ifs, and ifelse, to no avail. Thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
Hi, You did not specify what assumptions you can make about xf and xg (such as will they have identical lengths and is it possible both could contain nonmissing values in the same element?), but this seems a straightforward approach in your little example: index <- is.na(xf) xf[index] <- xg[index] HTH, Josh On Thu, Jun 16, 2011 at 6:59 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> Dear R People: > > Suppose I have the following two character vectors: > > ?xf > ?[1] "W" NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA ?NA >> xg > ?[1] NA ?"k" "h" NA ?"g" "r" "j" NA ?"v" "d" NA ?"v" NA ?"z" "r" "r" "i" >> > > I want to end up with > > "W" "k" "h" ... > > What is the best way to achieve this, please? ?I was thinking that if > there is an exclusive "or" that it might work. ?I've tried all kinds > of ifs, and ifelse, to no avail. > > > Thanks, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
I made different assumptions than Josh. xf<-c("W",NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA) xg<-c(NA,"k","h",NA,"g","r","j",NA,"v","d",NA,"v",NA,"z","r","r","i") xf xg unlist(apply(cbind(xf,xg), 1, function(x) x[!is.na(x)])) as.vector(unlist(apply(cbind(xf,xg), 1, function(x) x[!is.na(x)])))> unlist(apply(cbind(xf,xg), 1, function(x) x[!is.na(x)]))xf xg xg xg xg xg xg xg xg xg xg xg xg "W" "k" "h" "g" "r" "j" "v" "d" "v" "z" "r" "r" "i"> as.vector(unlist(apply(cbind(xf,xg), 1, function(x) x[!is.na(x)])))[1] "W" "k" "h" "g" "r" "j" "v" "d" "v" "z" "r" "r" "i">On Thu, Jun 16, 2011 at 9:59 PM, Erin Hodgess <erinm.hodgess@gmail.com>wrote:> Dear R People: > > Suppose I have the following two character vectors: > > xf > [1] "W" NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA > > xg > [1] NA "k" "h" NA "g" "r" "j" NA "v" "d" NA "v" NA "z" "r" "r" "i" > > > > I want to end up with > > "W" "k" "h" ... > > What is the best way to achieve this, please? I was thinking that if > there is an exclusive "or" that it might work. I've tried all kinds > of ifs, and ifelse, to no avail. > > > Thanks, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess@gmail.com > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
You don't say what happens if both arrays have non-missing entries, but assuming that doesn't happen:> ifelse(is.na(xf),xg,xf)[1] "W" "k" "h" NA "g" "r" "j" NA "v" "d" NA "v" NA "z" "r" "r" "i" ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Erin Hodgess Sent: Thursday, June 16, 2011 8:59 PM To: R help Subject: [R] combining strings Dear R People: Suppose I have the following two character vectors: xf [1] "W" NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA> xg[1] NA "k" "h" NA "g" "r" "j" NA "v" "d" NA "v" NA "z" "r" "r" "i">I want to end up with "W" "k" "h" ... What is the best way to achieve this, please? I was thinking that if there is an exclusive "or" that it might work. I've tried all kinds of ifs, and ifelse, to no avail. Thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
What do you want to happen when both are NA? what do you want to happen if both have values? -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Erin Hodgess > Sent: Thursday, June 16, 2011 7:59 PM > To: R help > Subject: [R] combining strings > > Dear R People: > > Suppose I have the following two character vectors: > > xf > [1] "W" NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA > > xg > [1] NA "k" "h" NA "g" "r" "j" NA "v" "d" NA "v" NA "z" "r" "r" > "i" > > > > I want to end up with > > "W" "k" "h" ... > > What is the best way to achieve this, please? I was thinking that if > there is an exclusive "or" that it might work. I've tried all kinds > of ifs, and ifelse, to no avail. > > > Thanks, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.