and you might want to check ?prop.table

> prop.table(a, 2)
     [,1]      [,2]
[1,]  0.0 0.3333333
[2,]  0.5 0.3333333
[3,]  0.5 0.3333333

or even ?sweep (which will be useful for more complex situations)

> sweep(a, 2, colSums(a), "/")
     [,1]      [,2]
[1,]  0.0 0.3333333
[2,]  0.5 0.3333333
[3,]  0.5 0.3333333

b

On Mar 7, 2008, at 5:32 PM, Alexey Shipunov wrote:

Dear list,

I just made a very simple mistake, but it was hard to spot. And I
think that I should warn other people, because it is probably so
simple to make...

=== R code  ===

# Let us create a matrix:
(a <- cbind(c(0,1,1), rep(1,3)))

#      [,1] [,2]
# [1,]    0    1
# [2,]    1    1
# [3,]    1    1

# That is a MISTAKE:
a/colSums(a)

#           [,1]      [,2]
# [1,] 0.0000000 0.3333333
# [2,] 0.3333333 0.5000000
# [3,] 0.5000000 0.3333333
# I just wonder if some R warning should be issued here?

# That is what I actually needed (column-wise frequencies):
t(t(a)/colSums(a))

#      [,1]      [,2]
# [1,]  0.0 0.3333333
# [2,]  0.5 0.3333333
# [3,]  0.5 0.3333333

=== end of R code ===

With best wishes and regards,
Alexey Shipunov

______________________________________________
R-help@r-project.org 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.

______________________________________________
R-help@r-project.org 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.

Reply via email to