r-help-bounces@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
"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-boun...@r-project.org [mailto:r-help-boun...@r-project
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)]))
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
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 t
5 matches
Mail list logo