Hi, I'm new to R, and trying to map stuff I knew how to do in Matlab to R. For instances, in matlab, sum(x) gave the column sums of matrix(/vector) x. In R it just gives the sum of all the elements, regardless of their form (vector or matrix). Is there a simple way to return a vector of column sums for a matrix? Also, in Matlab binary operators exist for concatenating a matrix onto the end or the bottom of another matrix (i.e. horiz or vertically) but no such operators seem to exist for R. is there an easy way to concatenate matrices? (I have found a way which involves several steps, first creating a new matrix of zeros with dimensions based on the dims of the old ones, and then inserting the contents of the old matrices into the new one, but this seems very cumbersome, esp. since I'm used to being able to do it with something like c = [a,b]. Thanks for entertaining my newbie questions dave +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |David Richmond It works on a | + Dept. of Sociology complex scientific + |Saint Mary's College principle, known as | + Notre Dame, IN 46556 "pot luck." + |219-284-4517 - The Doctor | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 13 Sep 2001, David A Richmond wrote:> Hi, > I'm new to R, and trying to map stuff I knew how to do in Matlab > to R. For instances, in matlab, sum(x) gave the column sums of > matrix(/vector) x. In R it just gives the sum of all the elements, > regardless of their form (vector or matrix). Is there a simple way to > return a vector of column sums for a matrix?apply(x,2,sum) gives column sums apply(x,1,sum) gives row sums> Also, in Matlab binary > operators exist for concatenating a matrix onto the end or the bottom of > another matrix (i.e. horiz or vertically) but no such operators seem to > exist for R. is there an easy way to concatenate matrices? (I have found a > way which involves several steps, first creating a new matrix of zeros > with dimensions based on the dims of the old ones, and then inserting the > contents of the old matrices into the new one, but this seems very > cumbersome, esp. since I'm used to being able to do it with something like > c = [a,b]. Thanks for entertaining my newbie questionscbind(x,y) concatenates matrices by column rbind(x,y) concatenates matrices by row -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> I'm new to R, and trying to map stuff I knew how to do in Matlab > to R. For instances, in matlab, sum(x) gave the column sums of > matrix(/vector) x.#column sums margin.table(x,2) # or apply(z,2,sum)> concatenating a matrix onto the end or > the bottom of > another matrix (i.e. horiz or vertically) but no such > operators seem to > exist for R. is there an easy way to concatenate matrices?# horizontally cbind(x1,x2) # vertically rbind(x1,x2) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I am currently converting Matlab code into R code. All basic matrix operations in Matlab exist also in R... You just have to find them because functions names and logic are sometimes a little bit different. You have a few hints with other answers in this thread. R is strongest in indexing matrices. It handles also data.frames, factors, etc... with no direct equivalent in Matlab. You are better to consider you learn a new language instead of trying to use R as a Matlab-like language. If you do so, I guess you will prefer R than Matlab for most applications (like I do!). My R code tend also to be somewhat shorter than Matlab equivalent :-) However, Matlab code (if run in v. 6) is often faster :-( All the best, Philippe Grosjean ...........]<(({?<...............<?}))><............................... ) ) ) ) ) __ __ ( ( ( ( ( |__) | _ ) ) ) ) ) | hilippe |__)rosjean ( ( ( ( ( Marine Biol. Lab., ULB, Belgium ) ) ) ) ) __ ( ( ( ( ( |\ /| |__) ) ) ) ) ) | \/ |ariculture & |__)iostatistics ( ( ( ( ( ) ) ) ) ) e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org ( ( ( ( ( SciViews project coordinator (http://www.sciviews.org) ) ) ) ) ) tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home) ( ( ( ( ( ) ) ) ) ) "I'm 100% confident that p is between 0 and 1" ( ( ( ( ( L. Gonick & W. Smith (1993) ) ) ) ) ) .......................................................................>Hi, > I'm new to R, and trying to map stuff I knew how to do in Matlab >to R. For instances, in matlab, sum(x) gave the column sums of >matrix(/vector) x. In R it just gives the sum of all the elements, >regardless of their form (vector or matrix). Is there a simple way to >return a vector of column sums for a matrix? Also, in Matlab binary >operators exist for concatenating a matrix onto the end or the bottom of >another matrix (i.e. horiz or vertically) but no such operators seem to >exist for R. is there an easy way to concatenate matrices? (I have found a >way which involves several steps, first creating a new matrix of zeros >with dimensions based on the dims of the old ones, and then inserting the >contents of the old matrices into the new one, but this seems very >cumbersome, esp. since I'm used to being able to do it with something like >c = [a,b]. Thanks for entertaining my newbie questions>dave>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ >|David Richmond It works on a | >+ Dept. of Sociology complex scientific + >|Saint Mary's College principle, known as | >+ Notre Dame, IN 46556 "pot luck." + >|219-284-4517 - The Doctor | >+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._