# [R] Simple table with frequency variable

Johan Lindbäck johan.lindback at ucr.uu.se
Thu Aug 2 11:03:35 CEST 2007

```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
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>

--
O    |>  Johan Lindbäck,  Biostatistician
/\    |   Uppsala Clinical Research Center
| \_.     SE-751 85  UPPSALA
~~~~~~~~~~ <http://www.ucr.uu.se> ----------

```