【问题标题】:Download csv into R from url does not work [closed]从url下载csv到R不起作用[关闭]
【发布时间】:2021-01-23 12:17:06
【问题描述】:

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

我想从这里下载一个 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 中,它可以正常工作。但我找不到它不像显示的那样工作的原因。

感谢您的帮助!

【问题讨论】:

  • 当我照原样复制时,您的代码对我有效。

标签: r csv url


【解决方案1】:

使用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

【讨论】:

  • 您好,谢谢您的回答;但我收到两个版本的错误消息: curl::curl_fetch_memory(file) 中的错误:schannel: next InitializeSecurityContext failed: SEC_E_ILLEGAL_MESSAGE (0x80090326) - 此错误通常发生在收到致命 SSL/TLS 警报时(例如握手失败)。你知道可能是什么问题吗?
  • 这个答案是你喜欢的类型吗?
  • 嗨,是的,这看起来完全符合我的要求,但是使用提供的代码,我无法得到它。显然,这是 SSL/TLS 的一个问题,它无法在所有设备/系统上重现,并且没有明确指出问题出在哪里:stackoverflow.com/questions/64147821/… 但是非常感谢您的帮助!
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多