# [R] if, apply, ifelse

Bert Gunter gunter.berton at gene.com
Thu Nov 28 15:45:43 CET 2013

```Jim, et. al:

rowSums(a, na.rm=TRUE) ## Fast!

tells you whether you have 0, 1, or >= 1 TRUE in each row.
This can then be combined with the ifelse() conditions to get what the
OP seems to want. As you said, it's clunky, and is just a minor
simplification. But, then again, her logic seemed somewhat confusing.

Cheers,
Bert

On Thu, Nov 28, 2013 at 1:10 AM, Jim Lemon <jim at bitwrit.com.au> wrote:
> On 11/28/2013 04:33 AM, Andrea Lamont wrote:
>>
>> Hello:
>>
>> This seems like an obvious question, but I am having trouble answering it.
>> I am new to R, so I apologize if its too simple to be posting. I have
>> searched for solutions to no avail.
>>
>> I have data that I am trying to set up for further analysis ("training
>> data"). What I need is 12 groups based on patterns of 4 variables. The
>> complication comes in when missing data is present. Let  me describe with
>> an example - focusing on just 3 of the 12 groups:
>> ...
>> Any ideas on how to approach this efficiently?
>>
> Hi Andrea,
> I would first convert the matrix "a" to a data frame:
>
> a1<-as.data.frame(a)
>
> Then I would start adding columns:
>
> # group 1 is a 1 (logical TRUE) in col1 and at least one other 1
> # here NAs are converted to zeros
> a1\$group1<-a1\$col1 & (ifelse(is.na(a1\$col2),0,a1\$col2) |
>  ifelse(is.na(a1\$col3),0,a1\$col3) |
>  ifelse(is.na(a1\$col4),0,a1\$col4))
> # group 2 is a 1 in col1 and no other 1s
> # here NAs are converted to 1s
> a1\$group2<-a1\$col1 & !(ifelse(is.na(a1\$col2),1,a1\$col2) |
>  ifelse(is.na(a1\$col3),1,a1\$col3) |
>  ifelse(is.na(a1\$col4),1,a1\$col4))
> # here NAs are converted to 1s
> a1\$group3<-!ifelse(is.na(a1\$col1),1,a1\$col1)
>
> and so on. It is clunky, but then you've got a clunky problem.
>
> Jim
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--

Bert Gunter
Genentech Nonclinical Biostatistics

(650) 467-7374

```