Re: [R] matrix logic

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Wed 11 Jan 2006 - 18:47:01 EST

Tom wrote:

> On Tue, 10 Jan 2006 20:25:23 -0500, r user <ruser2006@yahoo.com> wrote:
> 
> 

>>I have 2 dataframes, each with 5 columns and 20 rows.
>>They are called data1 and data2.I wish to create a
>>third dataframe called data3, also with 5 columns and
>>20 rows.
>>
>>I want data3 to contains the values in data1 when the
>>value in data1 is not NA. Otherwise it should contain
>>the values in data2.
>>
>>I have tried afew methids, but they do not seem to
>>work as intended.:
>>
>>data3<-ifelse(is.na(data1)=F,data1,data2)
>>
>>and
>>
>>data3[,]<-ifelse(is.na(data1[,])=F,data1[,],data2[,])
>>
>>Please suggest the “best” way.

"Better" way is to have the Syntax correct:

data3 <- ifelse(is.na(data1), data2, data1)

Please check the archives for almost millions of posts asking more or less this question...!

> Not sure about the bast but...
> 
> a<-c(1,2,3,NA,5)
> b<-c(4,4,4,4,4)
> 
> c<-a
> c[which(is.na(a))]<-b[which(is.na(a))]

Why do you want to know which()?

  na <- is.na(a)
  c[na] <- b[na]

Uwe Ligges

> 
> 
> 
> 

>>______________________________________________
>>R-help@stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide!
>>http://www.R-project.org/posting-guide.html
>>
> 
> 
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help

> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jan 11 19:30:48 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:59 EST