Hi I am trying to subset the data to state specific data frames and subsequently convert them to XTS objects.
Here is the code I am using.
library(tidyverse)
us <- read_csv(url("https://covidtracking.com/api/v1/states/daily.csv")) # pulling detailed state level data from covidtracking project website
us <- select(us, date, state, cases = positive, hosp = hospitalizedCumulative, icu = inIcuCumulative, death) # selecting columns of interest
head(us)
AK <- filter(us, state == "AK")
AK <- xts(AK[,c(-1,-2)], order.by = strptime(AK$date, format = "%Y%m%d")) %>% na.fill(0)
Instead of doing this process manually for all 50 states one by one, is there an easier way to do this automatically by doing something like this?
state <- unique(us$state)
x <- function(x) {
x <- filter(us, state == x)
x <- xts(x[,c(-1,-2)], order.by = strptime(x$date, format = "%Y%m%d")) %>% na.fill(0)
}
Running the function gives me nothing or an error.