[R] Calculating a Maximum for a row or column with NA's
Henrik Bengtsson
hb at stat.berkeley.edu
Sun Apr 18 11:47:52 CEST 2010
On Sun, Apr 18, 2010 at 7:26 AM, steven mosher <moshersteven at gmail.com> 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 <dwinsemius at 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,] 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 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
>> West Hartford, CT
>>
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list