Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function error : argument 3 matches multiple formal arguments

Tags:

r

This code :

f1 <- function(abc , ac , df){
  list(f1 = abc , f2=ac , f3 = df)
}

f1(1,2,a=3)

returns error :

Error in f1(1, 2, a = 3) : argument 3 matches multiple formal arguments

Is reason for this error a is being matched by arguments abc , ac in function f1 due to function partial matching ?

like image 253
blue-sky Avatar asked Dec 02 '17 10:12

blue-sky


1 Answers

When a named argument doesn't match exactly any formal arguments, R tries to apply partial matching by prefix. abc and ac both start with "a", and R doesn't know which one it should use, so it raises an error.

If you use f1(1, 2, ac=3), then R will assign 3 to ac, and then it will assign the remaining values to the remaining unassigned parameters, so 1 to abc and 2 to df.

like image 73
janos Avatar answered Sep 24 '22 02:09

janos