0

以下代码从网站收集数据。我检索列表列表,我想取消列出其中一个列表,对其进行编辑,然后将其重新嵌套到数据中,以接收到数据的形式。这是我下面的代码,它无法重新嵌套。

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 the datasets and unlist metadata
###########

df <- as.data.frame(json_data2$result$results)
Tags <- select(df, id, topic)

#Make some 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 back into list of lists
nestedList <- flatten(by_row(res, ..f = function(x) flatten_chr(x), .labels = FALSE)) #FAILS HERE

错误:flatten(by_row(res, ..f = function(x) flatten_chr(x), .labels = FALSE)) 中的错误:找不到函数“by_row”

4

1 回答 1

0

从问题措辞中不清楚您想要最终得到什么样的列表列表,但也许这就是您正在寻找的?

res %>%
  rowwise() %>% 
  as.list()

或者

res %>%
  t() %>%
  as.data.frame() %>%
  rowwise() %>% 
  as.list()
于 2017-08-15T13:35:42.700 回答