r - 从 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 中,它可以正常工作。但我找不到它不能像显示的那样工作的原因。
谢谢您的帮助!
解决方案
使用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
推荐阅读
- typescript - 如何在firebase函数中打开http url?
- c# - 如何在已发布的 c# Visual Studio 构建中按路径引用文件
- assembly - 如何在字符串中添加特定字符?
- python - 我在完成在线教程时收到 OSError,但我自己无法弄清楚
- .net - TokenAcquisition 失败时如何处理 MicrosoftIdentityWebChallengeUserException?
- hash - 为什么我的程序会错误地计算 MD2 哈希?
- sql - 我怎样才能更有效地编写这个嵌套查询?
- python - 如何附加来自多个 csv 文件的值?
- python - Django-Auth-App 的 app_secret_key 和 secret_key 之间的区别
- php - 如何在 MySQL 中为 PHP 中的电子商店制作正确的事务和/或锁定表?