毕业论文,需要搜集国际贸易方面的数据,UNcomtrade上面数据量太大了不能直接下载,所以打算用R把数据弄下来
本科没学过R,真的小白,想请问下照着下面的代码可以加载出UNcomtrade的数据,但是当我把Classification Code从“TOTAL”改成“ALL”(因为要所有商品的数据)之后,就显示不能下载(具体原因如图)。查了一下说是当前json字符串有误,没有正常拼接。想请教一下各位为什么改了之后就没办法显示数据了,且应该怎么调整
下面是从的代码:
library(rjson)
string <- "http://comtrade.un.org/data/cache/partnerAreas.json"
reporters <- fromJSON(file=string)
reporters <- as.data.frame(t(sapply(reporters$results,rbind)))
get.Comtrade <- function(url="http://comtrade.un.org/api/get?"
,maxrec=50000
,type="C"
,freq="A"
,px="HS"
,ps="1993"
,r
,p
,rg="all"
,cc="ALL"(这里之前是TOTAL,可以显示数据)
,fmt="json"
)
{
string<- paste(url
,"max=",maxrec,"&" #maximum no. of records returned
,"type=",type,"&" #type of trade (c=commodities)
,"freq=",freq,"&" #frequency
,"px=",px,"&" #classification
,"ps=",ps,"&" #time period
,"r=",r,"&" #reporting area
,"p=",p,"&" #partner country
,"rg=",rg,"&" #trade flow
,"cc=",cc,"&" #classification code
,"fmt=",fmt #Format
,sep = ""
)
if(fmt == "csv") {
raw.data<- read.csv(string,header=TRUE)
return(list(validation=NULL, data=raw.data))
} else {
if(fmt == "json" ) {
raw.data<- fromJSON(file=string)
data<- raw.data$dataset
validation<- unlist(raw.data$validation, recursive=TRUE)
ndata<- NULL
if(length(data)> 0) {
var.names<- names(data[[1]])
data<- as.data.frame(t( sapply(data,rbind)))
ndata<- NULL
for(i in 1:ncol(data)){
data[sapply(data[,i],is.null),i]<- NA
ndata<- cbind(ndata, unlist(data[,i]))
}
ndata<- as.data.frame(ndata)
colnames(ndata)<- var.names
}
return(list(validation=validation,data =ndata))
}
}
}
library("rjson")
s1 <- get.Comtrade(r="156", p="0")