# [R] matrix logic

Thu Jan 12 07:05:26 CET 2006

```I don't know how to keep factors' levels with :

data.frame(mapply(function(x,y,z) ifelse(is.na(y), z, y),
names(D), D, D2, SIMPLIFY=FALSE))

but in that way it's ok :

data.frame(mapply(function(z,x,y) { y[is.na(y)] <- x[is.na(y)] ; y },
names(D), D, D2, SIMPLIFY=F))

(?)

Uwe Ligges a écrit :

> t c wrote:
>
>> Uwe,
>>   FYI:
>>      I tried: "data3 <- ifelse(is.na(data1), data2, data1)"
>>      It seems to me that data3 is an array of length 100.
>>      I do NOT end up with a dataset of 5 columns and 20 rows.
>
>
> I have not read carefully enough, for a data.frame you can generalize
> the approach as follows:
>
>   data.frame(mapply(function(x,y,z) ifelse(is.na(y), z, y),
>              names(D), D, D2, SIMPLIFY=FALSE))
>
> Uwe Ligges
>
>
>
>> Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
>>   Tom wrote:
>>
>>> On Tue, 10 Jan 2006 20:25:23 -0500, r user 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 at stat.math.ethz.ch mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> http://www.R-project.org/posting-guide.html
>>>>
>>>
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>
>>
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>>
>>
>> ---------------------------------
>> Yahoo! Photos – Showcase holiday pictures in hardcover
>>  Photo Books. You design it and we’ll bind it!
>
>
>------------------------------------------------------------------------
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help