0

I have a list where one of the levels is duplicative and unnecessary. The first level is a list of unique accounts, which are each lists containing fueltype, fuelunits, description, modeltypes, and usagedata. The previous four are single value descriptors, but usagedata should be a list of 12 observations. Instead, there is an additional level between usagedata and the observations. I cannot figure out how to unlist or flatten this such that usagedata is a list of 12.

Example dataset for reproduction

library(dplyr)
my_data <- data.frame(
  Id.1 = as.character(c("A", "B", "C")),
  Id.2 = as.character(c("D", "E", "F")),
  Var.1 = as.numeric(c(100,150,200)),
  Time.1 = as.character(c("Jan","Jan","Jan")),
  Var.2 = as.numeric(c(75,125,175)),
  Time.2 = as.character(c("Feb", "Feb", "Feb")),
  Var.3 = as.numeric(c(80,120,140)),
  Time.3 = as.character(c("Mar", "Mar", "Mar"))
  )

Formatting code below. I use a function to create the usage data variable list, which is assigned to df.1. I wasn't sure how to do this within the transmute function itself.

my_fun <- function(x) {
  usagedata <- vector(mode = 'list', length = 3)
  for (i in 1:3){
    temp <- x %>%
      transmute(
        Time = select(x, starts_with("Time"))[i],
        Var = max(select(x, starts_with("Var"))[i])
      ) 
    temp <- (split(temp, seq(nrow(temp))))
    usagedata[i] <- temp
  }
  return(usagedata)
} 

df.1 <- vector(mode = 'list', length = 3)
for (i in 1:3){
  temp <- my_fun(my_data[i,]) 
  df.1[[i]] <- temp
}

inputdata <- my_data %>%
  transmute(
    Name = Id.1,
    Unit = Id.2,
    usagedata = df.1
  )

inputdata <- split(inputdata, seq(nrow(inputdata)))

Output of dput(head(inputdata). You can see that there is a double list for usagedata. I need that to be a single list.

list(`1` = structure(list(Name = "A", Unit = "D", usagedata = list(
    list(structure(list(Time = structure(list(Time.1 = "Jan"), row.names = 1L, class = "data.frame"), 
        Var = 100), row.names = 1L, class = "data.frame"), structure(list(
        Time = structure(list(Time.2 = "Feb"), row.names = 1L, class = "data.frame"), 
        Var = 75), row.names = 1L, class = "data.frame"), structure(list(
        Time = structure(list(Time.3 = "Mar"), row.names = 1L, class = "data.frame"), 
        Var = 80), row.names = 1L, class = "data.frame")))), row.names = 1L, class = "data.frame"), 
    `2` = structure(list(Name = "B", Unit = "E", usagedata = list(
        list(structure(list(Time = structure(list(Time.1 = "Jan"), row.names = 2L, class = "data.frame"), 
            Var = 150), row.names = 2L, class = "data.frame"), 
            structure(list(Time = structure(list(Time.2 = "Feb"), row.names = 2L, class = "data.frame"), 
                Var = 125), row.names = 2L, class = "data.frame"), 
            structure(list(Time = structure(list(Time.3 = "Mar"), row.names = 2L, class = "data.frame"), 
                Var = 120), row.names = 2L, class = "data.frame")))), row.names = 2L, class = "data.frame"), 
    `3` = structure(list(Name = "C", Unit = "F", usagedata = list(
        list(structure(list(Time = structure(list(Time.1 = "Jan"), row.names = 3L, class = "data.frame"), 
            Var = 200), row.names = 3L, class = "data.frame"), 
            structure(list(Time = structure(list(Time.2 = "Feb"), row.names = 3L, class = "data.frame"), 
                Var = 175), row.names = 3L, class = "data.frame"), 
            structure(list(Time = structure(list(Time.3 = "Mar"), row.names = 3L, class = "data.frame"), 
                Var = 140), row.names = 3L, class = "data.frame")))), row.names = 3L, class = "data.frame"))

Image of Nested List Hierarchy

DrSchuess
  • 11
  • 2
  • 1
    Please show the `dput(head(df2))` so that others can test it along with the expected output – akrun Oct 19 '21 at 18:46
  • 1
    Please do not upload Image of code. Why? https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – user12256545 Oct 19 '21 at 19:13
  • 1
    DrSchuess, it will help significantly if you can frame your questions in a way that facilitates us testing code using relevant/representative data. To this end, please read through https://stackoverflow.com/q/5963269 (and perhaps [mcve] and https://stackoverflow.com/tags/r/info) for good discussions on what to include, including (as akrun requested) unambiguous sample data from `dput(.)`. – r2evans Oct 19 '21 at 19:28
  • Thanks, those links were all very helpful. Let me know if the example above is suitable for testing. – DrSchuess Oct 20 '21 at 16:24

0 Answers0