Hello, I have a matrix with the numbers 0,1 and 9 I would like to write a function that could sum each line skiping everytime a number 9 appears for example [0 1 0 1 1 9 1] the sum would be 4. However I cannot replace 9 by 0 otherwise after the sum is done I wouldn?t be able to distiguish which ones were real zeros and which ones were nines replaced by zero just to sum. Thank you very much -- View this message in context: http://old.nabble.com/How-to-sum-only-a-few-elements-in-a-line-tp26519740p26519740.html Sent from the R help mailing list archive at Nabble.com.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Marcio Resende > Sent: Wednesday, November 25, 2009 12:27 PM > To: r-help at r-project.org > Subject: [R] How to sum only a few elements in a line > > > Hello, > > I have a matrix with the numbers 0,1 and 9 > I would like to write a function that could sum each line > skiping everytime > a number 9 appears > for example > [0 1 0 1 1 9 1] > the sum would be 4. > However I cannot replace 9 by 0 otherwise after the sum is > done I wouldn?t > be able to distiguish which ones were real zeros and which > ones were nines > replaced by zero just to sum.One of the nice things about the S language is that arguments functions are not altered by the function. When the function appears to alter an argument it is really altering a copy of it. Thus you can write a function like f <- function(matrix) { matrix[matrix==9] <- 0 rowSums(matrix) } and use it as > myMatrix <- rbind(c(0,1,0,1,1,9,1), c(9,9,9,9,9,17,9)) > f(myMatrix) [1] 4 17 > myMatrix # not altered by running f over it [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0 1 0 1 1 9 1 [2,] 9 9 9 9 9 17 9 By the way, it would help if you wrote your example data as an S expression, e.g., rbind(c(...),c(...)), and not as an expression in some other language, "[ 1 1 9 ]". Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> Thank you very much > > -- > View this message in context: > http://old.nabble.com/How-to-sum-only-a-few-elements-in-a-line-tp26519740p26519740.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Tena koe Marcio Try something like (untested) apply(yourMatrix, 1, function(x) sum(x[x!=9])) HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Marcio Resende > Sent: Thursday, 26 November 2009 9:27 a.m. > To: r-help at r-project.org > Subject: [R] How to sum only a few elements in a line > > > Hello, > > I have a matrix with the numbers 0,1 and 9 I would like to > write a function that could sum each line skiping everytime a > number 9 appears for example [0 1 0 1 1 9 1] the sum would be 4. > However I cannot replace 9 by 0 otherwise after the sum is > done I wouldn?t be able to distiguish which ones were real > zeros and which ones were nines replaced by zero just to sum. > Thank you very much > > -- > View this message in context: > http://old.nabble.com/How-to-sum-only-a-few-elements-in-a-line > -tp26519740p26519740.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 Nov 25, 2009, at 3:27 PM, Marcio Resende wrote:> > Hello, > > I have a matrix with the numbers 0,1 and 9 > I would like to write a function that could sum each line skiping > everytime > a number 9 appears > for example > [0 1 0 1 1 9 1]sum(x[x != 9])> the sum would be 4. > However I cannot replace 9 by 0 otherwise after the sum is done I > wouldn?t > be able to distiguish which ones were real zeros and which ones were > nines > replaced by zero just to sum. > Thank you very much > > --David Winsemius, MD Heritage Laboratories West Hartford, CT
Marcio Resende wrote:> Hello, > > I have a matrix with the numbers 0,1 and 9 > I would like to write a function that could sum each line skiping everytime > a number 9 appears > for example > [0 1 0 1 1 9 1] > the sum would be 4. > However I cannot replace 9 by 0 otherwise after the sum is done I wouldn??t > be able to distiguish which ones were real zeros and which ones were nines > replaced by zero just to sum. > Thank you very much >In order to distinguish 'real' 0s from those resulting from elimination of 9s (without going back to the original matrix), you can do this: f <- function(x){ Sum <- sum(x[x != 9]) has_9 <- 9 %in% x c(Sum = Sum, has_9 = has_9) } # make a sample matrix M <- matrix(sample(c(0,1,9), 30, TRUE), 6, 5) # get sums and whether the row contained one or more 9s t(apply(M, 1, f)) -Peter Ehlers
On Nov 25, 2009, at 3:45 PM, William Dunlap wrote:>> -----Original Message----- >> From: r-help-bounces at r-project.org >> [mailto:r-help-bounces at r-project.org] On Behalf Of Marcio Resende >> Sent: Wednesday, November 25, 2009 12:27 PM >> To: r-help at r-project.org >> Subject: [R] How to sum only a few elements in a line >> >> >> Hello, >> >> I have a matrix with the numbers 0,1 and 9 >> I would like to write a function that could sum each line >> skiping everytime >> a number 9 appears >> for example >> [0 1 0 1 1 9 1] >> the sum would be 4. >> However I cannot replace 9 by 0 otherwise after the sum is >> done I wouldn?t >> be able to distiguish which ones were real zeros and which >> ones were nines >> replaced by zero just to sum. > > One of the nice things about the S language is > that arguments functions are not altered by the > function. When the function appears to alter > an argument it is really altering a copy of it. > Thus you can write a function like > f <- function(matrix) { > matrix[matrix==9] <- 0 > rowSums(matrix) > } > and use it as >> myMatrix <- rbind(c(0,1,0,1,1,9,1), c(9,9,9,9,9,17,9)) >> f(myMatrix) > [1] 4 17How about using the element-wise behavior of "*" when working with matrices? > rowSums( myMatrix*(myMatrix != 9) ) [1] 4 17>> myMatrix # not altered by running f over it > [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] 0 1 0 1 1 9 1 > [2,] 9 9 9 9 9 17 9 > > By the way, it would help if you wrote your example data > as an S expression, e.g., rbind(c(...),c(...)), and not > as an expression in some other language, "[ 1 1 9 ]". > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > >> Thank you very much >> >> --David Winsemius, MD Heritage Laboratories West Hartford, CT