[R] function

David Winsemius dwinsemius at comcast.net
Wed May 12 19:41:25 CEST 2010


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

-- 
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



More information about the R-help mailing list