首页 > 解决方案 > 从 url 将 csv 下载到 R 中不起作用

问题描述

很抱歉,但我找不到我的代码不起作用的原因。

我想从这里下载一个 csv 文件:https ://www.ecdc.europa.eu/en/publications-data/download-todays-data-geographic-distribution-covid-19-cases-worldwide它甚至告诉我怎么做:

#these libraries need to be loaded
library(utils)

#read the Dataset sheet into “R”. The dataset will be called "data".
data <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", na.strings = "", fileEncoding = "UTF-8-BOM")

我总是收到错误消息:

文件中的错误(文件,“rt”,编码 = fileEncoding):无法打开连接

我试图用谷歌搜索,但找不到任何对我有帮助的东西。该文件存在(我认为),如果我先手动下载它然后将其放入 R 中,它可以正常工作。但我找不到它不能像显示的那样工作的原因。

谢谢您的帮助!

标签: rcsvurl

解决方案


使用data.table

library(data.table)
data <- fread("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", na.strings = "")

或使用rio包及其import()功能

data <- rio::import("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", format = "csv")

编辑

head(data)

      #dateRep year_week cases_weekly deaths_weekly countriesAndTerritories geoId countryterritoryCode popData2019
#1: 18/01/2021   2021-02          557            45             Afghanistan    AF                  AFG    38041757
#2: 11/01/2021   2021-01          675            71             Afghanistan    AF                  AFG    38041757
#3: 04/01/2021   2020-53          902            60             Afghanistan    AF                  AFG    38041757
#4: 28/12/2020   2020-52         1994            88             Afghanistan    AF                  AFG    38041757
#5: 21/12/2020   2020-51          740           111             Afghanistan    AF                  AFG    38041757
#6: 14/12/2020   2020-50         1757            71             Afghanistan    AF                  AFG    38041757
   #continentExp notification_rate_per_100000_population_14-days
#1:         Asia                                            3.24
#2:         Asia                                            4.15
#3:         Asia                                            7.61
#4:         Asia                                            7.19
#5:         Asia                                            6.56
#6:         Asia                                            9.01

推荐阅读