Re: [R] Take the maximum of every 12 columns

2018-02-21 Thread Miluji Sb
Dear Henrik, This is great - thank you so much! Sincerely, Milu On Tue, Feb 20, 2018 at 10:12 PM, Henrik Bengtsson < henrik.bengts...@gmail.com> wrote: > It looks like OP uses a data.frame, so in order to use matrixStats > (I'm the author) one would have to pay the price to coerce to a matrix

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Henrik Bengtsson
It looks like OP uses a data.frame, so in order to use matrixStats (I'm the author) one would have to pay the price to coerce to a matrix before using matrixStats::rowMaxs(). However, if it is that the original data could equally well live in a matrix, then matrixStats should be computational effi

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Ista Zahn
On Tue, Feb 20, 2018 at 11:58 AM, Bert Gunter wrote: > Ista, et. al: efficiency? > (Note: I needed to correct my previous post: do.call() is required for > pmax() over the data frame) > > > x <- data.frame(matrix(runif(12e6), ncol=12)) > > > system.time(r1 <- do.call(pmax,x)) >user system el

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Bert Gunter
Sorry, previous code should be: > x <- data.frame(matrix(runif(12e6), ncol=12)) > system.time(r1 <- do.call(pmax,x)) user system elapsed 0.049 0.000 0.049 > system.time(r2 <- apply(x,1,max)) user system elapsed 2.162 0.045 2.207 > identical(r1,r2) [1] TRUE Bert Gunter "The

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Bert Gunter
Ista, et. al: efficiency? (Note: I needed to correct my previous post: do.call() is required for pmax() over the data frame) > x <- data.frame(matrix(runif(12e6), ncol=12)) > system.time(r1 <- do.call(pmax,x)) user system elapsed 0.049 0.000 0.049 > identical(r1,r2) [1] FALSE > system.

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Miluji Sb
This is what I was looking for. Thank you everyone! Sincerely, Milu Mail priva di virus. www.avast.com

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Bert Gunter
Do you want the max of all the data in all 12 columns -- 1 number -- or the parallel max of the 12 columns -- a vector of length the number of rows. In the first case, max(df[,1:12]) will do (R uses 1-based indexing,not 0-based) ; in the second case, pmax(df[, 1:12]) will do. If either of these

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Ista Zahn
Hi Milu, byapply(df, 12, function(x) apply(x, 1, max)) You might also be interested in the matrixStats package. Best, Ista On Tue, Feb 20, 2018 at 9:55 AM, Miluji Sb wrote: > Dear all, > > I have monthly data in wide format, I am only providing data (at the bottom > of the email) for the firs

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Miluji Sb
Thank you for your kind replies. Maybe I was not clear with my question (I apologize) or I did not understand... I would like to take the max for X0...X11 and X12...X24 in my dataset. When I use pmax with the function byapply as in byapply(df, 12, pmax) I get back a list which I cannot convert t

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Bert Gunter
Don't do this (sorry Thierry)! max() already does this -- see ?max > x <- data.frame(a =rnorm(10), b = rnorm(10)) > max(x) [1] 1.799644 > max(sapply(x,max)) [1] 1.799644 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into i

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Thierry Onkelinx
The maximum over twelve columns is the maximum of the twelve maxima of each of the columns. single_col_max <- apply(x, 2, max) twelve_col_max <- apply( matrix(single_col_max, nrow = 12), 2, max ) ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INST

Re: [R] Take the maximum of every 12 columns

2018-02-20 Thread Bert Gunter
?pmax Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Feb 20, 2018 at 6:55 AM, Miluji Sb wrote: > Dear all, > > I have monthly data in wide

[R] Take the maximum of every 12 columns

2018-02-20 Thread Miluji Sb
Dear all, I have monthly data in wide format, I am only providing data (at the bottom of the email) for the first 24 columns but I have 2880 columns in total. I would like to take max of every 12 columns. I have taken the mean of every 12 columns with the following code: byapply <- function(x,