Francesco Napolitano
2009-Dec-24 02:46 UTC
[R] Newbie: colSums() compared with Matlab's sum()
Hi all, I'm trying to learn R after years of Matlab's experience. Here is an issue I couldn't solve today. Consider the following piece of code (written by memory): for(i in 1:n){ submat <- data[1:i,] C <- colSums(submat) } The problem is that at the first iteration, data[1:1,] reduces to a vector and colSums returns an error. This sounds really strange to me because a sum-over-columns operation should have no problem working with columns of length 1. Matlab's "sum()" works just fine in such case. The error says that I need an at least 2D array. So I try using matrix(data[1:i,]). Unfortunately this returns a column instead of a row. So I could do t(matrix(data[1:i,])), but this would transpose the also the sub-matrices from the 2nd iteration on. I could fix everything with some "if" but it would be really terrible code :-/. I'm sure I'm missing something because my neurons stick with Matlab behaviour. Can you help me to understand what's wrong with my reasoning? Thank you very much, Francesco.
Benilton Carvalho
2009-Dec-24 02:58 UTC
[R] Newbie: colSums() compared with Matlab's sum()
replace data[1:i,] by data[1:i,drop=FALSE]. b On Dec 24, 2009, at 12:46 AM, Francesco Napolitano wrote:> Hi all, > > I'm trying to learn R after years of Matlab's experience. Here is an > issue I couldn't solve today. > > Consider the following piece of code (written by memory): > > for(i in 1:n){ > submat <- data[1:i,] > C <- colSums(submat) > } > > The problem is that at the first iteration, data[1:1,] reduces to a > vector and colSums returns an error. This sounds really strange to me > because a sum-over-columns operation should have no problem working with > columns of length 1. Matlab's "sum()" works just fine in such case. > > The error says that I need an at least 2D array. So I try using > matrix(data[1:i,]). Unfortunately this returns a column instead of a > row. So I could do t(matrix(data[1:i,])), but this would transpose the > also the sub-matrices from the 2nd iteration on. I could fix everything > with some "if" but it would be really terrible code :-/. > > I'm sure I'm missing something because my neurons stick with Matlab > behaviour. Can you help me to understand what's wrong with my reasoning? > > Thank you very much, > Francesco. > > ______________________________________________ > 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.
Use submat <- data[1:i,, drop=FALSE] /H On Wed, Dec 23, 2009 at 6:46 PM, Francesco Napolitano <franapoli at gmail.com> wrote:> Hi all, > > I'm trying to learn R after years of Matlab's experience. Here is an > issue I couldn't solve today. > > Consider the following piece of code (written by memory): > > for(i in 1:n){ > ? ?submat <- data[1:i,] > ? ?C <- colSums(submat) > ? ?} > > The problem is that at the first iteration, data[1:1,] reduces to a > vector and colSums returns an error. This sounds really strange to me > because a sum-over-columns operation should have no problem working with > columns of length 1. Matlab's "sum()" works just fine in such case. > > The error says that I need an at least 2D array. So I try using > matrix(data[1:i,]). Unfortunately this returns a column instead of a > row. So I could do t(matrix(data[1:i,])), but this would transpose the > also the sub-matrices from the 2nd iteration on. I could fix everything > with some "if" but it would be really terrible code :-/. > > I'm sure I'm missing something because my neurons stick with Matlab > behaviour. Can you help me to understand what's wrong with my reasoning? > > Thank you very much, > Francesco. > > ______________________________________________ > 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. >
On Dec 23, 2009, at 9:46 PM, Francesco Napolitano wrote:> Hi all, > > I'm trying to learn R after years of Matlab's experience. Here is an > issue I couldn't solve today. > > Consider the following piece of code (written by memory): > > for(i in 1:n){ > submat <- data[1:i,] > C <- colSums(submat) > } >In R the loop is not necessary, even confusing as you are demonstrating: > mat <- matrix(rnorm(100), nrow=10) # 10 x 10 > colSums(mat[1:5, ]) # sums of 1st 5 rows [1] 3.33331735 0.86672248 -3.10971483 1.23620455 -0.31887421 -0.50544837 [7] 3.13636155 0.02175862 -2.18816961 -1.31760196> The problem is that at the first iteration, data[1:1,] reduces to a > vector and colSums returns an error. This sounds really strange to me > because a sum-over-columns operation should have no problem working > with > columns of length 1. Matlab's "sum()" works just fine in such case. > > The error says that I need an at least 2D array. So I try using > matrix(data[1:i,]). Unfortunately this returns a column instead of a > row. So I could do t(matrix(data[1:i,])), but this would transpose the > also the sub-matrices from the 2nd iteration on. I could fix > everything > with some "if" but it would be really terrible code :-/. > > I'm sure I'm missing something because my neurons stick with Matlab > behaviour. Can you help me to understand what's wrong with my > reasoning? > > Thank you very much, > Francesco. > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT