|
|
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
thanks for your attention
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
On 06-May-10 17:06:26, [hidden email] 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
>
> thanks for your attention
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) < [hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 06-May-10 Time: 19:08:51
------------------------------ XFMail ------------------------------
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
For completeness.
On Thu, May 6, 2010 at 8:03 PM, Joris Meys < [hidden email]> wrote:
> Table <- matrix(ncol=3,nrow=4,c(0,0,0,0,7,2,1,3,4,0,3,4))
>
> # one way
> t(t(Table)/colSums(Table))
>
> # another way
> apply(Table,2,function(x){x/sum(x)})
>
> Take in mind that your solution is wrong. If you divide 0 by 0, you get
> NaN. If you want this not to happen, you'll have to add an extra control.
>
> On Thu, May 6, 2010 at 7:06 PM, [hidden email] < [hidden email]>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
>>
>>
>> thanks for your attention
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
>
> --
> Joris Meys
> Statistical Consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> Coupure Links 653
> B-9000 Gent
>
> tel : +32 9 264 59 87
> [hidden email]
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php>
--
Joris Meys
Statistical Consultant
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control
Coupure Links 653
B-9000 Gent
tel : +32 9 264 59 87
[hidden email]
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
m = matrix(c(0,7,4,0,2,0,0,1,3,0,3,4),byrow = TRUE,ncol=3)
colSum = apply( m, 2, sum )
#Need to deal with dividing by zero...
m%*%diag(1/colSum)
-----Original Message-----
From: [hidden email] [mailto: [hidden email]] On
Behalf Of Ted Harding
Sent: Thursday, May 06, 2010 1:09 PM
To: [hidden email]
Cc: [hidden email]
Subject: Re: [R] frequency
On 06-May-10 17:06:26, [hidden email] 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
>
> thanks for your attention
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) < [hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 06-May-10 Time: 19:08:51
------------------------------ XFMail ------------------------------
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Hi:
There's always prop.table:
I read in your data as a data frame d. Since prop.table() expects a
matrix/array
as input,
> prop.table(as.matrix(d), 2)
V2 V3 V4
[1,] NaN 0.53846154 0.3636364
[2,] NaN 0.15384615 0.0000000
[3,] NaN 0.07692308 0.2727273
[4,] NaN 0.23076923 0.3636364
where the second argument indicates that you want column proportions.
You can always use round(prop.table(as.matrix(d), 2), 2) to simplify the
output
to two decimal places. (As you can see, Dr. Harding's comment about zero
columns is well placed...)
HTH,
Dennis
On Thu, May 6, 2010 at 10:06 AM, [hidden email] < [hidden email]>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
>
>
> thanks for your attention
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|