Re: [R] Warning: matrix by vector division

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 07 Mar 2008 17:47:05 -0500

R is working exactly as documented. If you look at how the matrix is stored (column-wise) and then what you are dividing by, you will see that it is doing what you asked (when recycling values):

> as.vector(a)

[1] 0 1 1 1 1 1
> as.vector(a)/c(2,3)

[1] 0.0000000 0.3333333 0.5000000 0.3333333 0.5000000 0.3333333
>

On Fri, Mar 7, 2008 at 5:32 PM, Alexey Shipunov <dactylorhiza_at_gmail.com> 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_at_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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_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.
Received on Fri 07 Mar 2008 - 22:52:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 07 Mar 2008 - 23:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive