将json数据转换为dataframe并将其保存为csv格式

时间:2015-03-24 20:20:51

标签: r

我已使用此answer将json转换为数据帧,但似乎无法将其实现到我的数据中。

我已从here下载文件。正如标题所述,他们是json fies。我想阅读它们,使它们成为数据帧,然后在该过程之后将它们保存为csv格式。

因为文件太大我在JavaScript字段中有一个实验的数据片段here我在下面的代码中使用这个json作为in.json文件。

对我来说奇怪的是,当我输入nrow(df)时我只收回2行,当我将其保存为csv格式时,我可以看到许多列错误。基于id我必须有超过2行。你能帮我解决一下我做错了吗?

这是我的代码

require(RJSONIO)
require(rjson)
library("rjson")
filename2 <- "C:/Users/Desktop/in.json"
json_data <- fromJSON(file = filename2)

json_data <- lapply(json_data, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

json <- do.call("rbind", json_data)

df=json

df1 = df[df$state != "live",]

df1$projects.deadline <- as.POSIXct(df1$projects.deadline, origin="1970-01-01")
df1$projects.state_changed_at <- as.POSIXct(df1$projects.state_changed_at, origin="1970-01-01")
df1$projects.created_at <- as.POSIXct(df1$projects.created_at, origin="1970-01-01")
df1$projects.launched_at <- as.POSIXct(df1$projects.launched_at, origin="1970-01-01")

df2 = df1[,c("projects.id","projects.name","projects.blurb","projects.goal","projects.pledged","projects.state","projects.slug","projects.country","projects.currency","projects.currency_trailing_code","projects.deadline","projects.state_changed_at","projects.created_at","projects.launched_at","projects.backers_count","projects.creator.name","projects.location.id","projects.location.short_name","projects.location.displayable_name","projects.location.country","projects.category.id","projects.category.name","projects.id.1","projects.location.urls.api.nearby_projects.1","projects.category.id.1")]

df3 = df2[!duplicated(df2), ]

write.csv(df3,file='C:/Users/Desktop/final.csv', row.names=FALSE)

请问您能帮助我解决前面步骤中的错误吗?

1 个答案:

答案 0 :(得分:1)

听起来好像您的数据框由两行列表组成。尝试使用apply函数来删除数据框中的每一行。