Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I pass lists as arguments in R?

Tags:

r

I have a function fetchWeather(city,month1,year1,...) that takes an unlimited combination of month/year inputs and spits out weather data for those periods.

I'd like to make it a little more compact by doing some combinations by shorthand. so for example, rather than

fetchWeather(Boston,4,2015,5,2015,6,2015,7,2015,8,2015,9,2015,10,2015)

I'd like to do

jv15<- c(4,2015,5,2015,6,2015,7,2015,8,2015,9,2015,10,2015)

and then call

fetchWeather(Boston,jv15)

but I can't get this to work.

I'm pretty new to R and I imagine that this is a pretty easy solve but I can't seem to figure it out. any help is appreciated.

like image 371
jsg51483 Avatar asked Sep 01 '25 10:09

jsg51483


1 Answers

Your error might be that boston is not in quotes, but assuming that Boston is truly a variable...

You can use 'do.call'

Takes a function and a list and calls a function with that list as arguments.

So first you'd want to prepend Boston onto your list and then do.call.

out.weather <- do.call(fetchWeather, c(Boston,jv15))
like image 175
mhwelsh Avatar answered Sep 03 '25 00:09

mhwelsh