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.
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
on 11/20/2008 10:31 AM 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,Try this: fn <- function(i) i^2> lapply(1:4, fn)[[1]] [1] 1 [[2]] [1] 4 [[3]] [1] 9 [[4]] [1] 16 The error message indicates that the argument 'i' that you have in your initial attempt to use lappply() is unused, because the values 1:4 are passed to the function's first argument by default already. Thus, specifying 'i' again is an error, since there is not a second argument in your function fn(). Note also that 'return()' is not needed, as per the Details in ?return: "If the end of a function is reached without calling return, the value of the last evaluated expression is returned." Note also, that since lapply() effectively uses an internal loop, a faster "vectorized" approach would be:> as.list((1:4) ^ 2)[[1]] [1] 1 [[2]] [1] 4 [[3]] [1] 9 [[4]] [1] 16 For example, using 100,000 instead of 4:> system.time(x1 <- lapply(1:100000, fn))user system elapsed 0.500 0.015 0.600> system.time(x2 <- as.list((1:100000) ^ 2))user system elapsed 0.018 0.004 0.039> identical(x1, x2)[1] TRUE HTH, Marc Schwartz
lapply already passes the first arg to fn and by specifying the i (which is undefined -- its only defined within fn) it would be trying to to pass a second arg to fn yet fn takes only takes one arg. Try these: lapply(1:4, fn) lapply(1:4, "^", 2) On Thu, Nov 20, 2008 at 11:31 AM, megh <megh700004 at yahoo.com> 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. >