【问题标题】:Importing CSV data from UN database从联合国数据库导入 CSV 数据
【发布时间】:2017-03-28 13:14:02
【问题描述】:

我正在尝试从 UNHCR http://popstats.unhcr.org/en/time_series 导入难民数据

我导出数据并尝试使用我以前从未遇到过问题的read.csv 函数导入它并收到以下错误代码

un <- read.csv("un.csv", na.strings = "..")
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  more columns than column names

对于 ref,我已经用 word 打开了 csv 文件,这是它的格式;

""Extracted from the UNHCR Population Statistics Reference Database","United Nations High Commissioner for Refugees"
"Date extracted: 2015-09-18 04:37:24 +02:00"

Year,"Country / territory of asylum/residence",Origin,"Population type",Value
1951,Australia,Various/Unknown,"Refugees (incl. refugee-like situations)",180000
1951,Austria,Various/Unknown,"Refugees (incl. refugee-like situations)",282000
1951,Belgium,Various/Unknown,"Refugees (incl. refugee-like situations)",55000
1951,Canada,Various/Unknown,"Refugees (incl. refugee-like situations)",168511
1951,Switzerland,Various/Unknown,"Refugees (incl. refugee-like situations)" 

所以似乎格式正确,所以我对出了什么问题有点茫然。

感谢您的帮助

克里斯

【问题讨论】:

  • 您需要跳过第一行,它确实有四列,而您的数据有五列。具体方法请参见?read.csv
  • 好的,这对我有用,非常感谢,感谢您的宝贵时间!

标签: r csv read.table read.csv


【解决方案1】:

您的数据有错别字。应该是这样的:

un = structure(list(Year = c(1951L, 1951L, 1951L, 1951L, 1951L), Country...territory.of.asylum.residence = structure(1:5, .Label = c("Australia", 
"Austria", "Belgium", "Canada", "Switzerland"), class = "factor"), 
    Origin = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Various/Unknown", class = "factor"), 
    Population.type = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Refugees (incl. refugee-like situations)", class = "factor"), 
    Value = c(180000, 282000, 55000, 168511, NA)), .Names = c("Year", 
"Country...territory.of.asylum.residence", "Origin", "Population.type", 
"Value"), class = "data.frame", row.names = c(NA, -5L))

然后您可以使用 read.table() 轻松导入:

df=read.table("un.csv", header = T, sep=",")

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    相关资源
    最近更新 更多