Re: [R] Calculating a Maximum for a row or column with NA's
Henrik, Thanks! I was just recommending the package to another fellow who is learning R as I am. I was going crazy. Jorge gave me a solution that works, however the data set I'm working with is huge so I'm hoping that switching to your package will give both readability and performance improvements. On Sun, Apr 18, 2010 at 2:47 AM, Henrik Bengtsson wrote: > On Sun, Apr 18, 2010 at 7:26 AM, steven mosher > wrote: > > Ya I got that result but fixing it was a mystery. especially since I will > > eventually want to subtract the row max from the row Min ( or calculate > the > > range) > > if a matrix thus is: > > > > [,1] [,2] [,3] > > [1,] NA NA NA > > [2,]258 > > [3,] NA69 > > > > and apply(m,1,max,na.rm=TRUE) > > > > yeilds > > > > [1] -Inf89 > > > > Then rowmin yeilds > > > > [1] -Inf26 > > > > need to see what happens if I subtract these two vectors. > > > > > > [,1] [,2] [,3] > > [1,] NA NA NA > > [2,]258 > > [3,] NA69 > >> rmax<-apply(m,1,max,na.rm=TRUE) > > Warning message: > > In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf > >> rmax > > [1] -Inf89 > >> rmin<-apply(m,1,min,na.rm=TRUE) > > Warning message: > > In FUN(newX[, i], ...) : no non-missing arguments to min; returning Inf > >> rmin > > [1] Inf 2 6 > >> rmax-rmin > > [1] -Inf63 > >> rrange<-rmax-rmin > >> rrange > > [1] -Inf63 > > > > > > The final maxtrix may have a large number of these -Inf.. > > > > I Was looking at maxtrixStats package but it still beta > > The matrixStats package is labelled "beta", because the author of it > is *extremely* picky when it comes to bumping code up to be labelled > "release"; he often requires a code base to be stable for years before > removing the label "beta". I would give matrixStats' rowMaxs() a try. > > /Henrik > (author of matrixStats) > > > > > > > On Sat, Apr 17, 2010 at 10:01 PM, David Winsemius < > dwinsem...@comcast.net>wrote: > > > >> > >> On Apr 18, 2010, at 12:16 AM, steven mosher wrote: > >> > >> Is there a simple way to calculate the maximum for a row or column of a > >>> matrix when there are NA,s present. > >>> > >>> # given a matrix that has any number of NA per row > >>> > m<-matrix(c(seq(1,9)),nrow=3) > m > > >>>[,1] [,2] [,3] > >>> [1,]147 > >>> [2,]258 > >>> [3,]369 > >>> > m[3,1]=NA > m[1,]=NA > m > > >>>[,1] [,2] [,3] > >>> [1,] NA NA NA > >>> [2,]258 > >>> [3,] NA69 > >>> > >>> # applying max to rows doesnt work as max returns > >>> # NA if any of the elements is NA. > >>> > row_max<-apply(m,1,max) > row_max > > >>> [1] NA 8 NA > >>> > >>> # my desired result given m would be: > >>> # NA, 8, 9 > >>> > >> > >> Not exactly your desired result, but surely you could fix that: > >> > >> > row_max<-apply(m,1,max, na.rm=TRUE) > >> Warning message: > >> In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf > >> > row_max > >> [1] -Inf89 > >> > >> > >> > >> > >>>[[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. > >>> > >> > >> David Winsemius, MD > >> West Hartford, CT > >> > >> > > > >[[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. > > > [[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] Calculating a Maximum for a row or column with NA's
On Sun, Apr 18, 2010 at 7:26 AM, steven mosher wrote: > Ya I got that result but fixing it was a mystery. especially since I will > eventually want to subtract the row max from the row Min ( or calculate the > range) > if a matrix thus is: > > [,1] [,2] [,3] > [1,] NA NA NA > [2,] 2 5 8 > [3,] NA 6 9 > > and apply(m,1,max,na.rm=TRUE) > > yeilds > > [1] -Inf 8 9 > > Then rowmin yeilds > > [1] -Inf 2 6 > > need to see what happens if I subtract these two vectors. > > > [,1] [,2] [,3] > [1,] NA NA NA > [2,] 2 5 8 > [3,] NA 6 9 >> rmax<-apply(m,1,max,na.rm=TRUE) > Warning message: > In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf >> rmax > [1] -Inf 8 9 >> rmin<-apply(m,1,min,na.rm=TRUE) > Warning message: > In FUN(newX[, i], ...) : no non-missing arguments to min; returning Inf >> rmin > [1] Inf 2 6 >> rmax-rmin > [1] -Inf 6 3 >> rrange<-rmax-rmin >> rrange > [1] -Inf 6 3 > > > The final maxtrix may have a large number of these -Inf.. > > I Was looking at maxtrixStats package but it still beta The matrixStats package is labelled "beta", because the author of it is *extremely* picky when it comes to bumping code up to be labelled "release"; he often requires a code base to be stable for years before removing the label "beta". I would give matrixStats' rowMaxs() a try. /Henrik (author of matrixStats) > > > On Sat, Apr 17, 2010 at 10:01 PM, David Winsemius > wrote: > >> >> On Apr 18, 2010, at 12:16 AM, steven mosher wrote: >> >> Is there a simple way to calculate the maximum for a row or column of a >>> matrix when there are NA,s present. >>> >>> # given a matrix that has any number of NA per row >>> m<-matrix(c(seq(1,9)),nrow=3) m >>> [,1] [,2] [,3] >>> [1,] 1 4 7 >>> [2,] 2 5 8 >>> [3,] 3 6 9 >>> m[3,1]=NA m[1,]=NA m >>> [,1] [,2] [,3] >>> [1,] NA NA NA >>> [2,] 2 5 8 >>> [3,] NA 6 9 >>> >>> # applying max to rows doesnt work as max returns >>> # NA if any of the elements is NA. >>> row_max<-apply(m,1,max) row_max >>> [1] NA 8 NA >>> >>> # my desired result given m would be: >>> # NA, 8, 9 >>> >> >> Not exactly your desired result, but surely you could fix that: >> >> > row_max<-apply(m,1,max, na.rm=TRUE) >> Warning message: >> In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf >> > row_max >> [1] -Inf 8 9 >> >> >> >> >>> [[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. >>> >> >> David Winsemius, MD >> West Hartford, CT >> >> > > [[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] Calculating a Maximum for a row or column with NA's
thx jorge, I was playing around with ifelse to solve the problem, but was unaware of the all(is.na(x)). RTFM I guess. On Sat, Apr 17, 2010 at 10:31 PM, Jorge Ivan Velez wrote: > Hi Steven, > > Try this: > > R> apply(m,1, function(x) ifelse(all(is.na(x)), NA, max(x, na.rm = TRUE))) > [1] NA 8 9 > > See ?ifelse, ?all and ?max for more information. > > HTH, > Jorge > > > On Sun, Apr 18, 2010 at 12:16 AM, steven mosher <> wrote: > >> Is there a simple way to calculate the maximum for a row or column of a >> matrix when there are NA,s present. >> >> # given a matrix that has any number of NA per row >> > m<-matrix(c(seq(1,9)),nrow=3) >> > m >> [,1] [,2] [,3] >> [1,]147 >> [2,]258 >> [3,]369 >> > m[3,1]=NA >> > m[1,]=NA >> > m >> [,1] [,2] [,3] >> [1,] NA NA NA >> [2,]258 >> [3,] NA69 >> >> # applying max to rows doesnt work as max returns >> # NA if any of the elements is NA. >> > row_max<-apply(m,1,max) >> > row_max >> [1] NA 8 NA >> >> # my desired result given m would be: >> # NA, 8, 9 >> >>[[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. >> > > [[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] Calculating a Maximum for a row or column with NA's
Hi Steven, Try this: R> apply(m,1, function(x) ifelse(all(is.na(x)), NA, max(x, na.rm = TRUE))) [1] NA 8 9 See ?ifelse, ?all and ?max for more information. HTH, Jorge On Sun, Apr 18, 2010 at 12:16 AM, steven mosher <> wrote: > Is there a simple way to calculate the maximum for a row or column of a > matrix when there are NA,s present. > > # given a matrix that has any number of NA per row > > m<-matrix(c(seq(1,9)),nrow=3) > > m > [,1] [,2] [,3] > [1,]147 > [2,]258 > [3,]369 > > m[3,1]=NA > > m[1,]=NA > > m > [,1] [,2] [,3] > [1,] NA NA NA > [2,]258 > [3,] NA69 > > # applying max to rows doesnt work as max returns > # NA if any of the elements is NA. > > row_max<-apply(m,1,max) > > row_max > [1] NA 8 NA > > # my desired result given m would be: > # NA, 8, 9 > >[[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. > [[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] Calculating a Maximum for a row or column with NA's
Ya I got that result but fixing it was a mystery. especially since I will eventually want to subtract the row max from the row Min ( or calculate the range) if a matrix thus is: [,1] [,2] [,3] [1,] NA NA NA [2,]258 [3,] NA69 and apply(m,1,max,na.rm=TRUE) yeilds [1] -Inf89 Then rowmin yeilds [1] -Inf26 need to see what happens if I subtract these two vectors. [,1] [,2] [,3] [1,] NA NA NA [2,]258 [3,] NA69 > rmax<-apply(m,1,max,na.rm=TRUE) Warning message: In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf > rmax [1] -Inf89 > rmin<-apply(m,1,min,na.rm=TRUE) Warning message: In FUN(newX[, i], ...) : no non-missing arguments to min; returning Inf > rmin [1] Inf 2 6 > rmax-rmin [1] -Inf63 > rrange<-rmax-rmin > rrange [1] -Inf63 The final maxtrix may have a large number of these -Inf.. I Was looking at maxtrixStats package but it still beta On Sat, Apr 17, 2010 at 10:01 PM, David Winsemius wrote: > > On Apr 18, 2010, at 12:16 AM, steven mosher wrote: > > Is there a simple way to calculate the maximum for a row or column of a >> matrix when there are NA,s present. >> >> # given a matrix that has any number of NA per row >> >>> m<-matrix(c(seq(1,9)),nrow=3) >>> m >>> >>[,1] [,2] [,3] >> [1,]147 >> [2,]258 >> [3,]369 >> >>> m[3,1]=NA >>> m[1,]=NA >>> m >>> >>[,1] [,2] [,3] >> [1,] NA NA NA >> [2,]258 >> [3,] NA69 >> >> # applying max to rows doesnt work as max returns >> # NA if any of the elements is NA. >> >>> row_max<-apply(m,1,max) >>> row_max >>> >> [1] NA 8 NA >> >> # my desired result given m would be: >> # NA, 8, 9 >> > > Not exactly your desired result, but surely you could fix that: > > > row_max<-apply(m,1,max, na.rm=TRUE) > Warning message: > In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf > > row_max > [1] -Inf89 > > > > >>[[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. >> > > David Winsemius, MD > West Hartford, CT > > [[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] Calculating a Maximum for a row or column with NA's
On Apr 18, 2010, at 12:16 AM, steven mosher wrote: Is there a simple way to calculate the maximum for a row or column of a matrix when there are NA,s present. # given a matrix that has any number of NA per row m<-matrix(c(seq(1,9)),nrow=3) m [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 m[3,1]=NA m[1,]=NA m [,1] [,2] [,3] [1,] NA NA NA [2,]258 [3,] NA69 # applying max to rows doesnt work as max returns # NA if any of the elements is NA. row_max<-apply(m,1,max) row_max [1] NA 8 NA # my desired result given m would be: # NA, 8, 9 Not exactly your desired result, but surely you could fix that: > row_max<-apply(m,1,max, na.rm=TRUE) Warning message: In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf > row_max [1] -Inf89 [[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. David Winsemius, MD West Hartford, CT __ 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] Calculating a Maximum for a row or column with NA's
Is there a simple way to calculate the maximum for a row or column of a matrix when there are NA,s present. # given a matrix that has any number of NA per row > m<-matrix(c(seq(1,9)),nrow=3) > m [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 > m[3,1]=NA > m[1,]=NA > m [,1] [,2] [,3] [1,] NA NA NA [2,]258 [3,] NA69 # applying max to rows doesnt work as max returns # NA if any of the elements is NA. > row_max<-apply(m,1,max) > row_max [1] NA 8 NA # my desired result given m would be: # NA, 8, 9 [[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.