[R] how to combine several subsets?

David Winsemius dwinsemius at comcast.net
Tue Sep 7 03:49:29 CEST 2010


On Sep 6, 2010, at 9:22 PM, tooblue wrote:

>
> I simply put,
>> NEVER=subset(infants$bwt,ISNO1)
>> UNTILPREGNANT=subset(infants$bwt, ISNO2)
>> ONCENOTNOW=subset(infants$bwt, ISNO3)
>
> and I wanna combine those three.
> I do it like
> ISNO=NEVER&UNTILPREGNANT&ONCENOTNOW

The "&" operator does not do concatenation, but rather returns a  
logical vector. You may have had your mind adversely affected by  
excessive exposure to Excel.

Hopefully you did not do just that at the command line. I could  
imagine thinking that might work as part of the subset argument to  
subset, but it would be through the use of the or operator, "|":

ALL <- subset(infants$bwt, ISNO1| ISNO2| ISNO3)

If they were dataframes, you could also have done:

ALL <-  rbind(NEVER, UNTILPREGNANT, ONCENOTNOW)

But below you suggested they might be vectors; if so, why not:

ALL <-  c(NEVER, UNTILPREGNANT, ONCENOTNOW)

>
> and R tells me
> 1: In NEVER & UNTILPREGNANT :
>  longer object length is not a multiple of shorter object length
> 2: In NEVER & UNTILPREGNANT & ONCENOTNOW :
>  longer object length is not a multiple of shorter object length
>
> I'm confused coz these are not objects, but a list of sets of numbers.

They really _must_ be objects since you assigned a result to those  
names.

(Greater clarity would occur if you offered at least str(NEVER)

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list