0

I am trying to convert around 200 lines of json strings into dataframe in R. I end up getting default column names in the data frame instead of the ones mentioned in the list. Here is my code:

 out <- lapply(readLines("exampleFile.json", n=200), fromJSON) // Used to convert json into a list
 test <- data.frame(t(sapply(out,c))) //convert the list into a dataframe

Attached is the sceenshot of the results of the variables 'out' enter image description here

Attached is the sceenshot of the results of the variables 'test' enter image description here

Anyhelp is appreciated!

Justin
  • 321
  • 1
  • 5
  • 19
  • can you post `dput(lapply(out[1:4], head))` ? – Ronak Shah May 09 '19 at 07:40
  • list(list(banner_id = 261, bid_id = "1551", bid_price = 0.03, browser = "Chrome", campaign_id = 33L, city = "Raa"), list(banner_id = 26L, bid_id = "155", bid_price = 0.037, browser = "Chrome", campaign_id = 3395L, city = "Ein"), l list(banner_id = 26, bid_id = "1", bid_price = 0.03, browser = "Safari", campaign_id = 33, city = "Hee"), list(banner_id = -1L, bid_id = "1551", bid_price = 0, browser = "Chrome", campaign_id = -1L, city = "Eng")) – Justin May 09 '19 at 07:54
  • Try `do.call(rbind.data.frame, out)` – Ronak Shah May 09 '19 at 07:59
  • It says: invalid list argument: all variables should have the same length – Justin May 09 '19 at 08:57
  • 1
    If you have unequal length , Try `data.table::rbindlist(out, fill = TRUE)` – Ronak Shah May 09 '19 at 09:01
  • It did work! Thank you. Can you please post this as an answer, so that I can accept it! – Justin May 09 '19 at 09:12
  • Thanks, however this has been asked before so I marked it as duplicate of that post. Hope that is fine. – Ronak Shah May 09 '19 at 09:19

0 Answers0