0

Trying to create a loot out of the following code which works well enough:

dateList = c("2016-01-08","2016-01-09","2016-01-10","2016-01-11",
         "2016-01-12")

df1 <- get_intraday_data(cookie, what="heart-rate", date=dateList[1])
df2 <- get_intraday_data(cookie, what="heart-rate", date=dateList[2])
df3 <- get_intraday_data(cookie, what="heart-rate", date=dateList[3])
df4 <- get_intraday_data(cookie, what="heart-rate", date=dateList[4])
df5 <- get_intraday_data(cookie, what="heart-rate", date=dateList[5])

I've written the loop below, which essentially substitutes 1-5 with [i] within a loop, but its just producing an integer named i. How can I get each day as its own dataframe?

    for (i in 1:5){
df[i] <- get_intraday_data(cookie, what="heart-rate", date=dateList[i])
}

Thanks

dingusaur
  • 11
  • 2
  • 1
    Make a [list of data frames](http://stackoverflow.com/q/17499013/903061). First initialize `df = list()`, then inside your loop `df[[i]] <- ...` – Gregor Thomas Jan 25 '16 at 04:38
  • 2
    `df <- lapply(dateList, function(x) get_intraday_data(cookie, what="heart-rate", date=x))`. – josliber Jan 25 '16 at 04:55

2 Answers2

0

You could try the map function in the purrr package. It's kinda like lapply. I'm not sure if I have it quite right, but maybe this will work:

purrr::map(1:5, ~ get_intraday_data(cookie, what="heart-rate", date=dateList[.x]))
Chris Merkord
  • 306
  • 1
  • 5
0

Using "assign" function inside a for loop:

dateList = c("2016-01-08","2016-01-09","2016-01-10","2016-01-11",
             "2016-01-12")

for (i in 1:length(dateList)) {
  assign(paste0("df", i), get_intraday_data(cookie, what="heart-rate", date=dateList[i]))
}
rocknaxe
  • 11
  • 1