简体   繁体   中英

R mapply with named arguments

One fear I have when using mapply in R is that I may mess up the order of arguments & hence unconsciously generate garbage results.

mydata<-data.frame(Temperature=foobar,Pressure=foobar2)
myfunction<-function(P,T)
{
....
}
mapply(FUN = myfunction,mydata$Temperature,mydata$Pressure)

Is there a way to utilize named arguments to avoid this sort of error via mapply?

If we need to match the function arguments, name the arguments for Map/mapply with the arguments of the function

mapply(FUN = myfunction,T=mydata$Temperature,P=mydata$Pressure)

We can apply the function directly instead of mapply though (based on the example provided below in my post)

do.call(myfunction, unname(mydata[2:1]))

data

mydata <- data.frame(Temperature = 1:5, Pressure = 16:20)
myfunction <- function(P, T) {P*5 + T*10}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM