[R] apply on rows and columns?
I have the following scenario: m - matrix(1:4, ncol=2) m [,1] [,2] [1,]13 [2,]24 apply(m, 2, sum) [1] 3 7 apply(m, 1, sum) [1] 4 6 So I can apply to rows *or* columns. According to the documentation (?apply) MARGIN a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names. But I get the following results: apply(m, c(1,2), sum) [,1] [,2] [1,]13 [2,]24 How am I to interpret this result? Thank you. Kevin [[alternative HTML version deleted]] __ 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.
Re: [R] apply on rows and columns?
It's the same as what you began with -- and that's because you broke it down by columns and rows and took the sum of everything that resulted. I.e., sum(m[1,1]) sum(m[2,1]) sum(m[1,2]) sum(m[2,2]) and put them back together. Michael On Wed, Nov 16, 2011 at 3:13 PM, rkevinbur...@charter.net wrote: I have the following scenario: m - matrix(1:4, ncol=2) m [,1] [,2] [1,] 1 3 [2,] 2 4 apply(m, 2, sum) [1] 3 7 apply(m, 1, sum) [1] 4 6 So I can apply to rows *or* columns. According to the documentation (?apply) MARGIN a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names. But I get the following results: apply(m, c(1,2), sum) [,1] [,2] [1,] 1 3 [2,] 2 4 How am I to interpret this result? Thank you. Kevin [[alternative HTML version deleted]] __ 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.
Re: [R] apply on rows and columns?
Hi, On Wed, Nov 16, 2011 at 3:13 PM, rkevinbur...@charter.net wrote: I have the following scenario: m - matrix(1:4, ncol=2) m [,1] [,2] [1,] 1 3 [2,] 2 4 apply(m, 2, sum) [1] 3 7 apply(m, 1, sum) [1] 4 6 So I can apply to rows *or* columns. According to the documentation (?apply) MARGIN a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names. But I get the following results: apply(m, c(1,2), sum) [,1] [,2] [1,] 1 3 [2,] 2 4 How am I to interpret this result? I'm pretty sure R is taking the sum of m[1,1] and putting it [1,1], and the sum of m[1,2] and putting it in [1,2] and so on. You instructed apply() to work on rows and columns *simultaneously*, rather than sequentially. apply() on c(1,2) is useful if you have a matrix that's three-dimensional, but not so much if it's two dimensional. What are you trying to accomplish? Sarah -- Sarah Goslee http://www.functionaldiversity.org __ 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.
Re: [R] apply on rows and columns?
To expand on what Sarah and Michael said: if you have a 3d array: x-array(1:4,c(2,2,4)) x , , 1 [,1] [,2] [1,]13 [2,]24 , , 2 [,1] [,2] [1,]13 [2,]24 , , 3 [,1] [,2] [1,]13 [2,]24 , , 4 [,1] [,2] [1,]13 [2,]24 apply(x,c(1,2),sum) [,1] [,2] [1,]4 12 [2,]8 16 a margin of c(1,2) makes more sense. Hope that clarifies things. Justin On Wed, Nov 16, 2011 at 12:18 PM, Sarah Goslee sarah.gos...@gmail.com wrote: Hi, On Wed, Nov 16, 2011 at 3:13 PM, rkevinbur...@charter.net wrote: I have the following scenario: m - matrix(1:4, ncol=2) m [,1] [,2] [1,] 1 3 [2,] 2 4 apply(m, 2, sum) [1] 3 7 apply(m, 1, sum) [1] 4 6 So I can apply to rows *or* columns. According to the documentation (?apply) MARGIN a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names. But I get the following results: apply(m, c(1,2), sum) [,1] [,2] [1,] 1 3 [2,] 2 4 How am I to interpret this result? I'm pretty sure R is taking the sum of m[1,1] and putting it [1,1], and the sum of m[1,2] and putting it in [1,2] and so on. You instructed apply() to work on rows and columns *simultaneously*, rather than sequentially. apply() on c(1,2) is useful if you have a matrix that's three-dimensional, but not so much if it's two dimensional. What are you trying to accomplish? Sarah -- Sarah Goslee http://www.functionaldiversity.org __ 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.