[R] sapply and median, possible or not ?
Hello, I have a list of data.frame rowsplit : List of 15 $ (0,0.025] :'data.frame': 169 obs. of 7 variables: $ (0.025,0.05]:'data.frame': 174 obs. of 7 variables: $ (0.05,0.075]:'data.frame': 92 obs. of 7 variables: $ (0.075,0.1] :'data.frame': 76 obs. of 7 variables: $ (0.1,0.125] :'data.frame': 37 obs. of 7 variables: $ (0.125,0.15]:'data.frame': 32 obs. of 7 variables: $ (0.15,0.175]:'data.frame': 45 obs. of 7 variables: $ (0.175,0.2] :'data.frame': 56 obs. of 7 variables: $ (0.2,0.225] :'data.frame': 36 obs. of 7 variables: $ (0.225,0.25]:'data.frame': 47 obs. of 7 variables: $ (0.25,0.275]:'data.frame': 34 obs. of 7 variables: $ (0.275,0.3] :'data.frame': 43 obs. of 7 variables: $ (0.3,0.325] :'data.frame': 29 obs. of 7 variables: $ (0.325,0.35]:'data.frame': 29 obs. of 7 variables: $ (0.35,0.375]:'data.frame': 17 obs. of 7 variables: And I would like to get a data.frame gathering the median value of each variable for each intervall. as.data.frame(t(sapply(rowsplit, mean)) works well but I would prefer to use median but with sapply(rowsplit, median), I get the following message "Error in median.default(X[[1L]], ...) : need numeric data". Any idea ? Thanks in advance for your help, Have a nice week-end, Ptit Bleu. PS :I'm under XP and use R2.7.2. -- View this message in context: http://www.nabble.com/sapply-and-median%2C-possible-or-not---tp20378222p20378222.html Sent from the R help mailing list archive at Nabble.com. __ 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] sapply and median, possible or not ?
Unfortunately, I have the same error message. lapply(rowsplit, function(x)mean(x[,sapply(x, is.numeric)])) works but not with median. Strange, isn't it? Any other idea? Thanks in advance, Ptit Bleu. Henrique Dallazuanna wrote: > > Try this: > > lapply(l, function(x)median(x[,sapply(x, is.numeric)])) > > > -- > Henrique Dallazuanna > Curitiba-Paraná-Brasil > 25° 25' 40" S 49° 16' 22" O > > [[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. > > -- View this message in context: http://www.nabble.com/sapply-and-median%2C-possible-or-not---tp20378222p20378663.html Sent from the R help mailing list archive at Nabble.com. __ 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] sapply and median, possible or not ?
You can provide a example of your data? On Fri, Nov 7, 2008 at 9:14 AM, Ptit_Bleu <[EMAIL PROTECTED]> wrote: > > Unfortunately, I have the same error message. > lapply(rowsplit, function(x)mean(x[,sapply(x, is.numeric)])) works but not > with median. > Strange, isn't it? > > Any other idea? > > Thanks in advance, > Ptit Bleu. > > > Henrique Dallazuanna wrote: > > > > Try this: > > > > lapply(l, function(x)median(x[,sapply(x, is.numeric)])) > > > > > > -- > > Henrique Dallazuanna > > Curitiba-Paraná-Brasil > > 25° 25' 40" S 49° 16' 22" O > > > > [[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. > > > > > > -- > View this message in context: > http://www.nabble.com/sapply-and-median%2C-possible-or-not---tp20378222p20378663.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[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] sapply and median, possible or not ?
I haven't looked at the detail, but I guess the answer is that mean works on a data frame while median doesn't. ?mean For a data frame, a named vector with the appropriate method being applied column by column. - I guess to use median you'll need nested '[l/s]apply's, the outer working through the list of dataframes and the inner working through the columns of each dataframe. Or perhaps, by analogy with mean.data.frame you could just define - median.data.frame <- function(x, ...) sapply(x, median, ...) I haven't tried it, but it might work hth Keith J - "Ptit_Bleu" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > Unfortunately, I have the same error message. > lapply(rowsplit, function(x)mean(x[,sapply(x, is.numeric)])) works but not > with median. > Strange, isn't it? > > Any other idea? > > Thanks in advance, > Ptit Bleu. > > > Henrique Dallazuanna wrote: >> >> Try this: >> >> lapply(l, function(x)median(x[,sapply(x, is.numeric)])) >> >> >> -- >> Henrique Dallazuanna >> Curitiba-Paraná-Brasil >> 25° 25' 40" S 49° 16' 22" O >> >> [[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. >> >> > > -- > View this message in context: > http://www.nabble.com/sapply-and-median%2C-possible-or-not---tp20378222p20378663.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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] sapply and median, possible or not ?
Try this: lapply(l, function(x)median(x[,sapply(x, is.numeric)])) On Fri, Nov 7, 2008 at 8:42 AM, Ptit_Bleu <[EMAIL PROTECTED]> wrote: > > Hello, > > I have a list of data.frame > rowsplit : List of 15 > $ (0,0.025] :'data.frame': 169 obs. of 7 variables: > $ (0.025,0.05]:'data.frame': 174 obs. of 7 variables: > $ (0.05,0.075]:'data.frame': 92 obs. of 7 variables: > $ (0.075,0.1] :'data.frame': 76 obs. of 7 variables: > $ (0.1,0.125] :'data.frame': 37 obs. of 7 variables: > $ (0.125,0.15]:'data.frame': 32 obs. of 7 variables: > $ (0.15,0.175]:'data.frame': 45 obs. of 7 variables: > $ (0.175,0.2] :'data.frame': 56 obs. of 7 variables: > $ (0.2,0.225] :'data.frame': 36 obs. of 7 variables: > $ (0.225,0.25]:'data.frame': 47 obs. of 7 variables: > $ (0.25,0.275]:'data.frame': 34 obs. of 7 variables: > $ (0.275,0.3] :'data.frame': 43 obs. of 7 variables: > $ (0.3,0.325] :'data.frame': 29 obs. of 7 variables: > $ (0.325,0.35]:'data.frame': 29 obs. of 7 variables: > $ (0.35,0.375]:'data.frame': 17 obs. of 7 variables: > > And I would like to get a data.frame gathering the median value of each > variable for each intervall. > as.data.frame(t(sapply(rowsplit, mean)) works well but I would prefer to > use > median but with sapply(rowsplit, median), I get the following message > "Error > in median.default(X[[1L]], ...) : need numeric data". > > Any idea ? > > Thanks in advance for your help, > Have a nice week-end, > Ptit Bleu. > > PS :I'm under XP and use R2.7.2. > -- > View this message in context: > http://www.nabble.com/sapply-and-median%2C-possible-or-not---tp20378222p20378222.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[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.