Home > Software design >  Renaming multiple nested lists in R
Renaming multiple nested lists in R

Time:06-02

I am trying to prepare some raw data for a function I made. This is the function:

getSleep <- function(auth.code, start.date, end.date){
  start = as.Date(start.date)
  end = as.Date(end.date)
  #create dataframe
  all.days = data.frame(Time = c(),Sleep = c())
  #loop through every day
  current.date = start
  while (current.date <= end)
  {
    query.response <- GET(paste0("https://api.fitbit.com/1/user/-/sleep/date/",
                                 format(current.date),
                                 ".json"),
                          add_headers(
                            Authorization = auth.code)
    )
    #check for error
    if(query.response$status_code == 200){
      
      json.sleep <- content(query.response, "parsed")
      
      names(json.sleep$sleep)[1] <- "Data"
      
      new.day.dataset <- json.sleep$sleep$Data$minuteData
      #
      if(is.null(new.day.dataset) || length(new.day.dataset) == 0){
        print(paste(
          "No data found on ",
          format(current.date),
          "skipping date and moving on"
        ))
        current.date <- current.date   1
        next
      }
      new.day <- data.frame(matrix(unlist(new.day.dataset), ncol = 2, byrow = T), stringsAsFactors = FALSE)
      
      new.day <- new.day %>% rename(Time = X1, Sleep = X2) %>% 
        mutate(Time = ymd_hms(paste(format(current.date), Time)),
               Sleep = as.numeric(Sleep))
      
      all.days <- rbind(all.days, new.day)
      
      current.date <- current.date   1     
    }
    #we errored
    else{
      print(paste(
        "Error on ",
        format(current.date)
      ))
      #What was the error?
      print(query.response)
      #rate limit error?
      if(query.response$status_code == 429){
        print("Hit rate limit, waiting 1 hour")
        Sys.sleep(3600)
      }
      else{
        return(all.days)
        break
      }
      
    }
    
  }
  return(all.days)
}

I have exported a large list with available raw data for one person, like below.

$sleep[[19]]$minuteData[[257]]$value
[1] "1"


$sleep[[19]]$minuteData[[258]]
$sleep[[19]]$minuteData[[258]]$dateTime
[1] "17:24:00"

$sleep[[19]]$minuteData[[258]]$value
[1] "1"


$sleep[[19]]$minuteData[[259]]
$sleep[[19]]$minuteData[[259]]$dateTime
[1] "17:25:00"

$sleep[[19]]$minuteData[[259]]$value
[1] "1"


$sleep[[19]]$minuteData[[260]]
$sleep[[19]]$minuteData[[260]]$dateTime
[1] "17:26:00"

$sleep[[19]]$minuteData[[260]]$value
[1] "1"


$sleep[[19]]$minuteData[[261]]
$sleep[[19]]$minuteData[[261]]$dateTime
[1] "17:27:00"

$sleep[[19]]$minuteData[[261]]$value
[1] "1"


$sleep[[19]]$minuteData[[262]]
$sleep[[19]]$minuteData[[262]]$dateTime
[1] "17:28:00"

$sleep[[19]]$minuteData[[262]]$value
[1] "1"


$sleep[[19]]$minuteData[[263]]
$sleep[[19]]$minuteData[[263]]$dateTime
[1] "17:29:00"

$sleep[[19]]$minuteData[[263]]$value
[1] "1"


$sleep[[19]]$minuteData[[264]]
$sleep[[19]]$minuteData[[264]]$dateTime
[1] "17:30:00"

$sleep[[19]]$minuteData[[264]]$value
[1] "1"


$sleep[[19]]$minuteData[[265]]
$sleep[[19]]$minuteData[[265]]$dateTime
[1] "17:31:00"

$sleep[[19]]$minuteData[[265]]$value
[1] "1"


$sleep[[19]]$minuteData[[266]]
$sleep[[19]]$minuteData[[266]]$dateTime
[1] "17:32:00"

$sleep[[19]]$minuteData[[266]]$value
[1] "1"


$sleep[[19]]$minuteData[[267]]
$sleep[[19]]$minuteData[[267]]$dateTime
[1] "17:33:00"

$sleep[[19]]$minuteData[[267]]$value
[1] "1"


$sleep[[19]]$minuteData[[268]]
$sleep[[19]]$minuteData[[268]]$dateTime
[1] "17:34:00"

$sleep[[19]]$minuteData[[268]]$value
[1] "1"


$sleep[[19]]$minuteData[[269]]
$sleep[[19]]$minuteData[[269]]$dateTime
[1] "17:35:00"

$sleep[[19]]$minuteData[[269]]$value
[1] "1"


$sleep[[19]]$minuteData[[270]]
$sleep[[19]]$minuteData[[270]]$dateTime
[1] "17:36:00"

$sleep[[19]]$minuteData[[270]]$value
[1] "1"


$sleep[[19]]$minuteData[[271]]
$sleep[[19]]$minuteData[[271]]$dateTime
[1] "17:37:00"

$sleep[[19]]$minuteData[[271]]$value
[1] "1"


$sleep[[19]]$minuteData[[272]]
$sleep[[19]]$minuteData[[272]]$dateTime
[1] "17:38:00"

$sleep[[19]]$minuteData[[272]]$value
[1] "1"


$sleep[[19]]$minuteData[[273]]
$sleep[[19]]$minuteData[[273]]$dateTime
[1] "17:39:00"

$sleep[[19]]$minuteData[[273]]$value
[1] "1"


$sleep[[19]]$minuteData[[274]]
$sleep[[19]]$minuteData[[274]]$dateTime
[1] "17:40:00"

$sleep[[19]]$minuteData[[274]]$value
[1] "1"


$sleep[[19]]$minuteData[[275]]
$sleep[[19]]$minuteData[[275]]$dateTime
[1] "17:41:00"

$sleep[[19]]$minuteData[[275]]$value
[1] "1"


$sleep[[19]]$minuteData[[276]]
$sleep[[19]]$minuteData[[276]]$dateTime
[1] "17:42:00"

$sleep[[19]]$minuteData[[276]]$value
[1] "1"


$sleep[[19]]$minuteData[[277]]
$sleep[[19]]$minuteData[[277]]$dateTime
[1] "17:43:00"

$sleep[[19]]$minuteData[[277]]$value
[1] "1"


$sleep[[19]]$minuteData[[278]]
$sleep[[19]]$minuteData[[278]]$dateTime
[1] "17:44:00"

$sleep[[19]]$minuteData[[278]]$value
[1] "1"


$sleep[[19]]$minuteData[[279]]
$sleep[[19]]$minuteData[[279]]$dateTime
[1] "17:45:00"

$sleep[[19]]$minuteData[[279]]$value
[1] "1"


$sleep[[19]]$minuteData[[280]]
$sleep[[19]]$minuteData[[280]]$dateTime
[1] "17:46:00"

$sleep[[19]]$minuteData[[280]]$value
[1] "1"


$sleep[[19]]$minuteData[[281]]
$sleep[[19]]$minuteData[[281]]$dateTime
[1] "17:47:00"

$sleep[[19]]$minuteData[[281]]$value
[1] "1"


$sleep[[19]]$minuteData[[282]]
$sleep[[19]]$minuteData[[282]]$dateTime
[1] "17:48:00"

$sleep[[19]]$minuteData[[282]]$value
[1] "1"


$sleep[[19]]$minuteData[[283]]
$sleep[[19]]$minuteData[[283]]$dateTime
[1] "17:49:00"

$sleep[[19]]$minuteData[[283]]$value
[1] "1"


$sleep[[19]]$minuteData[[284]]
$sleep[[19]]$minuteData[[284]]$dateTime
[1] "17:50:00"

$sleep[[19]]$minuteData[[284]]$value
[1] "1"


$sleep[[19]]$minuteData[[285]]
$sleep[[19]]$minuteData[[285]]$dateTime
[1] "17:51:00"

$sleep[[19]]$minuteData[[285]]$value
[1] "1"


$sleep[[19]]$minuteData[[286]]
$sleep[[19]]$minuteData[[286]]$dateTime
[1] "17:52:00"

$sleep[[19]]$minuteData[[286]]$value
[1] "1"


$sleep[[19]]$minuteData[[287]]
$sleep[[19]]$minuteData[[287]]$dateTime
[1] "17:53:00"

$sleep[[19]]$minuteData[[287]]$value
[1] "1"


$sleep[[19]]$minuteData[[288]]
$sleep[[19]]$minuteData[[288]]$dateTime
[1] "17:54:00"

$sleep[[19]]$minuteData[[288]]$value
[1] "1"


$sleep[[19]]$minuteData[[289]]
$sleep[[19]]$minuteData[[289]]$dateTime
[1] "17:55:00"

$sleep[[19]]$minuteData[[289]]$value
[1] "1"


$sleep[[19]]$minuteData[[290]]
$sleep[[19]]$minuteData[[290]]$dateTime
[1] "17:56:00"

$sleep[[19]]$minuteData[[290]]$value
[1] "1"


$sleep[[19]]$minuteData[[291]]
$sleep[[19]]$minuteData[[291]]$dateTime
[1] "17:57:00"

$sleep[[19]]$minuteData[[291]]$value
[1] "1"


$sleep[[19]]$minuteData[[292]]
$sleep[[19]]$minuteData[[292]]$dateTime
[1] "17:58:00"

$sleep[[19]]$minuteData[[292]]$value
[1] "1"


$sleep[[19]]$minuteData[[293]]
$sleep[[19]]$minuteData[[293]]$dateTime
[1] "17:59:00"

$sleep[[19]]$minuteData[[293]]$value
[1] "1"


$sleep[[19]]$minuteData[[294]]
$sleep[[19]]$minuteData[[294]]$dateTime
[1] "18:00:00"

$sleep[[19]]$minuteData[[294]]$value
[1] "1"


$sleep[[19]]$minuteData[[295]]
$sleep[[19]]$minuteData[[295]]$dateTime
[1] "18:01:00"

$sleep[[19]]$minuteData[[295]]$value
[1] "1"


$sleep[[19]]$minuteData[[296]]
$sleep[[19]]$minuteData[[296]]$dateTime
[1] "18:02:00"

$sleep[[19]]$minuteData[[296]]$value
[1] "1"


$sleep[[19]]$minuteData[[297]]
$sleep[[19]]$minuteData[[297]]$dateTime
[1] "18:03:00"

$sleep[[19]]$minuteData[[297]]$value
[1] "1"


$sleep[[19]]$minuteData[[298]]
$sleep[[19]]$minuteData[[298]]$dateTime
[1] "18:04:00"

$sleep[[19]]$minuteData[[298]]$value
[1] "1"


$sleep[[19]]$minuteData[[299]]
$sleep[[19]]$minuteData[[299]]$dateTime
[1] "18:05:00"

$sleep[[19]]$minuteData[[299]]$value
[1] "1"


$sleep[[19]]$minuteData[[300]]
$sleep[[19]]$minuteData[[300]]$dateTime
[1] "18:06:00"

$sleep[[19]]$minuteData[[300]]$value
[1] "1"


$sleep[[19]]$minuteData[[301]]
$sleep[[19]]$minuteData[[301]]$dateTime
[1] "18:07:00"

$sleep[[19]]$minuteData[[301]]$value
[1] "1"


$sleep[[19]]$minuteData[[302]]
$sleep[[19]]$minuteData[[302]]$dateTime
[1] "18:08:00"

$sleep[[19]]$minuteData[[302]]$value
[1] "1"


$sleep[[19]]$minuteData[[303]]
$sleep[[19]]$minuteData[[303]]$dateTime
[1] "18:09:00"

$sleep[[19]]$minuteData[[303]]$value
[1] "1"


$sleep[[19]]$minuteData[[304]]
$sleep[[19]]$minuteData[[304]]$dateTime
[1] "18:10:00"

$sleep[[19]]$minuteData[[304]]$value
[1] "1"


$sleep[[19]]$minuteData[[305]]
$sleep[[19]]$minuteData[[305]]$dateTime
[1] "18:11:00"

$sleep[[19]]$minuteData[[305]]$value
[1] "1"


$sleep[[19]]$minuteData[[306]]
$sleep[[19]]$minuteData[[306]]$dateTime
[1] "18:12:00"

$sleep[[19]]$minuteData[[306]]$value
[1] "1"


$sleep[[19]]$minuteData[[307]]
$sleep[[19]]$minuteData[[307]]$dateTime
[1] "18:13:00"

$sleep[[19]]$minuteData[[307]]$value
[1] "1"


$sleep[[19]]$minuteData[[308]]
$sleep[[19]]$minuteData[[308]]$dateTime
[1] "18:14:00"

$sleep[[19]]$minuteData[[308]]$value
[1] "1"


$sleep[[19]]$minuteData[[309]]
$sleep[[19]]$minuteData[[309]]$dateTime
[1] "18:15:00"

$sleep[[19]]$minuteData[[309]]$value
[1] "1"


$sleep[[19]]$minuteData[[310]]
$sleep[[19]]$minuteData[[310]]$dateTime
[1] "18:16:00"

$sleep[[19]]$minuteData[[310]]$value
[1] "1"


$sleep[[19]]$minuteData[[311]]
$sleep[[19]]$minuteData[[311]]$dateTime
[1] "18:17:00"

$sleep[[19]]$minuteData[[311]]$value
[1] "3"


$sleep[[19]]$minuteData[[312]]
$sleep[[19]]$minuteData[[312]]$dateTime
[1] "18:18:00"

$sleep[[19]]$minuteData[[312]]$value
[1] "3"


$sleep[[19]]$minuteData[[313]]
$sleep[[19]]$minuteData[[313]]$dateTime
[1] "18:19:00"

$sleep[[19]]$minuteData[[313]]$value
[1] "3"


$sleep[[19]]$minuteData[[314]]
$sleep[[19]]$minuteData[[314]]$dateTime
[1] "18:20:00"

$sleep[[19]]$minuteData[[314]]$value
[1] "3"


$sleep[[19]]$minuteData[[315]]
$sleep[[19]]$minuteData[[315]]$dateTime
[1] "18:21:00"

$sleep[[19]]$minuteData[[315]]$value
[1] "3"


$sleep[[19]]$minuteData[[316]]
$sleep[[19]]$minuteData[[316]]$dateTime
[1] "18:22:00"

$sleep[[19]]$minuteData[[316]]$value
[1] "2"


$sleep[[19]]$minuteData[[317]]
$sleep[[19]]$minuteData[[317]]$dateTime
[1] "18:23:00"

$sleep[[19]]$minuteData[[317]]$value
[1] "1"


$sleep[[19]]$minuteData[[318]]
$sleep[[19]]$minuteData[[318]]$dateTime
[1] "18:24:00"

$sleep[[19]]$minuteData[[318]]$value
[1] "3"


$sleep[[19]]$minuteData[[319]]
$sleep[[19]]$minuteData[[319]]$dateTime
[1] "18:25:00"

$sleep[[19]]$minuteData[[319]]$value
[1] "3"


$sleep[[19]]$minuteData[[320]]
$sleep[[19]]$minuteData[[320]]$dateTime
[1] "18:26:00"

$sleep[[19]]$minuteData[[320]]$value
[1] "3"


$sleep[[19]]$minuteData[[321]]
$sleep[[19]]$minuteData[[321]]$dateTime
[1] "18:27:00"

$sleep[[19]]$minuteData[[321]]$value
[1] "3"


$sleep[[19]]$minuteData[[322]]
$sleep[[19]]$minuteData[[322]]$dateTime
[1] "18:28:00"

$sleep[[19]]$minuteData[[322]]$value
[1] "3"


$sleep[[19]]$minuteData[[323]]
$sleep[[19]]$minuteData[[323]]$dateTime
[1] "18:29:00"

$sleep[[19]]$minuteData[[323]]$value
[1] "1"


$sleep[[19]]$minuteData[[324]]
$sleep[[19]]$minuteData[[324]]$dateTime
[1] "18:30:00"

$sleep[[19]]$minuteData[[324]]$value
[1] "3"


$sleep[[19]]$minuteData[[325]]
$sleep[[19]]$minuteData[[325]]$dateTime
[1] "18:31:00"

$sleep[[19]]$minuteData[[325]]$value
[1] "1"


$sleep[[19]]$minuteData[[326]]
$sleep[[19]]$minuteData[[326]]$dateTime
[1] "18:32:00"

$sleep[[19]]$minuteData[[326]]$value
[1] "1"


$sleep[[19]]$minuteData[[327]]
$sleep[[19]]$minuteData[[327]]$dateTime
[1] "18:33:00"

$sleep[[19]]$minuteData[[327]]$value
[1] "1"


$sleep[[19]]$minuteData[[328]]
$sleep[[19]]$minuteData[[328]]$dateTime
[1] "18:34:00"

$sleep[[19]]$minuteData[[328]]$value
[1] "1"


$sleep[[19]]$minuteData[[329]]
$sleep[[19]]$minuteData[[329]]$dateTime
[1] "18:35:00"

$sleep[[19]]$minuteData[[329]]$value
[1] "1"


$sleep[[19]]$minuteData[[330]]
$sleep[[19]]$minuteData[[330]]$dateTime
[1] "18:36:00"

$sleep[[19]]$minuteData[[330]]$value
[1] "1"


$sleep[[19]]$minuteData[[331]]
$sleep[[19]]$minuteData[[331]]$dateTime
[1] "18:37:00"

$sleep[[19]]$minuteData[[331]]$value
[1] "1"


$sleep[[19]]$minuteData[[332]]
$sleep[[19]]$minuteData[[332]]$dateTime
[1] "18:38:00"

$sleep[[19]]$minuteData[[332]]$value
[1] "1"


$sleep[[19]]$minuteData[[333]]
$sleep[[19]]$minuteData[[333]]$dateTime
[1] "18:39:00"

$sleep[[19]]$minuteData[[333]]$value
[1] "1"


$sleep[[19]]$minuteData[[334]]
$sleep[[19]]$minuteData[[334]]$dateTime
[1] "18:40:00"

$sleep[[19]]$minuteData[[334]]$value
[1] "1"


$sleep[[19]]$minuteData[[335]]
$sleep[[19]]$minuteData[[335]]$dateTime
[1] "18:41:00"

$sleep[[19]]$minuteData[[335]]$value
[1] "1"


$sleep[[19]]$minuteData[[336]]
$sleep[[19]]$minuteData[[336]]$dateTime
[1] "18:42:00"

$sleep[[19]]$minuteData[[336]]$value
[1] "1"


$sleep[[19]]$minuteData[[337]]
$sleep[[19]]$minuteData[[337]]$dateTime
[1] "18:43:00"

$sleep[[19]]$minuteData[[337]]$value
[1] "1"


$sleep[[19]]$minuteData[[338]]
$sleep[[19]]$minuteData[[338]]$dateTime
[1] "18:44:00"

$sleep[[19]]$minuteData[[338]]$value
[1] "1"


$sleep[[19]]$minuteData[[339]]
$sleep[[19]]$minuteData[[339]]$dateTime
[1] "18:45:00"

$sleep[[19]]$minuteData[[339]]$value
[1] "1"


$sleep[[19]]$minuteData[[340]]
$sleep[[19]]$minuteData[[340]]$dateTime
[1] "18:46:00"

$sleep[[19]]$minuteData[[340]]$value
[1] "1"


$sleep[[19]]$minuteData[[341]]
$sleep[[19]]$minuteData[[341]]$dateTime
[1] "18:47:00"

$sleep[[19]]$minuteData[[341]]$value
[1] "3"


$sleep[[19]]$minuteData[[342]]
$sleep[[19]]$minuteData[[342]]$dateTime
[1] "18:48:00"

$sleep[[19]]$minuteData[[342]]$value
[1] "1"


$sleep[[19]]$minuteData[[343]]
$sleep[[19]]$minuteData[[343]]$dateTime
[1] "18:49:00"

$sleep[[19]]$minuteData[[343]]$value
[1] "1"


$sleep[[19]]$minuteData[[344]]
$sleep[[19]]$minuteData[[344]]$dateTime
[1] "18:50:00"

$sleep[[19]]$minuteData[[344]]$value
[1] "1"


$sleep[[19]]$minuteData[[345]]
$sleep[[19]]$minuteData[[345]]$dateTime
[1] "18:51:00"

$sleep[[19]]$minuteData[[345]]$value
[1] "1"


$sleep[[19]]$minuteData[[346]]
$sleep[[19]]$minuteData[[346]]$dateTime
[1] "18:52:00"

$sleep[[19]]$minuteData[[346]]$value
[1] "1"


$sleep[[19]]$minuteData[[347]]
$sleep[[19]]$minuteData[[347]]$dateTime
[1] "18:53:00"

$sleep[[19]]$minuteData[[347]]$value
[1] "1"


$sleep[[19]]$minuteData[[348]]
$sleep[[19]]$minuteData[[348]]$dateTime
[1] "18:54:00"

$sleep[[19]]$minuteData[[348]]$value
[1] "1"


$sleep[[19]]$minuteData[[349]]
$sleep[[19]]$minuteData[[349]]$dateTime
[1] "18:55:00"

$sleep[[19]]$minuteData[[349]]$value
[1] "1"


$sleep[[19]]$minuteData[[350]]
$sleep[[19]]$minuteData[[350]]$dateTime
[1] "18:56:00"

$sleep[[19]]$minuteData[[350]]$value
[1] "1"


$sleep[[19]]$minuteData[[351]]
$sleep[[19]]$minuteData[[351]]$dateTime
[1] "18:57:00"

$sleep[[19]]$minuteData[[351]]$value
[1] "1"


$sleep[[19]]$minuteData[[352]]
$sleep[[19]]$minuteData[[352]]$dateTime
[1] "18:58:00"

$sleep[[19]]$minuteData[[352]]$value
[1] "1"


$sleep[[19]]$minuteData[[353]]
$sleep[[19]]$minuteData[[353]]$dateTime
[1] "18:59:00"

$sleep[[19]]$minuteData[[353]]$value
[1] "1"


$sleep[[19]]$minuteData[[354]]
$sleep[[19]]$minuteData[[354]]$dateTime
[1] "19:00:00"

$sleep[[19]]$minuteData[[354]]$value
[1] "1"


$sleep[[19]]$minuteData[[355]]
$sleep[[19]]$minuteData[[355]]$dateTime
[1] "19:01:00"

$sleep[[19]]$minuteData[[355]]$value
[1] "1"


$sleep[[19]]$minuteData[[356]]
$sleep[[19]]$minuteData[[356]]$dateTime
[1] "19:02:00"

$sleep[[19]]$minuteData[[356]]$value
[1] "1"


$sleep[[19]]$minuteData[[357]]
$sleep[[19]]$minuteData[[357]]$dateTime
[1] "19:03:00"

$sleep[[19]]$minuteData[[357]]$value
[1] "1"


$sleep[[19]]$minuteData[[358]]
$sleep[[19]]$minuteData[[358]]$dateTime
[1] "19:04:00"

$sleep[[19]]$minuteData[[358]]$value
[1] "1"


$sleep[[19]]$minuteData[[359]]
$sleep[[19]]$minuteData[[359]]$dateTime
[1] "19:05:00"

$sleep[[19]]$minuteData[[359]]$value
[1] "1"


$sleep[[19]]$minuteData[[360]]
$sleep[[19]]$minuteData[[360]]$dateTime
[1] "19:06:00"

$sleep[[19]]$minuteData[[360]]$value
[1] "3"


$sleep[[19]]$minuteData[[361]]
$sleep[[19]]$minuteData[[361]]$dateTime
[1] "19:07:00"

$sleep[[19]]$minuteData[[361]]$value
[1] "1"


$sleep[[19]]$minuteData[[362]]
$sleep[[19]]$minuteData[[362]]$dateTime
[1] "19:08:00"

$sleep[[19]]$minuteData[[362]]$value
[1] "1"


$sleep[[19]]$minuteData[[363]]
$sleep[[19]]$minuteData[[363]]$dateTime
[1] "19:09:00"

$sleep[[19]]$minuteData[[363]]$value
[1] "1"


$sleep[[19]]$minuteData[[364]]
$sleep[[19]]$minuteData[[364]]$dateTime
[1] "19:10:00"

$sleep[[19]]$minuteData[[364]]$value
[1] "1"


$sleep[[19]]$minuteData[[365]]
$sleep[[19]]$minuteData[[365]]$dateTime
[1] "19:11:00"

$sleep[[19]]$minuteData[[365]]$value
[1] "1"


$sleep[[19]]$minuteData[[366]]
$sleep[[19]]$minuteData[[366]]$dateTime
[1] "19:12:00"

$sleep[[19]]$minuteData[[366]]$value
[1] "1"


$sleep[[19]]$minuteData[[367]]
$sleep[[19]]$minuteData[[367]]$dateTime
[1] "19:13:00"

$sleep[[19]]$minuteData[[367]]$value
[1] "1"


$sleep[[19]]$minuteData[[368]]
$sleep[[19]]$minuteData[[368]]$dateTime
[1] "19:14:00"

$sleep[[19]]$minuteData[[368]]$value
[1] "1"


$sleep[[19]]$minuteData[[369]]
$sleep[[19]]$minuteData[[369]]$dateTime
[1] "19:15:00"

$sleep[[19]]$minuteData[[369]]$value
[1] "1"


$sleep[[19]]$minuteData[[370]]
$sleep[[19]]$minuteData[[370]]$dateTime
[1] "19:16:00"

$sleep[[19]]$minuteData[[370]]$value
[1] "1"


$sleep[[19]]$minuteData[[371]]
$sleep[[19]]$minuteData[[371]]$dateTime
[1] "19:17:00"

$sleep[[19]]$minuteData[[371]]$value
[1] "1"


$sleep[[19]]$minuteData[[372]]
$sleep[[19]]$minuteData[[372]]$dateTime
[1] "19:18:00"

$sleep[[19]]$minuteData[[372]]$value
[1] "3"


$sleep[[19]]$minuteData[[373]]
$sleep[[19]]$minuteData[[373]]$dateTime
[1] "19:19:00"

$sleep[[19]]$minuteData[[373]]$value
[1] "1"


$sleep[[19]]$minuteData[[374]]
$sleep[[19]]$minuteData[[374]]$dateTime
[1] "19:20:00"

$sleep[[19]]$minuteData[[374]]$value
[1] "1"


$sleep[[19]]$minuteData[[375]]
$sleep[[19]]$minuteData[[375]]$dateTime
[1] "19:21:00"

$sleep[[19]]$minuteData[[375]]$value
[1] "1"


$sleep[[19]]$minuteData[[376]]
$sleep[[19]]$minuteData[[376]]$dateTime
[1] "19:22:00"

$sleep[[19]]$minuteData[[376]]$value
[1] "1"


$sleep[[19]]$minuteData[[377]]
$sleep[[19]]$minuteData[[377]]$dateTime
[1] "19:23:00"

$sleep[[19]]$minuteData[[377]]$value
[1] "1"


$sleep[[19]]$minuteData[[378]]
$sleep[[19]]$minuteData[[378]]$dateTime
[1] "19:24:00"

$sleep[[19]]$minuteData[[378]]$value
[1] "1"



$sleep[[19]]$minutesAfterWakeup
[1] 0

$sleep[[19]]$minutesAsleep
[1] 327

$sleep[[19]]$minutesAwake
[1] 51

$sleep[[19]]$minutesToFallAsleep
[1] 0

$sleep[[19]]$restlessCount
[1] 5

$sleep[[19]]$restlessDuration
[1] 7

$sleep[[19]]$startTime
[1] "2020-12-04T13:07:00.000"

$sleep[[19]]$timeInBed
[1] 378

I am sharing a minimal sample of json.sleep as proposed:

    value = "1"), list(dateTime = "02:43:30", value = "1"), 
        list(dateTime = "02:44:30", value = "1"), list(dateTime = "02:45:30", 
            value = "1"), list(dateTime = "02:46:30", value = "1"), 
        list(dateTime = "02:47:30", value = "1"), list(dateTime = "02:48:30", 
            value = "1"), list(dateTime = "02:49:30", value = "1"), 
        list(dateTime = "02:50:30", value = "1"), list(dateTime = "02:51:30", 
            value = "1"), list(dateTime = "02:52:30", value = "1"), 
        list(dateTime = "02:53:30", value = "1"), list(dateTime = "02:54:30", 
            value = "1"), list(dateTime = "02:55:30", value = "1"), 
        list(dateTime = "02:56:30", value = "1"), list(dateTime = "02:57:30", 
            value = "1"), list(dateTime = "02:58:30", value = "1"), 
        list(dateTime = "02:59:30", value = "1"), list(dateTime = "03:00:30", 
            value = "1"), list(dateTime = "03:01:30", value = "1"), 
        list(dateTime = "03:02:30", value = "1"), list(dateTime = "03:03:30", 
            value = "1"), list(dateTime = "03:04:30", value = "1"), 
        list(dateTime = "03:05:30", value = "1"), list(dateTime = "03:06:30", 
            value = "1"), list(dateTime = "03:07:30", value = "1"), 
        list(dateTime = "03:08:30", value = "1"), list(dateTime = "03:09:30", 
            value = "1"), list(dateTime = "03:10:30", value = "1"), 
        list(dateTime = "03:11:30", value = "1"), list(dateTime = "03:12:30", 
            value = "1"), list(dateTime = "03:13:30", value = "1"), 
        list(dateTime = "03:14:30", value = "1"), list(dateTime = "03:15:30", 
            value = "1"), list(dateTime = "03:16:30", value = "1"), 
        list(dateTime = "03:17:30", value = "1"), list(dateTime = "03:18:30", 
            value = "1"), list(dateTime = "03:19:30", value = "1"), 
        list(dateTime = "03:20:30", value = "1"), list(dateTime = "03:21:30", 
            value = "1"), list(dateTime = "03:22:30", value = "1"), 
        list(dateTime = "03:23:30", value = "1"), list(dateTime = "03:24:30", 
            value = "1"), list(dateTime = "03:25:30", value = "1"), 
        list(dateTime = "03:26:30", value = "1"), list(dateTime = "03:27:30", 
            value = "1"), list(dateTime = "03:28:30", value = "1"), 
        list(dateTime = "03:29:30", value = "1"), list(dateTime = "03:30:30", 
            value = "1"), list(dateTime = "03:31:30", value = "1"), 
        list(dateTime = "03:32:30", value = "1"), list(dateTime = "03:33:30", 
            value = "1"), list(dateTime = "03:34:30", value = "1"), 
        list(dateTime = "03:35:30", value = "1"), list(dateTime = "03:36:30", 
            value = "1"), list(dateTime = "03:37:30", value = "1"), 
        list(dateTime = "03:38:30", value = "1"), list(dateTime = "03:39:30", 
            value = "1"), list(dateTime = "03:40:30", value = "1"), 
        list(dateTime = "03:41:30", value = "1"), list(dateTime = "03:42:30", 
            value = "1"), list(dateTime = "03:43:30", value = "1"), 
        list(dateTime = "03:44:30", value = "1"), list(dateTime = "03:45:30", 
            value = "1"), list(dateTime = "03:46:30", value = "1"), 
        list(dateTime = "03:47:30", value = "1"), list(dateTime = "03:48:30", 
            value = "1"), list(dateTime = "03:49:30", value = "1"), 
        list(dateTime = "03:50:30", value = "1"), list(dateTime = "03:51:30", 
            value = "1"), list(dateTime = "03:52:30", value = "1"), 
        list(dateTime = "03:53:30", value = "1"), list(dateTime = "03:54:30", 
            value = "1"), list(dateTime = "03:55:30", value = "1"), 
        list(dateTime = "03:56:30", value = "1"), list(dateTime = "03:57:30", 
            value = "1"), list(dateTime = "03:58:30", value = "1"), 
        list(dateTime = "03:59:30", value = "1"), list(dateTime = "04:00:30", 
            value = "1"), list(dateTime = "04:01:30", value = "1"), 
        list(dateTime = "04:02:30", value = "1"), list(dateTime = "04:03:30", 
            value = "1"), list(dateTime = "04:04:30", value = "1"), 
        list(dateTime = "04:05:30", value = "1"), list(dateTime = "04:06:30", 
            value = "1"), list(dateTime = "04:07:30", value = "1"), 
        list(dateTime = "04:08:30", value = "1"), list(dateTime = "04:09:30", 
            value = "1"), list(dateTime = "04:10:30", value = "1"), 
        list(dateTime = "04:11:30", value = "1"), list(dateTime = "04:12:30", 
            value = "1"), list(dateTime = "04:13:30", value = "1"), 
        list(dateTime = "04:14:30", value = "1"), list(dateTime = "04:15:30", 
            value = "1"), list(dateTime = "04:16:30", value = "1"), 
        list(dateTime = "04:17:30", value = "1"), list(dateTime = "04:18:30", 
            value = "1"), list(dateTime = "04:19:30", value = "1"), 
        list(dateTime = "04:20:30", value = "1"), list(dateTime = "04:21:30", 
            value = "1"), list(dateTime = "04:22:30", value = "1"), 
        list(dateTime = "04:23:30", value = "1"), list(dateTime = "04:24:30", 
            value = "1"), list(dateTime = "04:25:30", value = "1"), 
        list(dateTime = "04:26:30", value = "1"), list(dateTime = "04:27:30", 
            value = "1"), list(dateTime = "04:28:30", value = "1"), 
        list(dateTime = "04:29:30", value = "1"), list(dateTime = "04:30:30", 
            value = "1"), list(dateTime = "04:31:30", value = "1"), 
        list(dateTime = "04:32:30", value = "1"), list(dateTime = "04:33:30", 
            value = "1"), list(dateTime = "04:34:30", value = "1"), 
        list(dateTime = "04:35:30", value = "1"), list(dateTime = "04:36:30", 
            value = "1"), list(dateTime = "04:37:30", value = "1"), 
        list(dateTime = "04:38:30", value = "1"), list(dateTime = "04:39:30", 
            value = "1"), list(dateTime = "04:40:30", value = "1"), 
        list(dateTime = "04:41:30", value = "1"), list(dateTime = "04:42:30", 
            value = "1"), list(dateTime = "04:43:30", value = "1"), 
        list(dateTime = "04:44:30", value = "1"), list(dateTime = "04:45:30", 
            value = "1"), list(dateTime = "04:46:30", value = "1"), 
        list(dateTime = "04:47:30", value = "1"), list(dateTime = "04:48:30", 
            value = "1"), list(dateTime = "04:49:30", value = "1"), 
        list(dateTime = "04:50:30", value = "1"), list(dateTime = "04:51:30", 
            value = "1"), list(dateTime = "04:52:30", value = "1"), 
        list(dateTime = "04:53:30", value = "1"), list(dateTime = "04:54:30", 
            value = "1"), list(dateTime = "04:55:30", value = "1"), 
        list(dateTime = "04:56:30", value = "1"), list(dateTime = "04:57:30", 
            value = "1"), list(dateTime = "04:58:30", value = "1"), 
        list(dateTime = "04:59:30", value = "1"), list(dateTime = "05:00:30", 
            value = "1"), list(dateTime = "05:01:30", value = "1"), 
        list(dateTime = "05:02:30", value = "1"), list(dateTime = "05:03:30", 
            value = "1"), list(dateTime = "05:04:30", value = "1"), 
        list(dateTime = "05:05:30", value = "1"), list(dateTime = "05:06:30", 
            value = "1"), list(dateTime = "05:07:30", value = "1"), 
        list(dateTime = "05:08:30", value = "1"), list(dateTime = "05:09:30", 
            value = "1"), list(dateTime = "05:10:30", value = "1"), 
        list(dateTime = "05:11:30", value = "1"), list(dateTime = "05:12:30", 
            value = "1"), list(dateTime = "05:13:30", value = "1"), 
        list(dateTime = "05:14:30", value = "1"), list(dateTime = "05:15:30", 
            value = "1"), list(dateTime = "05:16:30", value = "1"), 
        list(dateTime = "05:17:30", value = "1"), list(dateTime = "05:18:30", 
            value = "1"), list(dateTime = "05:19:30", value = "1"), 
        list(dateTime = "05:20:30", value = "1"), list(dateTime = "05:21:30", 
            value = "1"), list(dateTime = "05:22:30", value = "1"), 
        list(dateTime = "05:23:30", value = "1"), list(dateTime = "05:24:30", 
            value = "1"), list(dateTime = "05:25:30", value = "1"), 

The problem is this part of the function: new.day.dataset <- json.sleep$sleep$Data$minuteData Specifically, Data. All I need is the minuteData. When I try to rename multiple nested lists using this code:

for (i in 1:length(json.sleep)) {
  names(json.sleep$sleep) <- "Data"
}

only the first list is named Data but the other are unknown.

If I use this code:

names(json.sleep$sleep) <- paste0("Data",1:length(json.sleep$sleep))

there is a problem when I am trying to apply the function over all lists (i.e., Data1, Data2, Data3,...)

Is there any potentially elegant solution for solving this problem? As I am fairly new to R, and never worked with lists, nested lists are too complex to wrap my head around this problem at this point in time.

CodePudding user response:

If your input list, say: the_list, indeed has the structure of your edit, i. e. an unnamed umbrella list containing named lists with elements "dateTime" and "value", it's readily converted to a dataframe using package {purrr}:

## example list:
the_list  <- list(
  list(dateTime = "05:08:30", value = "1"), 
  list(dateTime = "05:09:30", value = "1"),
  list(dateTime = "05:10:30", value = "1")
)

convert to dataframe of columns "dateTime" and "value":

library(purrr)

map_dfr(the_list, ~ .x) ## .x is shorthand for the element being mapped

... where _dfr stands for data frame rows, a convenience extension of simple map

a somewhat more elaborated mapping for demonstration:

map_dfr(the_list,
        ~ list('how late was it?' = paste('the time was', .x$dateTime),
            'and the value was half of' = .x$value * 2
            )
        )

output (of latter):

## # A tibble: 3 x 2
##   `how late was it?`    `and the value was half of`
##   <chr>                                       <dbl>
## 1 the time was 05:08:30                           2
## 2 the time was 05:09:30                           2
## 3 the time was 05:10:30                           2

CodePudding user response:

Thank you @I_O for pointing me in the other direction, I just needed to think of another solution. This did the trick for me. The problem and solution is located in this part of the function:

json.sleep <- content(query.response, "parsed")
      
      names(json.sleep$sleep)[1] <- "Data"
      
      new.day.dataset <- json.sleep$sleep$Data$minuteData

After some thinking and searching, I found an answer of directly converting the api response to a dataframe by using this line of code:

# get the json response for sleep
      
json.sleep <- fromJSON(httr::content(query.response, "text"), flatten = TRUE)
# directly convert api response to dataframe
      
new.day.dataset <- json.sleep$sleep$minuteData

Also, later I just changed the byrow argument to FALSE in this line of code:

new.day <- data.frame(matrix(unlist(new.day.dataset), ncol = 2, byrow = FALSE), stringsAsFactors = FALSE)

This is the final function:

getSleep <- function(auth.code, start.date, end.date){
  start = as.Date(start.date)
  end = as.Date(end.date)
  #create dataframe
  all.days = data.frame(Time = c(),Sleep = c())
  #loop through every day
  current.date = start
  while (current.date <= end)
  {
    query.response <- GET(paste0("https://api.fitbit.com/1/user/-/sleep/date/",
                                 format(current.date),
                                 ".json"),
                          add_headers(
                            Authorization = auth.code)
    )
    #check for error
    if(query.response$status_code == 200){
      
      # get the json response for sleep
      
      json.sleep <- fromJSON(httr::content(query.response, "text"), flatten = TRUE)
      # directly convert api response to dataframe
      
      new.day.dataset <- json.sleep$sleep$minuteData
      
      #
      if(is.null(new.day.dataset) || length(new.day.dataset) == 0){
        print(paste(
          "No data found on ",
          format(current.date),
          "skipping date and moving on"
        ))
        current.date <- current.date   1
        next
      }
      new.day <- data.frame(matrix(unlist(new.day.dataset), ncol = 2, byrow = FALSE), stringsAsFactors = FALSE)
      
      new.day <- new.day %>% rename(Time = X1, Sleep = X2) %>% 
        mutate(Time = ymd_hms(paste(format(current.date), Time)),
               Sleep = as.numeric(Sleep))
      
      all.days <- rbind(all.days, new.day)
      
      current.date <- current.date   1     
    }
    #we errored
    else{
      print(paste(
        "Error on ",
        format(current.date)
      ))
      #What was the error?
      print(query.response)
      #rate limit error?
      if(query.response$status_code == 429){
        print("Hit rate limit, waiting 1 hour")
        Sys.sleep(3600)
      }
      else{
        return(all.days)
        break
      }
      
    }
    
  }
  return(all.days)
}

With this function I got the desired output (dataframe):

'data.frame':   8939 obs. of  2 variables:
 $ Time : POSIXct, format: "2020-12-04 13:07:00" "2020-12-04 13:08:00" "2020-12-04 13:09:00" ...
 $ Sleep: num  1 1 1 1 1 1 1 1 1 1 ...
  • Related