0

I am pulling individual logs in that show changes in production tanks through an API. When trying to create on data frame with all of these logs I have been running into various issues. Below I have a section of my code:

Binded_TL<-do.call(rbind,TL_JSON_TEXT1)
TL_JSON <-purrr::map(Binded_TL, jsonlite::fromJSON)
TL_JSON2 <- TL_JSON[[1]]$Data

I have no issue with the above code, TL_JSON2 prints as a data frame with the correct headers but when I run TL_JSON2 as a for loop to try and combine them all:

for (i in 1:length(TL_JSON_TEXT1)){
  TL_JSON2[[i]] <- as.data.frame(TL_JSON[[i]]$Data)
  
}

Is where I am running into an issue. Not sure if for loop is the way to go or if I should be doing something completely different.

I have tried the following:

TL_JSON2 <- data.frame()
for (i in 1:length(TL_JSON)){
  TL_JSON2[[i]] <- paste0(TL_JSON[[i]]$Data)}

But I get the error of "replacement has 43 rows, data has 0"

Reproducible code

tank1 <- data.frame(TankName = c("tank1", "tank1", "tank1"), Capacity = c(100,100,100), PercentFull = c(10,13,20), Date = c("1/2/22", "1/3/22", "1/5/22"))
tank2 <- data.frame(TankName = c("tank2"), Capacity = c(200), PercentFull= c(50), Date = c("2/7/22"))
tank3 <- data.frame(TankName = c("tank3", "tank3"), Capacity = c(300, 300), PercentFull = c(80, 60), Date = c("1/3/22","1/6/22"))

Nested_DF <- list(tank1, tank2, tank3)

I have something similar to the Nested_DF and I am trying to create a combined df that looks like

  TankName Capacity PercentFull   Date
1    tank1      100          10 1/2/22
2    tank1      100          13 1/3/22
3    tank1      100          20 1/5/22
4    tank2      200          50 2/7/22
5    tank3      300          80 1/3/22
6    tank3      300          60 1/6/22
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please don't post images of data because we cannot copy/paste that into R for testing. – MrFlick Mar 30 '22 at 23:20
  • +1 for a reproducible example. That being said my strategy with json data now is always to put the list into a tibble (using `enframe`) and then unnest it using some combination of `unnest, unnest_longer or unnest_wider`. See for example https://stackoverflow.com/a/71644484/18605835 – mkpt_uk Mar 31 '22 at 05:53
  • Edited to add reproducible code. – Julia Sawin Apr 13 '22 at 17:09

0 Answers0