简体   繁体   中英

Separate data into multiple time series R

I have a single excel sheet with all of the states in the united states. I would like to create a time series out of each state and have the data frequency be daily (it is currently by minute). The most I have done so far was to delete all of the excess columns but I am having trouble thinking of an efficient way to make the data daily and separated by state without doing it manually.

I hope to use ggplot with all of these new time series. I tried using the melt function and gather function but both did not work.

Here is a portion of my data:

在此处输入图像描述 Where the state column goes from 1-51 and the dates are repeated every now and then since its frequency was per minute. I would like to ultimiatley create a time series for each of these states so I could analyze them side by side. Some states may not have data records for everyday, how do I fill in those dates automatically with zero?

Welcome to SO, Cherry. In the future, please provide a reproducible example rather than a picture of a data frame. The function dput(your_df_here) might be useful.

Here is my sample data, which is different from your:

df <- structure(list(STATE = c(1, 1, 1, 2, 2, 2), VETOTAL = c(2, 2, 3, 1, 1, 2), VEFORMS = c(2, 2, 3, 1, 1, 2), 
           PVHJNVL = c(0, 0, 0, 0, 0, 0), PEDS = c(0, 0, 0, 1, 0, 0), PERSONS = c(3, 2, 4, 1, 1, 2), 
           PERMVIT = c(3, 2, 4, 1, 1, 2), PERNOTMVI = c(0, 0, 0, 1, 0, 0), COUNTY = c(81, 55, 29, 55, 3, 85), 
           CITY = c(2340, 1280, 0, 2562, 0, 0), DAY = c(7, 23, 22, 7, 23, 22), MONTH = c(2, 1, 1, 2, 1, 1), 
           YEAR = c(2019, 2019, 2019, 2019, 2019, 2019), FATALS = c(1, 1, 1, 1, 0, 1), DRUNK_DR = c(1, 0, 0, 0, 1, 0)), 
       row.names = c(NA, -6L), class = "data.frame")

Here is how, with the help of {tidyverse} we create a date observation, group by State and date, and then summarise a sum.

library(tidyverse)
df %>% 
   mutate(date = as.Date(paste(YEAR, MONTH, DAY, sep = "-"))) %>% # create a date
   group_by(STATE, date) %>% # Group by State id and date
   summarise_at(.vars = vars(VETOTAL:PERNOTMVI, FATALS, DRUNK_DR), sum) ## Summarise a sum of those variables between VETOTAL and PERNOTMVI, plus FATALS and DRUNK_DR

Results:

# A tibble: 6 x 10
# Groups:   STATE [2]
  STATE date       VETOTAL VEFORMS PVHJNVL  PEDS PERSONS PERMVIT PERNOTMVI FATALS
  <dbl> <date>       <dbl>   <dbl>   <dbl> <dbl>   <dbl>   <dbl>     <dbl>  <dbl>
1     1 2019-01-22       3       3       0     0       4       4         0      1
2     1 2019-01-23       2       2       0     0       2       2         0      1
3     1 2019-02-07       2       2       0     0       3       3         0      1
4     2 2019-01-22       2       2       0     0       2       2         0      1
5     2 2019-01-23       1       1       0     0       1       1         0      0
6     2 2019-02-07       1       1       0     1       1       1         1      1

Filling missing dates.

If you want to fill with 0 the values of missing dates within a range (ie those dates without recorded observations), we can do so with the help of {padr}

library(padr)
    df %>% 
  mutate(date = as.Date(paste(YEAR, MONTH, DAY, sep = "-"))) %>% 
  group_by(STATE, date) %>% 
  summarise_at(.vars = vars(VETOTAL:PERNOTMVI, FATALS), sum) %>% 
  padr::pad(start_val = min(.$date), #This sets the start value as the earliest date present in the "date" variable 
            end_val = max(.$date)) %>%    #This sets the end value as the earliest date present in the "date" variable
  fill_by_value(value = 0)

Results:

# A tibble: 34 x 10
# Groups:   STATE [2]
   STATE date       VETOTAL VEFORMS PVHJNVL  PEDS PERSONS PERMVIT PERNOTMVI FATALS
   <dbl> <date>       <dbl>   <dbl>   <dbl> <dbl>   <dbl>   <dbl>     <dbl>  <dbl>
 1     1 2019-01-22       3       3       0     0       4       4         0      1
 2     1 2019-01-23       2       2       0     0       2       2         0      1
 3     1 2019-01-24       0       0       0     0       0       0         0      0
 4     1 2019-01-25       0       0       0     0       0       0         0      0
 5     1 2019-01-26       0       0       0     0       0       0         0      0
 6     1 2019-01-27       0       0       0     0       0       0         0      0
 7     1 2019-01-28       0       0       0     0       0       0         0      0
 8     1 2019-01-29       0       0       0     0       0       0         0      0
 9     1 2019-01-30       0       0       0     0       0       0         0      0
10     1 2019-01-31       0       0       0     0       0       0         0      0
# ... with 24 more rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM