r语言 - group_by(国家/地区)中的错误:找不到对象'Country.Region'



我正在尝试为冠状病毒创建一个闪亮的应用程序。

library(data.table)
library(magrittr)
library(dplyr)
url <- "https://github.com/datasets/covid-19/blob/master/time-series-19-covid-combined.csv"
data <- read.csv(url)
Confirmed <- data[which(data$Date=="2020-03-12"),] %>%
group_by(Country.Region) %>%
summarise(Confirmed = sum(Confirmed)) %>%
arrange(desc(Confirmed))

Confirmed,我得到一个错误:

group_by(Country.Regon(中出错:找不到对象"Country.Regin">

试试这个:

URL <- "https://raw.githubusercontent.com/datasets/covid-19/master/time-series-19-covid-combined.csv"
data <- read.csv(url(URL), stringsAsFactors = FALSE)  # important !
str(data)  # Always check before continuing further

'data.frame':   12768 obs. of  8 variables:
$ Province.State: Factor w/ 164 levels "","Alameda County, CA",..: 4 4 4 4 4 4 4 4 4 4 ...
$ Country.Region: Factor w/ 111 levels "Afghanistan",..: 60 60 60 60 60 60 60 60 60 60 ...
$ Lat           : num  31.8 31.8 31.8 31.8 31.8 ...
$ Long          : num  117 117 117 117 117 ...
$ Date          : chr  "2020-01-22" "2020-01-23" "2020-01-24" "2020-01-25" ...
$ Confirmed     : int  1 9 15 39 60 70 106 152 200 237 ...
$ Recovered     : int  0 0 0 0 0 0 0 2 2 3 ...
$ Deaths        : int  0 0 0 0 0 0 0 0 0 0 ...

请注意,"日期"是字符。您需要将其转换为"日期"类

library(dplyr)
data <- data %>%
mutate(Date = as.Date(Date))
Confirmed <- data %>%
filter(Date=="2020-03-12") %>%
group_by(Country.Region) %>%
summarise(Confirmed = sum(Confirmed)) %>%
arrange(desc(Confirmed))
# A tibble: 0 x 2
# ... with 2 variables: Country.Region <chr>, Confirmed <int>

相关内容

最新更新