[R] Working With Variables Having Different Lengths

David Winsemius dwinsemius at comcast.net
Mon Oct 24 17:57:04 CEST 2011


On Oct 24, 2011, at 11:34 AM, Rich Shepard wrote:

> On Fri, 21 Oct 2011, David Winsemius wrote:
>
>> The first thing I would try would be
>> with(subset(chemdata, param %in% c('TDS', 'Cond', 'Mg', 'SO4',  
>> 'Cl', 'Na', and 'Ca') , 1:4) ,
>>   xtabs(quant ~ site + sampdate + param) )
>
> David,
>
>  Need to remove the 'and' from the above.

Right. The perils of untested code, but then, you never provided any  
data to test did you.

>
>  The results include _all_ params,

I doubt it. I suspect there are all zeroes in the tables for levels  
which were not in that list of subset criteria.

> not just the six above and all sampdates
> from 31 years ago. The first table begins with


The appearance of levels with all zeroes is probably because I didn't  
include  drop.unused.levels = FALSE in the xtabs specification.

>
> , , param = AGP
>
>                sampdate
> site             1981-11-30 1982-04-28 1982-05-24 1982-06-29  
> 1983-10-20
>  BC-0.5              0.000      0.000      0.000      0.000      0.000
>  BC-1                0.000      0.000      0.000      0.000      0.000
>  BC-1.5              0.000      0.000      0.000      0.000      0.000
>  BC-2                0.000      0.000      0.000      0.000      0.000
>  BC-3                0.000      0.000      0.000      0.000      0.000
>
> and 20296 lines later (in emacs) that param ends and so does R's   
> [ reached
> getOption("max.print") -- omitted 31 row(s) and 65 matrix slice(s) ].

It's always better to work with small subsets in developing your code.

>  Why didn't the '%in%' limit the output to the specified params?
>
>  Is the design of expressions such as the above based on your years of
> experience with R or are such topics covered in a document somewhere?

I don't take your meaning here. Using `subset` to limit analyses to a  
particular segment of data it pretty fundamental. `xtabs` is a fairly  
standard operation. You could ahve broken them apart and created a  
working subset first and then used it with xtabs.


> I have
> bought almost a dozen R books in the past few months, have read most  
> of
> them, and don't recall seeing anything like the above.
>
> Thanks,
>
> Rich
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list