7

私は EUROSTAT からのデータをよく使用しますが、データを R に直接ロードできないことは非常に面倒でした。EUROSTAT http://epp.eurostat.ec からの一括ダウンロード機能によって提供されるデータセットを取得するために、このスニペットを作成しました。 europa.eu/NavTree_prod/everybody/BulkDownloadListing?sort=1&dir=dic%2Fen

より良い方法はありますか?..これは私のために働いた:

    #this library is used to download data from eurostat and to find datasets
#later extend to extend to find datasets with certain dimensions

#download data from eurostat
#unpack and convert to dataframe
#load label descriptions
#load factors
#save as r data object

datasetname="ebd_all"

LANGUAGE="en"

install.packages("RCurl")
library(RCurl)
library(data.table)
library(reshape)
library(stringr)

baseurl="http://epp.eurostat.ec.europa.eu/NavTree_prod/everybody/BulkDownloadListing?sort=1&file=data%2F"

fullfilename=paste(datasetname,".tsv.gz",sep="")
temp <- paste(tempfile(),".gz",sep="")
download.file(paste(baseurl,fullfilename,sep=""),temp)
dataconnection <- gzfile(temp)
d=read.delim(dataconnection)
longdata=melt(d,id=colnames(d)[1])

firstname=colnames(d)[1] # remove .time and count how many headings are there 
firstname=substr(firstname,1,nchar(firstname)-nchar(".time"))
headings=toupper(strsplit(firstname,".",fixed=TRUE)[[1]])
headingcount=length(headings)
colnames(longdata)=c("dimensions","time","value")


#get the data on the dimension tables
df=data.frame(dimensions=as.character(longdata[,"dimensions"]))
df = transform(df, dimensions= colsplit(dimensions, split = "\\,",names=headings))
dimensions=data.table(df$dimensions)

#download the dimension labels - save headings as better variable
dimfile=paste("http://epp.eurostat.ec.europa.eu/NavTree_prod/everybody/BulkDownloadListing?sort=1&file=dic%2F",LANGUAGE,"%2Fdimlst.dic",sep="")

temp <- paste(tempfile(),".gz",sep="")
download.file(dimfile,temp)
dataconnection <- gzfile(temp)
dimdata=read.delim(dataconnection,header=FALSE)
colnames(dimdata)=c("colname","desc")
lab=dimdata$desc
names(lab)=dimdata$colname

#create  headings that speak for themselves for columns
speakingheadings=as.character(lab[headings])

#download factors for each heading and add
for(heading in headings){
  factorfile=paste("http://epp.eurostat.ec.europa.eu/NavTree_prod/everybody/BulkDownloadListing?sort=1&file=dic%2F",LANGUAGE,"%2F",tolower(heading),".dic",sep="")
  temp <- paste(tempfile(),".gz",sep="")
  download.file(factorfile,temp)
  dataconnection <- gzfile(temp)
  factordata=read.delim(dataconnection,header=FALSE)
  colnames(factordata)=c(heading,paste(heading,"_desc",sep=""))
  #join the heading to the heading dataset
  dimensions=merge(dimensions,factordata,by=heading,all.x=TRUE)
}


  #at the end at speaking headings
setnames(dimensions,colnames(dimensions)[1:length(speakingheadings)],speakingheadings)

  #add data columns by writing and reading again---FASTER ;-)
temp=tempfile()
values=data.frame(value=as.character(longdata$value))
values = transform(values, value= colsplit(value, split = "\\ ",names=c("value","flag")))
values=values$value
values=data.table(values)

values$value=as.character(values$value)
values$flag=as.character(values$flag)
values[value==flag,flag:=NA]
values$value=as.double(values$value)

eurostatdata=cbind(dimensions,time=longdata$time,values) 
save(eurostatdata,file=paste(datasetname,".RData"))
4

3 に答える 3

7

SmarterPolandパッケージをチェックしてください。EUROSTATから直接データをダウンロードする (そして R に入る) 関数があります。

ここに例があります:

library(SmarterPoland)
# info about passagers
grepEurostatTOC("split of passenger transport")
## get table
tmp <- getEurostatRCV("tsdtr210")
summary(tmp)

##     vehicle         geo            time          value     
##  BUS_TOT:756   AT     :  63   1990   : 108   Min.   : 0.0  
##  CAR    :756   BE     :  63   1991   : 108   1st Qu.: 6.9  
##  TRN    :756   BG     :  63   1992   : 108   Median :12.9  
##                CH     :  63   1993   : 108   Mean   :33.6  
##                CY     :  63   1994   : 108   3rd Qu.:77.4  
##                CZ     :  63   1995   : 108   Max.   :93.4  
##                (Other):1890   (Other):1620   NA's   :397

ソース: www.smarterpoland.pl

于 2012-11-19T13:57:25.600 に答える
2

修正された回答

RJSDMXパッケージを使用して、Eurostat から R にデータを取得することもできます。以下に例を示します。

library(RJSDMX)
data <- getTimeSeries("EUROSTAT","nama_gdp_c/.EUR_HAB.B1GM.DE")

最初の回答(参照用にここに残されています)

より良い方法は、Eurostat データへのプログラムによるアクセスを提供するEurostat Web サービス機能を使用することです。Web サービス機能は、一括ダウンロード機能に加えて、Eurostat がデータベースからデータを抽出するために提供する別のサービスです。サービスを使用するには、REST または SOAP リクエストを送信する必要があります。サーバーから取得したデータは階層データ構造 (XML ドキュメントの一種) であり、XML パッケージを使用して解析できます。

以下の簡単な例を確認する前に、Eurotat が提供するサービスに関する情報をいくつか読むことをお勧めします。 Getting_started/a_few_useful_points

# Step 0: Load the XML package.
# This is used later on to parse the XML retrieved from Eurostat.
# For a tutorial on XML and parsing XML documents, read this: http://www.w3schools.com/xpath/default.asp

library(XML)

# Step 1: Construct the appropriate REST query.
# First read this:     http://epp.eurostat.ec.europa.eu/portal/page/portal/sdmx_web_services/getting_started/a_few_useful_points

# Specify the data to be retrieved.

resource    <- "data"
dataflow    <- "nama_gdp_c"
key         <- ".EUR_HAB.B1GM.DE"
time_filter <- "?startPeriod=2010"

# Construct the query

partial_url <- paste(paste(resource, dataflow, key, sep="/"), time_filter, sep="")
base_url    <- "http://ec.europa.eu/eurostat/SDMX/diss-web/rest/"
rest_query  <- paste(base_url, partial_url, sep="")

# Step 2: Make the request using cURL (that is, retrieve the data)
# For information about cURL, read this: http://curl.haxx.se/
# For information about the curl command, check out the man pages: http://curl.haxx.se/docs/manpage.html

command   <- paste("curl", rest_query)
raw_data  <- system(command, intern=TRUE)

# Note: at this stage, the data is a character object.

class(raw_data)

# View the data, which can be found commented out at the bottom of this script. Note that it is a hierarchical data structure. 

# Step 3: Parse the data
# Here we use functions from the XML package - one could, of course, use base package functions, but why?

data <- xmlParse(raw_data)

# Parsing the data returns an object of class: "XMLInternalDocument" and "XMLAbstractDocument"

class(data)

# Step 4: Extract the numerical data 

# Data can be found using getNodeSet(), but the data remains stuck between "tags" - we just want the numbers.

getNodeSet(data,"//generic:ObsValue")

# The numbers we want to extract are, in this case, value "attributes". We can target these values as follows.

xpathApply(data, "//generic:ObsValue", xmlGetAttr, name="value")

# That does the job, but really we'd like those numbers in a vector rather than a list object.

numbers <- as.numeric(xpathApply(data, "//generic:ObsValue", xmlGetAttr, name="value"))

# Step 5: Extract the dates (years) - i.e. get some metadata.
# This is similar to above.

years <- as.numeric(xpathApply(data, "//generic:ObsDimension", xmlGetAttr, name="value"))

# Step 6 and on-wards:
#
# Enter your code here... =)
#
#
#

参考までに、生データは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<message:GenericData xmlns:footer="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message/footer" xmlns:generic="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" xmlns:common="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:message="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <message:Header>
    <message:ID>13e08b8d24936d75b3a6fa1b9c632e22</message:ID>
    <message:Test>false</message:Test>
    <message:Prepared>2014-10-20T21:10:50</message:Prepared>
    <message:Sender id="ESTAT">
      <common:Name xml:lang="en">Eurostat</common:Name>
      <message:Timezone>+01:00</message:Timezone>
    </message:Sender>
    <message:Receiver id="RECEIVER"/>
    <message:Structure structureID="ESTAT_DSD_nama_gdp_c_1_0" dimensionAtObservation="TIME_PERIOD">
      <common:Structure>
        <Ref agencyID="ESTAT" id="DSD_nama_gdp_c" version="1.0"/>
      </common:Structure>
    </message:Structure>
    <message:DataSetAction>Append</message:DataSetAction>
    <message:DataSetID>nama_gdp_c</message:DataSetID>
  </message:Header>
  <message:DataSet structureRef="ESTAT_DSD_nama_gdp_c_1_0">
    <generic:Series>
      <generic:SeriesKey>
        <generic:Value id="UNIT" value="EUR_HAB"/>
        <generic:Value id="INDIC_NA" value="B1GM"/>
        <generic:Value id="GEO" value="DE"/>
        <generic:Value id="FREQ" value="A"/>
      </generic:SeriesKey>
      <generic:Obs>
        <generic:ObsDimension value="2013"/>
        <generic:ObsValue value="33300.0"/>
      </generic:Obs>
      <generic:Obs>
        <generic:ObsDimension value="2012"/>
        <generic:ObsValue value="32600.0"/>
      </generic:Obs>
      <generic:Obs>
        <generic:ObsDimension value="2011"/>
        <generic:ObsValue value="31900.0"/>
      </generic:Obs>
      <generic:Obs>
        <generic:ObsDimension value="2010"/>
        <generic:ObsValue value="30500.0"/>
      </generic:Obs>
    </generic:Series>
  </message:DataSet>
</message:GenericData>

注意点: 私は cURL を使用してリクエストを送信しましたが、これは他の多くの方法で行うことができました。たとえば、Wget、Perl、PHP などを使用することもできます。システムを使用する意思がある (そして使用できる) 限り。 () コマンドを使用すると、プログラムによる方法で Eurostat から R にデータを取得するのは簡単なはずです (以下の編集を参照)。たとえば、データを ts オブジェクト (または、送信するクエリによっては mts オブジェクト) に変更することも簡単です。最後に、私は Linux オペレーティング システム (Ubuntu ディストリビューション) を使用しているため、Windows を使用している場合、上記の例は機能しない可能性があります。

これが役立つことを願っています!

編集: RCurl パッケージがロードされていることに気付きました。必要に応じて、パッケージが提供する機能を、私が使用したシステム コマンドに置き換えることができます。

于 2014-10-20T22:47:03.067 に答える