[R] Error with lapply

baptiste auguie ba208 at exeter.ac.uk
Thu Nov 20 17:43:20 CET 2008


Hi,

you are feeding lapply "i" as an optional argument, which is passed to  
fn() and causes an error. Just use lapply(1:4, fn), or better yet,  
sapply,

 > fn <- function(i) return(i^2)
 > sapply(1:4, fn)
[1]  1  4  9 16

Hope this helps,

baptiste


On 20 Nov 2008, at 16:31, megh wrote:

>
> I have written following codes, with intention to get a list with  
> values
> 1,2,9,16 :
>
> fn <- function(i) return(i^2)
> lapply(1:4, fn, i)
>
> However I got following error :
> Error in FUN(1:4[[1L]], ...) : unused argument(s) (1)
>
> Can anyone please tell me what will be the correct code here?
>
> Regards,
>
>
> --
> View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20605066.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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list