【问题标题】:Importing a .tsv file导入 .tsv 文件
【发布时间】:2017-06-10 01:29:21
【问题描述】:

我在 R 中导入 .tsv 文件时遇到问题。数据文件来自 Eurostats,并且是公开的: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=",",但它似乎在创建其他问题的同时解决了一些问题。

【问题讨论】:

标签: r


【解决方案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

【讨论】:

    【解决方案2】:

    这样的事情可能是一个起点:

    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")
    

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 2022-11-04
      • 2014-07-21
      • 2012-12-18
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多