[R] Simple table with frequency variable

G. Draisma g.draisma at erasmusmc.nl
Thu Aug 2 15:40:20 CEST 2007


Thanks Johan,

I realize that I did not use the right example.
I have a table with two factors, say b and c,
and a third case weight variable, say w.
Then I would like the table command to sum the weights w
for each combination of i and j.
For instance, with
   b <- rbinom(10,6,0.3)
   c <- rbinom(10,6,0.9)
   w <- trunc(runif(10)*3)
the command
   table(i,j)
counts the occurrences of combinations (b,c).
I am looking for a command like
   table(i,j,weight=w)
that would print the sum of the w's for each combination.
e.g. if the combination (2,5) occurs twice
I would like to see the sum of the two weights
in the table, instead of the count 2.

Gerrit.




on 02-Aug-2007 11:03 Johan Lindbäck said the following:
> Would it be ok with a matrix?
> 
> i <- 1:5; j <- 1:2
> 
> li <- length(i)
> lj <- length(j)
> A <- matrix(numeric(li * lj), nrow = li, dimnames = list(i, j))
> for (r in 1:li)
>   for (s in 1:lj)
>     A[r, s] <- 10*r + s
> A
> 
> HTH
> /Johan
> 
> 
> 
> G. Draisma skrev:
>> Thank you Jim,
>> Sorry, that I was not clear enough.
>> Each case has a frequency variable N.
>> so when tabulating combinations (i,j) they
>> should be weighted with weight N.
>>
>> In this case I would like a command
>> table(i,j,N)
>> resulting in
>>      j
>>   i   1  2
>>     1 11 12
>>     2 21 22
>>     ...
>>     5 51 52
>>
>> And I was looking for a table command
>> that allows for a case weight variable.
>> Gerrit.
>>
>>
>> on 01-Aug-2007 22:38 jim holtman said the following:
>>> I am not exactly sure what you are asking for.  I am assuming that you
>>> want a vector that represent the combinations that are given
>>> combinations that are present:
>>>
>>>> N
>>>  [1] 11 22 31 42 51 12 21 32 41 52
>>>> table(i,j)
>>>    j
>>> i   1 2
>>>   1 1 1
>>>   2 1 1
>>>   3 1 1
>>>   4 1 1
>>>   5 1 1
>>>> z <- table(i,j)
>>>> which(z==1)
>>>  [1]  1  2  3  4  5  6  7  8  9 10
>>>> which(z==1,arr.ind=T)
>>>   row col
>>> 1   1   1
>>> 2   2   1
>>> 3   3   1
>>> 4   4   1
>>> 5   5   1
>>> 1   1   2
>>> 2   2   2
>>> 3   3   2
>>> 4   4   2
>>> 5   5   2
>>>> x <- which(z==1,arr.ind=T)
>>>> paste(rownames(z)[x[,'row']], colnames(z)[x[,'col']], sep='')
>>>  [1] "11" "21" "31" "41" "51" "12" "22" "32" "42" "52"
>>>
>>>
>>> On 8/1/07, G. Draisma <g.draisma at erasmusmc.nl> wrote:
>>>> Hallo,
>>>>
>>>> Im trying to find out how to tabulate frequencies
>>>> of factors when the data have a frequency variable.
>>>>
>>>> e,g:
>>>> i<-rep(1:5,2)
>>>> j<-rep(1:2,5)
>>>> N<-10*i+j
>>>>
>>>> table(i,j) gives a table of ones
>>>> as each combination occurs only once.
>>>> How does one get a table with the corresponding N's?
>>>>
>>>> Thanks!
>>>> Gerrit.
>>>>
>>>>
>>>> --
>>>> Gerrit Draisma
>>>> Department of Public Health
>>>> Erasmus MC, University Medical Center Rotterdam
>>>> Room AE-103
>>>> P.O. Box 2040 3000 CA  Rotterdam The Netherlands
>>>> Phone: +31 10 4087124 Fax: +31 10 4638474
>>>> http://mgzlx4.erasmusmc.nl/pwp/?gdraisma
>>>>
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch mailing list
>>>> 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.
>>>>
> 

-- 
Gerrit Draisma
Department of Public Health
Erasmus MC, University Medical Center Rotterdam
Room AE-103
P.O. Box 2040 3000 CA  Rotterdam The Netherlands
Phone: +31 10 4087124 Fax: +31 10 4638474
http://mgzlx4.erasmusmc.nl/pwp/?gdraisma



More information about the R-help mailing list