[R] function

William Dunlap wdunlap at tibco.com
Wed May 12 20:07:52 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
> Sent: Wednesday, May 12, 2010 10:41 AM
> To: Daniel Malter
> Cc: r-help at r-project.org
> Subject: Re: [R] function
> 
> 
> On May 12, 2010, at 1:28 PM, Daniel Malter wrote:
> 
> >
> > There is too little information to answer your question 
> definitively.
> >
> > However, an obvious reason is that you want to apply the 
> function over
> > columns of a data.frame, which is done with apply(), but 
> you try to  
> > apply
> > the function over elements of a list using lapply(). A list is not a
> > data.frame and vice versa,
> 
> Not correct. Using your example below:
> 
>  > is.list(data)
> [1] TRUE
> 
> > which would be a good reason for your function to
> > fail.
> 
> Maybe, maybe not. lapply() works quite well with dataframes:
> 
>  > lapply(data, sum)
> $y
> [1] 2.982636
> 
> $x
> [1] -4.718842
> 
> $e
> [1] 0.969399

Furthermore, apply() can work quite badly on data.frames.
If all columns have the same type it is generally ok, as in
   > d <- data.frame(x=c(1,-10,31.4159265),y=c(666,.05,9.999))
   > apply(d,2,max)
          x         y
   31.41593 666.00000
But if you add a character (or POSIXct or ...) column you
get bad results
   > d$name <- c("Joe", "Jack", "Socrates")
   > apply(d,2,max)
             x           y        name
   " 31.41593"   "  9.999"  "Socrates"

Use plyr or [ls]apply on data.frames, not apply.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
   
> 
> -- 
> David.
> 
> >  The below example works:
> >
> > data=data.frame(y=rnorm(100),x=rnorm(100),e=rnorm(100))
> >
> > f=function(x){quantile(x,probs=0.25)}
> >
> > apply(data,2,f)
> >
> > Also, for debugging, you want to disassemble the whole and check  
> > whether the
> > individual parts work. Does the "function()" work when applied on  
> > just one
> > column, for example? If not, then you also have a problem with the
> > definition of the function. If so, you would have to check, 
> whether  
> > the
> > quantile or IQR function fails in one of the instances.
> >
> > HTH,
> > Daniel
> > -- 
> > View this message in context: 
> http://r.789695.n4.nabble.com/function-tp2196408p2196445.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > R-help at 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.
> 
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list