Data Harvesting in R: Get nested lists, unlist, make edits, re-nest them back -
the following code harvests data website. retrieve list of lists, want unlist 1 of lists, edit it, re-nest data form data received. here code below, fails 1 re-nesting.
library(jsonlite) library(plyr) library(ckanr) library(purrr) library(dplyr) ckanr_setup(url = "https://energydata.info/") package_search(q = 'organization:world-bank-grou')$count json_data2 <- fromjson("https://energydata.info/api/3/action/package_search?q=organization:world-bank-grou", flatten = true) dat2 <- json_data2$result str(dat2) ########### #get datasets , unlist metadata ########### df <- as.data.frame(json_data2$result$results) tags <- select(df, id, topic) #make edits tags$topic <- tolower(tags$topic) res <- rbind.fill(lapply(tags,function(y){as.data.frame(t(y),stringsasfactors=false)})) res$v1 = paste0("some edit:",res$v1) res$v2 = paste0("some edits:", res$v2) res$v3 = paste0("some edit:", res$v3) res[res=="some edit:na"]<-na res$v1 <- gsub(" ", "_", res$v1) res$v2 <- gsub(" ", "_", res$v2) res$v3 <- sub(" ", "_", res$v3) res ########### #re-nest ########### #turning res df list of lists nestedlist <- flatten(by_row(res, ..f = function(x) flatten_chr(x), .labels = false)) #fails here error: error in flatten(by_row(res, ..f = function(x) flatten_chr(x), .labels = false)) : not find function "by_row"
unclear question wording kind of list of lists want end with, maybe you're looking for?
res %>% rowwise() %>% as.list() or
res %>% t() %>% as.data.frame() %>% rowwise() %>% as.list()
Comments
Post a Comment