2017-06-10 21 views
0

Ich habe Probleme, eine .tsv Datei in R. Die Datendatei von Eurostats ist importieren, und ist öffentlich zugänglich: http://ec.europa.eu/eurostat/en/web/products-datasets/-/MIGR_IMM10CTBImportieren einer .tsv Datei

ich den folgenden Code verwenden, um sie zu importieren:

immig <- read.table(file="immig.tsv", sep="\t", header=TRUE) 

Der Code scheint jedoch nicht zu funktionieren. Ich erhalte keine Fehlermeldungen, aber die Ausgabe sieht so aus:

> immig[1:3, 1:3] 
    age.agedef.c_birth.unit.sex.geo.time X2015 X2014 
1 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,AT 4723 4093 
2 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,BE 1017 953 
3 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,BG 559 577 

Was mache ich falsch? Ich habe versucht, stattdessen sep="," zu verwenden, aber es scheint einige Probleme zu lösen, während andere erstellt werden.

+1

Die Tabelle, die Sie sich beziehen, ist nicht eine Textdatei mit Tabulatoren getrennte Werte ... – ssp3nc3r

+0

die Datei in ein nutzbares Format hier herunterladen: http://appsso.eurostat.ec.europa.eu /nui/setupDownloads.do – ssp3nc3r

+0

Die Seite sagt "Session ungültig!" – neutral

Antwort

1

Ist das Problem, dass Sie die 2013 Daten fehlen?

heruntergeladen ich die Datei zu diesem Link, entpackt es ein Kommandozeilen-Tool verwenden, und dann kann es ganz gut importiert werden, um die readr Bibliothek:

library(readr) 

immigration <- read_tsv("~/Downloads/migr_imm10ctb.tsv", na = ":") 
#> Parsed with column specification: 
#> cols(
#> `age,agedef,c_birth,unit,sex,geo\time` = col_character(), 
#> `2015` = col_character(), 
#> `2014` = col_character(), 
#> `2013` = col_character() 
#>) 

immigration 
#> # A tibble: 45,558 x 4 
#> `age,agedef,c_birth,unit,sex,geo\\time` `2015` `2014` `2013` 
#>          <chr> <chr> <chr> <chr> 
#> 1 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,AT 4723 4093 4085 
#> 2 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,BE 1017 953 1035 
#> 3 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,BG 559 577 743 p 
#> 4 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,CH 2876 2766 2758 
#> 5 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,CY <NA> <NA>  54 
#> 6 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,CZ 120 106 155 
#> 7 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,DE <NA> <NA> 14984 
#> 8 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,DK 372 365 405 
#> 9 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,EE  23  7  16 
#> 10 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,EL <NA> <NA> 234 
#> # ... with 45,548 more rows 

Sieht aus wie es einige Ersatz Zeichen herumschweben (743 p), wo es nur Zahlen geben sollte, so müssen Sie mehr Reinigung und dann in numerische konvertieren.

library(dplyr) 
library(stringr) 

immigration %>% 
    mutate_at(vars(`2015`:`2013`), str_extract, pattern = "[0-9]+") %>% 
    mutate_at(vars(`2015`:`2013`), as.numeric) 
#> # A tibble: 45,558 x 4 
#> `age,agedef,c_birth,unit,sex,geo\\time` `2015` `2014` `2013` 
#>          <chr> <dbl> <dbl> <dbl> 
#> 1 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,AT 4723 4093 4085 
#> 2 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,BE 1017 953 1035 
#> 3 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,BG 559 577 743 
#> 4 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,CH 2876 2766 2758 
#> 5 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,CY  NA  NA  54 
#> 6 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,CZ 120 106 155 
#> 7 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,DE  NA  NA 14984 
#> 8 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,DK 372 365 405 
#> 9 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,EE  23  7  16 
#> 10 TOTAL,COMPLET,CC5_13_FOR_X_IS,NR,F,EL  NA  NA 234 
#> # ... with 45,548 more rows 

Es ist eine tabstoppgetrennte Datei, aber das erste Spalte alle mit Komma zusammen, also, wenn, was Sie wollen, dass die Informationen herausgetrennt ist, können Sie mit tidyr::separate() tun könnte.

library(tidyr) 

immigration %>% 
    separate(`age,agedef,c_birth,unit,sex,geo\\time`, 
      c("age", "agedef", "c_birth", "unit", "sex", "geo"), 
      sep = ",") 
#> # A tibble: 45,558 x 9 
#>  age agedef   c_birth unit sex geo `2015` `2014` `2013` 
#> * <chr> <chr>   <chr> <chr> <chr> <chr> <chr> <chr> <chr> 
#> 1 TOTAL COMPLET CC5_13_FOR_X_IS NR  F AT 4723 4093 4085 
#> 2 TOTAL COMPLET CC5_13_FOR_X_IS NR  F BE 1017 953 1035 
#> 3 TOTAL COMPLET CC5_13_FOR_X_IS NR  F BG 559 577 743 p 
#> 4 TOTAL COMPLET CC5_13_FOR_X_IS NR  F CH 2876 2766 2758 
#> 5 TOTAL COMPLET CC5_13_FOR_X_IS NR  F CY <NA> <NA>  54 
#> 6 TOTAL COMPLET CC5_13_FOR_X_IS NR  F CZ 120 106 155 
#> 7 TOTAL COMPLET CC5_13_FOR_X_IS NR  F DE <NA> <NA> 14984 
#> 8 TOTAL COMPLET CC5_13_FOR_X_IS NR  F DK 372 365 405 
#> 9 TOTAL COMPLET CC5_13_FOR_X_IS NR  F EE  23  7  16 
#> 10 TOTAL COMPLET CC5_13_FOR_X_IS NR  F EL <NA> <NA> 234 
#> # ... with 45,548 more rows 
0

so etwas wie dies könnte ein Ausgangspunkt sein:

link <- "http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/migr_imm10ctb.tsv.gz" 

data <- readr::read_csv(link) %>% 
     separate("geo\\time\t2015 \t2014 \t2013", into = c("geo", "2015", "2014", "2013"), sep = "\t") 
Verwandte Themen