[R] function for rowMedian?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Jan 22 12:44:29 CET 2006
On Sun, 22 Jan 2006, Philippe Grosjean wrote:
> Prof Brian Ripley wrote:
>> Interchange 1 and 2 (see ?apply: Andy was answering the subject line, not
>> that in the body).
>>
>> However, Max asked also for column medians of a data frame, for which use
>> sapply(DF, median)
>
> By the way, what is the preferred code for column-wise calculation on *all
> numeric* data in a data frame?
>> sapply(DF, median)
> which considers the list and then simplifies the results down to a vector or
> matrix,... or
>> apply(DF, 2, median)
> which first convert into a matrix and then do the calculation on it?
The first. apply(DF,2, ...) converts to a matrix, extracts each column
and applies FUN to each (in a for loop) to form a list answer and then
simplifies.
I did once try rewriting apply to call lapply(split()), but that was no
faster (at the time) and used more memory.
> Best,
>
> Philippe Grosjean
>
>
>> I didn't understand the question, and it seems I was not alone :)
>>
>> On Fri, 20 Jan 2006, Philippe Grosjean wrote:
>>
>>
>>> Note that there is a confusion here: 1st dimension is row, 2nd dimension
>>> is column for matrix & data.frame.
>>
>>
>> And the dim arg to apply is the one you want the answer to have.
>>
>> A <- matrix(runif(6), 2, 3)
>> apply(A, 1, median) # row medians
>> apply(A, 2, median) # col medians
>>
>>
>>
>>> So, if the question is about "rowMedian", you have:
>>>
>>>
>>>> rowMedian <- function(x, na.rm = FALSE)
>>>> apply(x, 2, median, na.rm = na.rm)
>>>
>>> Now, you ask for the "median for specified columns", which should be as
>>> Andy proposes you, or, if you really want a colMedian function:
>>>
>>>
>>>> colMedian <- function(x, na.rm = FALSE)
>>>> apply(x, 1, median, na.rm = na.rm)
>>>
>>> Best,
>>>
>>> Philippe Grosjean
>>>
>>> Liaw, Andy wrote:
>>>
>>>> apply(x, 1, median) should do it. If not, you need to explain why.
>>>>
>>>> Andy
>>>>
>>>> -----Original Message-----
>>>> From: Max Kauer
>>>> Sent: Friday, January 20, 2006 10:24 AM
>>>> To: r-help at stat.math.ethz.ch
>>>> Subject: [R] function for rowMedian?
>>>>
>>>>
>>>> Hi
>>>> is anybody aware of a function to calculate a the median for specified
>>>> columns in a dataframe or matrix - so analogous to rowMeans?
>>>> Thanks
>>>> Max
>>
>>
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list