[R] Crosstabulation with a frequency variable

Marc Schwartz marc_schwartz at me.com
Thu Aug 13 16:15:33 CEST 2015


Hi,

As Sarah noted, there are a variety of ways in R to accomplish this, such as:

DF <- data.frame(var1 = c(0, 0, 1, 1), var2 = c(0, 1, 0, 1), freq = c(11, 12, 13, 14))

> xtabs(freq ~ var1 + var2, data = DF)
    var2
var1  0  1
   0 11 12
   1 13 14

See ?xtabs 

Regards,

Marc Schwartz



> On Aug 13, 2015, at 8: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



More information about the R-help mailing list