我在导入R中的.tsv
文件时遇到问题。数据文件来自欧盟统计局,并且公开发布:http://ec.europa.eu/eurostat/en/web/products-datasets/-/MIGR_IMM10CTB
我使用以下代码导入它:
immig <- read.table(file="immig.tsv", sep="\t", header=TRUE)
但是,代码似乎不起作用。我没有收到任何错误消息,但输出如下:
> 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
我做错了什么?我尝试使用sep=","
代替,但似乎在创建其他问题时解决了一些问题。
答案 0 :(得分:1)
您是否缺少2013年数据?
我在该链接下载了该文件,使用命令行工具解压缩,然后使用readr库可以很好地导入它:
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
看起来周围有一些备用字符(743 p
),只有数字,所以你需要做更多的清理,然后转换为数字。
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
它是一个制表符分隔的文件,但第一列全部用逗号组合在一起,所以如果您想要的是分离出的信息,您可以使用tidyr::separate()
执行此操作。
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
答案 1 :(得分:0)
这样的事情可能是一个起点:
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")