Home > Enterprise >  Passing a List as Function Argument R
Passing a List as Function Argument R

Time:11-30

I have a function that retrieves game data from ESPN. It takes one argument. Need a way to create a list of game ids that the function can loop over. We will call the name of the function "get_data".

The function works for a single game

get_data(1000001)

But if I have multiple games stored in a list... how can I loop over them in one call?

game_ids <- c("1000001", "1000002", "1000003")


get_data(game_ids) 

CodePudding user response:

If it is not vectorized, Vectorize it and apply on a vector of 'ids' with length >= 1

Vectorize(get_data)(game_ids)

Or another option is to loop over the vector and apply the function individually

lst1 <- lapply(game_ids, function(x) get_data(x))
  • Related