[R] Crosstabulation with a frequency variable

peter dalgaard pdalgd at gmail.com
Thu Aug 13 17:18:35 CEST 2015


> On 13 Aug 2015, at 16:24 , Bert Gunter <bgunter.4567 at gmail.com> wrote:
> 
> Well, just using base R, ...
> 
>> with(mydata,tapply(freq,list(var1,var2),I))
>   0  1
> 0 11 12
> 1 13 14

If you insist on avoiding the stats package...

However, I'd use sum() rather than I() to get an xtabs() workalike.

-pd

> 
> 
> Cheers,
> Bert
> 
> 
> Bert Gunter
> 
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
>   -- Clifford Stoll
> 
> 
> On Thu, Aug 13, 2015 at 6:39 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>> Hi,
>> 
>> There are lots of ways to do it in base R, but a long time ago I got
>> frustrated and wrote a crosstab function that did exactly what I
>> wanted:
>> 
>> library(ecodist)
>> mydata <- data.frame(var1=c(0,0,1,1),var2=c(0,1,0,1),freq=c(11,12,13,14))
>> crosstab(var1, var2, freq, data=mydata)
>> 
>>   0  1
>> 0 11 12
>> 1 13 14
>> 
>> Sarah
>> 
>> On Thu, Aug 13, 2015 at 5:30 AM, Dean1 <web13site at yahoo.co.uk> wrote:
>>> Hi all,
>>> 
>>> I've had a few years experience with R, which is why this is so frustrating,
>>> my problem seems so simple but I can't find a solution.
>>> 
>>> I have a data frame in the following form:
>>> 
>>> data.frame(var1=c(0,0,1,1),var2=c(0,1,0,1),freq=c(11,12,13,14))
>>> 
>>> How do I create a crosstab with frequencies?
>>>     0    1
>>> 0: 11  12
>>> 1: 13  14
>>> 
>>> 
>> 
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list