-1

I am trying to create a shiny app for Coronavirus.

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))

At Confirmed, I get an error:

Error in group_by(Country.Region) : object 'Country.Region' not found

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Prathit
  • 87
  • 8
  • May this helps you https://stackoverflow.com/questions/14441729/read-a-csv-from-github-into-r – LocoGris Mar 14 '20 at 12:57
  • Try `url <- "https://raw.githubusercontent.com/datasets/covid-19/master/time-series-19-covid-combined.csv"` to read in your data (same as selecting `raw` from the github page you referenced. – Ben Mar 14 '20 at 14:14

1 Answers1

0

Try this:

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 ...

Note that the "Date" is character. You need to convert it into a "Date" class

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>
Edward
  • 10,360
  • 2
  • 11
  • 26