Assa Yeroslaviz
Assa Yeroslaviz

Reputation: 764

lapply error message when creating a sequence

I was wondering why the lapply function gives an error back when using this term:

>lapply(x=3:9, seq)
Error in match.fun(FUN) : argument "FUN" is missing, with no default

but does work perfectly, when running it like this:

>lapply(X=3:9, seq)

[[1]]    
[1] 1 2 3

[[2]]    
[1] 1 2 3 4

[[3]]    
[1] 1 2 3 4 5

[[4]]    
[1] 1 2 3 4 5 6

[[5]]   
[1] 1 2 3 4 5 6 7

[[6]]    
[1] 1 2 3 4 5 6 7 8

[[7]]
[1] 1 2 3 4 5 6 7 8 9

Both objects (x and X) are not available.

Is there some kind of internal functions at work here?

Assa

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] C/UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.0

Upvotes: 0

Views: 3011

Answers (2)

sjakw
sjakw

Reputation: 481

The lapply function requires 2 arguments X and FUN. Since R is case sensitive, it will not work with x instead of X. The error message is about FUN but the real problem is your x.

I recommend to look at the help page when you use a new function by running ?lapply.

Upvotes: 0

MrFlick
MrFlick

Reputation: 206401

The signature of lapply is

function (X, FUN, ...) 

so it has two named parameters: X and FUN. When you use X=3:9, it's setting the first parameter to 3:9. When you run x=3:9, then this parameter is being passed in the ... part (since R is case sensitive) and then seq is now the first parameter. It is customary not to use the name for the first parameter when calling lapply. just use

lapply(3:9, seq)

Upvotes: 4

Related Questions