简体   繁体   中英

Passing a List as Function Argument R

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) 

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))

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