[R] using pmax in presence of NAs

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Wed Nov 16 22:36:58 CET 2016


Thank you, Peter!

On Wed, Nov 16, 2016 at 4:21 PM, peter dalgaard <pdalgd at gmail.com> wrote:
>
>> On 16 Nov 2016, at 21:58 , Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:
>>
>> Hello!
>>
>> I need to calculate the maximum of each row of a data frame.
>> This works:
>>
>>  x <- data.frame(a = 1:5, b=11:15, c=111:115)
>>  x
>>  do.call(pmax, x)
>> [1] 111 112 113 114 115
>>
>> However, how should I modify it if my data frame has NAs?
>> I'd like it to ignore NAs and return the maximum of all non-NAs in each row:
>>
>>  x <- data.frame(a = c(1:5), b=11:15, c=c(111:114,NA))
>>  x
>> I'd like it to return:
>> [1] 111 112 113 114 15
>>
>> Thanks a lot!
>
> The first thing to notice is that pmax allows na.rm=TRUE, so it works fine to do
>
>> pmax(a = c(1:5), b=11:15, c=c(111:114,NA), na.rm=TRUE)
> [1] 111 112 113 114  15
>
> I.e., it is your desire to use do.call() that is the challenge. The 2nd argument should be a list containing the arguments as in the above call, so this works too
>
>> do.call(pmax, list(a = c(1:5), b=11:15, c=c(111:114,NA), na.rm=TRUE))
> [1] 111 112 113 114  15
>
> The form do.call(pmax,x) uses the fact that a data.frame is a kind of list. What you need to do is to tack on the element na.rm=TRUE. This works
>
>> do.call(pmax, c(x, list(na.rm=TRUE)))
> [1] 111 112 113 114  15
>
> and it even works without the list()
>
>> do.call(pmax, c(x, na.rm=TRUE))
> [1] 111 112 113 114  15
>
> -pd
>
>
>>
>> --
>> Dimitri Liakhovitski
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>
>
>
>
>
>
>
>
>



-- 
Dimitri Liakhovitski



More information about the R-help mailing list