That didn't work Jim! Thanks anyway On Mon, Apr 18, 2016 at 9:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Michael, > At a guess, try this: > > iqr<-function(x) { > return(paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > } > > .col3_Range=iqr(datat$tenure) > > Jim > > > > On Tue, Apr 19, 2016 at 11:15 AM, Michael Artz <michaeleartz at gmail.com> > wrote: > > Hi, > > I am trying to show an interquartile range while grouping values using > > the function ddply(). So my function call now is like > > > > groupedAll <- ddply(data > > ,~groupColumn > > ,summarise > > ,col1_mean=mean(col1) > > ,col2_mode=Mode(col2) #Function I wrote for getting the > > mode shown below > > > > ,col3_Range=paste(as.character(round(quantile(datat$tenure,c(.25)))), > > as.character(round(quantile(data$tenure,c(.75)))), sep = "-") > > ) > > > > #custom Mode function > > Mode <- function(x) { > > ux <- unique(x) > > ux[which.max(tabulate(match(x, ux)))] > > } > > > > I am not sre what is going wrong on my interquartile range function, it > > works on its own outside of ddply() > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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. >[[alternative HTML version deleted]]
Are you aware that there *already is* a function that does this? ?IQR (also your "function" iqr" is just a character string and would have to be parsed and evaluated to become a function. But this is a TERRIBLE way to do things in R as it completely circumvents R's central functional programming paradigm). 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, Apr 19, 2016 at 7:56 AM, Michael Artz <michaeleartz at gmail.com> wrote:> That didn't work Jim! > > Thanks anyway > > On Mon, Apr 18, 2016 at 9:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote: > >> Hi Michael, >> At a guess, try this: >> >> iqr<-function(x) { >> return(paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") >> } >> >> .col3_Range=iqr(datat$tenure) >> >> Jim >> >> >> >> On Tue, Apr 19, 2016 at 11:15 AM, Michael Artz <michaeleartz at gmail.com> >> wrote: >> > Hi, >> > I am trying to show an interquartile range while grouping values using >> > the function ddply(). So my function call now is like >> > >> > groupedAll <- ddply(data >> > ,~groupColumn >> > ,summarise >> > ,col1_mean=mean(col1) >> > ,col2_mode=Mode(col2) #Function I wrote for getting the >> > mode shown below >> > >> > ,col3_Range=paste(as.character(round(quantile(datat$tenure,c(.25)))), >> > as.character(round(quantile(data$tenure,c(.75)))), sep = "-") >> > ) >> > >> > #custom Mode function >> > Mode <- function(x) { >> > ux <- unique(x) >> > ux[which.max(tabulate(match(x, ux)))] >> > } >> > >> > I am not sre what is going wrong on my interquartile range function, it >> > works on its own outside of ddply() >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > 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. >> > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi bert, I understand the difference between a character string and a number. I need to return a character string, that is a requirement. It needs to be in that format. Getting the range with IQR is trivial I already tried it. The grouping function accepts only one return value, and IQR returns two. Thanks for the reply though sir. On Apr 19, 2016 10:20 AM, "Bert Gunter" <bgunter.4567 at gmail.com> wrote:> Are you aware that there *already is* a function that does this? > > ?IQR > > (also your "function" iqr" is just a character string and would have > to be parsed and evaluated to become a function. But this is a > TERRIBLE way to do things in R as it completely circumvents R's > central functional programming paradigm). > > 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, Apr 19, 2016 at 7:56 AM, Michael Artz <michaeleartz at gmail.com> > wrote: > > That didn't work Jim! > > > > Thanks anyway > > > > On Mon, Apr 18, 2016 at 9:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote: > > > >> Hi Michael, > >> At a guess, try this: > >> > >> iqr<-function(x) { > >> > return(paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > >> } > >> > >> .col3_Range=iqr(datat$tenure) > >> > >> Jim > >> > >> > >> > >> On Tue, Apr 19, 2016 at 11:15 AM, Michael Artz <michaeleartz at gmail.com> > >> wrote: > >> > Hi, > >> > I am trying to show an interquartile range while grouping values > using > >> > the function ddply(). So my function call now is like > >> > > >> > groupedAll <- ddply(data > >> > ,~groupColumn > >> > ,summarise > >> > ,col1_mean=mean(col1) > >> > ,col2_mode=Mode(col2) #Function I wrote for getting > the > >> > mode shown below > >> > > >> > ,col3_Range=paste(as.character(round(quantile(datat$tenure,c(.25)))), > >> > as.character(round(quantile(data$tenure,c(.75)))), sep = "-") > >> > ) > >> > > >> > #custom Mode function > >> > Mode <- function(x) { > >> > ux <- unique(x) > >> > ux[which.max(tabulate(match(x, ux)))] > >> > } > >> > > >> > I am not sre what is going wrong on my interquartile range function, > it > >> > works on its own outside of ddply() > >> > > >> > [[alternative HTML version deleted]] > >> > > >> > ______________________________________________ > >> > 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. > >> > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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. >[[alternative HTML version deleted]]
> That didn't work Jim!It always helps to say how the suggestion did not work. Jim's function had a typo in it - was that the problem? Or did you not change the call to ddply to use that function. Here is something that might "work" for you: library(plyr) data <- data.frame(groupColumn=rep(1:5,1:5), col1=2^(0:14)) myIqr <- function(x) { paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") } ddply(data, ~groupColumn, summarise, col1_myIqr=myIqr(col1), col1_IQR=stats::IQR(col1)) # groupColumn col1_myIqr col1_IQR #1 1 1-1 0 #2 2 2-4 1 #3 3 12-24 12 #4 4 112-320 208 #5 5 2048-8192 6144 The important point is that paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") is not a function, it is an expression. ddplyr wants functions. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Apr 19, 2016 at 7:56 AM, Michael Artz <michaeleartz at gmail.com> wrote:> That didn't work Jim! > > Thanks anyway > > On Mon, Apr 18, 2016 at 9:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote: > > > Hi Michael, > > At a guess, try this: > > > > iqr<-function(x) { > > > return(paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > > } > > > > .col3_Range=iqr(datat$tenure) > > > > Jim > > > > > > > > On Tue, Apr 19, 2016 at 11:15 AM, Michael Artz <michaeleartz at gmail.com> > > wrote: > > > Hi, > > > I am trying to show an interquartile range while grouping values > using > > > the function ddply(). So my function call now is like > > > > > > groupedAll <- ddply(data > > > ,~groupColumn > > > ,summarise > > > ,col1_mean=mean(col1) > > > ,col2_mode=Mode(col2) #Function I wrote for getting > the > > > mode shown below > > > > > > ,col3_Range=paste(as.character(round(quantile(datat$tenure,c(.25)))), > > > as.character(round(quantile(data$tenure,c(.75)))), sep = "-") > > > ) > > > > > > #custom Mode function > > > Mode <- function(x) { > > > ux <- unique(x) > > > ux[which.max(tabulate(match(x, ux)))] > > > } > > > > > > I am not sre what is going wrong on my interquartile range function, it > > > works on its own outside of ddply() > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
HI that did not work for me either. The value I got returned from that function was "<rounded mean> - <rounded mean>" :(. thanks for the reply through On Tue, Apr 19, 2016 at 10:34 AM, William Dunlap <wdunlap at tibco.com> wrote:> > That didn't work Jim! > > It always helps to say how the suggestion did not work. Jim's > function had a typo in it - was that the problem? Or did you not > change the call to ddply to use that function. Here is something > that might "work" for you: > > library(plyr) > > data <- data.frame(groupColumn=rep(1:5,1:5), col1=2^(0:14)) > myIqr <- function(x) { > paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > } > ddply(data, ~groupColumn, summarise, col1_myIqr=myIqr(col1), > col1_IQR=stats::IQR(col1)) > # groupColumn col1_myIqr col1_IQR > #1 1 1-1 0 > #2 2 2-4 1 > #3 3 12-24 12 > #4 4 112-320 208 > #5 5 2048-8192 6144 > > The important point is that > paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > is not a function, it is an expression. ddplyr wants functions. > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Tue, Apr 19, 2016 at 7:56 AM, Michael Artz <michaeleartz at gmail.com> > wrote: > >> That didn't work Jim! >> >> Thanks anyway >> >> On Mon, Apr 18, 2016 at 9:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote: >> >> > Hi Michael, >> > At a guess, try this: >> > >> > iqr<-function(x) { >> > >> return(paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") >> > } >> > >> > .col3_Range=iqr(datat$tenure) >> > >> > Jim >> > >> > >> > >> > On Tue, Apr 19, 2016 at 11:15 AM, Michael Artz <michaeleartz at gmail.com> >> > wrote: >> > > Hi, >> > > I am trying to show an interquartile range while grouping values >> using >> > > the function ddply(). So my function call now is like >> > > >> > > groupedAll <- ddply(data >> > > ,~groupColumn >> > > ,summarise >> > > ,col1_mean=mean(col1) >> > > ,col2_mode=Mode(col2) #Function I wrote for getting >> the >> > > mode shown below >> > > >> > > ,col3_Range=paste(as.character(round(quantile(datat$tenure,c(.25)))), >> > > as.character(round(quantile(data$tenure,c(.75)))), sep = "-") >> > > ) >> > > >> > > #custom Mode function >> > > Mode <- function(x) { >> > > ux <- unique(x) >> > > ux[which.max(tabulate(match(x, ux)))] >> > > } >> > > >> > > I am not sre what is going wrong on my interquartile range function, >> it >> > > works on its own outside of ddply() >> > > >> > > [[alternative HTML version deleted]] >> > > >> > > ______________________________________________ >> > > 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. >> > >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > >[[alternative HTML version deleted]]
To be precise: paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") is an expression that evaluates to a character string: "round(quantile(x,.25),0) - round(quantile(x,0.75),0)" no matter what the argument of your function, x. Hence return(paste(...)) will return this exact character string and never evaluates x. 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, Apr 19, 2016 at 8:34 AM, William Dunlap via R-help <r-help at r-project.org> wrote:>> That didn't work Jim! > > It always helps to say how the suggestion did not work. Jim's > function had a typo in it - was that the problem? Or did you not > change the call to ddply to use that function. Here is something > that might "work" for you: > > library(plyr) > > data <- data.frame(groupColumn=rep(1:5,1:5), col1=2^(0:14)) > myIqr <- function(x) { > paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > } > ddply(data, ~groupColumn, summarise, col1_myIqr=myIqr(col1), > col1_IQR=stats::IQR(col1)) > # groupColumn col1_myIqr col1_IQR > #1 1 1-1 0 > #2 2 2-4 1 > #3 3 12-24 12 > #4 4 112-320 208 > #5 5 2048-8192 6144 > > The important point is that > paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") > is not a function, it is an expression. ddplyr wants functions. > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Tue, Apr 19, 2016 at 7:56 AM, Michael Artz <michaeleartz at gmail.com> > wrote: > >> That didn't work Jim! >> >> Thanks anyway >> >> On Mon, Apr 18, 2016 at 9:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote: >> >> > Hi Michael, >> > At a guess, try this: >> > >> > iqr<-function(x) { >> > >> return(paste(round(quantile(x,0.25),0),round(quantile(x,0.75),0),sep="-") >> > } >> > >> > .col3_Range=iqr(datat$tenure) >> > >> > Jim >> > >> > >> > >> > On Tue, Apr 19, 2016 at 11:15 AM, Michael Artz <michaeleartz at gmail.com> >> > wrote: >> > > Hi, >> > > I am trying to show an interquartile range while grouping values >> using >> > > the function ddply(). So my function call now is like >> > > >> > > groupedAll <- ddply(data >> > > ,~groupColumn >> > > ,summarise >> > > ,col1_mean=mean(col1) >> > > ,col2_mode=Mode(col2) #Function I wrote for getting >> the >> > > mode shown below >> > > >> > > ,col3_Range=paste(as.character(round(quantile(datat$tenure,c(.25)))), >> > > as.character(round(quantile(data$tenure,c(.75)))), sep = "-") >> > > ) >> > > >> > > #custom Mode function >> > > Mode <- function(x) { >> > > ux <- unique(x) >> > > ux[which.max(tabulate(match(x, ux)))] >> > > } >> > > >> > > I am not sre what is going wrong on my interquartile range function, it >> > > works on its own outside of ddply() >> > > >> > > [[alternative HTML version deleted]] >> > > >> > > ______________________________________________ >> > > 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. >> > >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.