# [R] frequency

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu May 6 20:08:55 CEST 2010

```On 06-May-10 17:06:26, n.vialma at libero.it wrote:
>
> Dear list,
> Im trying to do the following operation but im not able to do it
> This is my table:
>          1 2 3
> 1      0 7 4
> 2      0 2 0
> 3      0 1 3
> 4      0 3 4
>
> what i would like to do is
>
> divide each row values with the corresponding column' sum,namely:
>
>          1           2              3
> 1      0           0.54       0.36
> 2      0           0.15        0
> 3      0           0.08       0.27
> 4      0           0.23       0.36
>

There is a problem with the first columns, because it's sum is 0,
so you would be doing "0/0", with result "NaN". So, for illustration
(and to allow easy verification) I've replaced your first column
with 1,2,3,4.

Suppose your table is a matrix:

M <- matrix(c(1,2,3,4,7,2,1,3,4,0,3,4),ncol=3)
M
#      [,1] [,2] [,3]
# [1,]    1    7    4
# [2,]    2    2    0
# [3,]    3    1    3
# [4,]    4    3    4

Note that the entries are given in the order of moving down
successive columns. That is what a mtrix is: a vector of
numbers, in that order, with in addition a "dim" attribute:

dim(M)
# [1] 4 3

Now you can do the clever bit. Transpose the matrix M:
tM <- t(M)
tM
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    3    4
# [2,]    7    2    1    3
# [3,]    4    0    3    4

This is a new matrix whose elements will again be read in the
order of going down succesive columns, i.e. as
c(1,7,4,2,2,0,3,1,3,4,3,3)

Now compute the column sums of tM:

Csums <- colSums(tM)
Csums
# [1] 10 13 11

Now, in the expression "tM/Csums", the successive elements of tM,
in the above order, will be divided by the successive elements
of Csums, with Csums being recycled until it is finished:

tM/Csums
#           [,1]      [,2]       [,3]      [,4]
# [1,] 0.1000000 0.2000000 0.30000000 0.4000000
# [2,] 0.5384615 0.1538462 0.07692308 0.2307692
# [3,] 0.3636364 0.0000000 0.27272727 0.3636364

But this is the transpose of what you want, so transpose it back
to t(tM/Rsums).

So, just as a full check:

M
#      [,1] [,2] [,3]
# [1,]    1    7    4
# [2,]    2    2    0
# [3,]    3    1    3
# [4,]    4    3    4

Csums <- colSums(M)
Csums
# [1] 10 13 11

t(tM/Csums)
#      [,1]       [,2]      [,3]
# [1,]  0.1 0.53846154 0.3636364
# [2,]  0.2 0.15384615 0.0000000
# [3,]  0.3 0.07692308 0.2727273
# [4,]  0.4 0.23076923 0.3636364

Explicitly: the division of t(M)

= #      [,1] [,2] [,3] [,4]
# [1,]    1    2    3    4
# [2,]    7    2    1    3
# [3,]    4    0    3    4

by Csums=c(10,13,11) has been done as

1/10,7/13,4/11, 2/10,2/13,0/11, 3/10,1/13,3/13, 4/10,3/13,4/11

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 06-May-10                                       Time: 19:08:51
------------------------------ XFMail ------------------------------

```