How to group the data in time series in consecutive triples and plot it.

I imported a csv file and I need to filter a row from the data frame. Then I need to take the number of death as a time series, i.e., s_1, s_2, s_3, s_4, ..., s_n, group the data into consecutive triples (s_1, s_2, s_3), (s_2, s_3, s_4), (s_3, s_4, s_5), etc. And plot the first two values as x and y coordinates and the third value as z coordinate.

My code is:
setwd("E:/COVID.UG/COVID-19-master/COVID-19 master/csse_covid_19_data/csse_covid_19_time_series")

deaths.df = read.csv(file = "time_series_covid19_deaths_US.csv", header = TRUE)

D <- deaths.df %>%

  • filter(Province_State == "Georgia" & Admin2 == "Macon")

I am not sure how to proceed!

library(tidyverse)
library(plotly)
set.seed(43)
(df <- tibble(t=1:99,
 s = 1+ cumsum(sample.int(3,99,replace=TRUE) -2)))


(df <- mutate(df,
              lead_s1 = lead(s),lead_s2=lead(s,2)))

plot_ly(data=df,
        type="scatter3d",
        mode="markers",
        x=~s,
        y=~lead_s1,
        z=~lead_s2)
D <- deaths.df %>%
+     + filter(Province_State == "Georgia" & Admin2 == "Macon")
Error in deaths.df %>% +filter(Province_State == "Georgia" & Admin2 ==  : 
  could not find function "%>%"
> 
> library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.0 --
v ggplot2 3.3.2     v purrr   0.3.4
v tibble  3.0.1     v dplyr   1.0.0
v tidyr   1.1.0     v stringr 1.4.0
v readr   1.3.1     v forcats 0.5.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
Warning message:
package ‘ggplot2’ was built under R version 4.0.2 
> library(plotly)
Error in library(plotly) : there is no package called ‘plotly’
> set.seed(43)
> (df <- tibble(t=1:99,
+               s = 1+ cumsum(sample.int(3,99,replace=TRUE) -2)))
# A tibble: 99 x 2
       t     s
   <int> <dbl>
 1     1     0
 2     2     0
 3     3    -1
 4     4    -1
 5     5    -2
 6     6    -1
 7     7     0
 8     8     0
 9     9     1
10    10     1
# ... with 89 more rows
> 
> 
> (df <- mutate(df,
+               lead_s1 = lead(s),lead_s2=lead(s,2)))
# A tibble: 99 x 4
       t     s lead_s1 lead_s2
   <int> <dbl>   <dbl>   <dbl>
 1     1     0       0      -1
 2     2     0      -1      -1
 3     3    -1      -1      -2
 4     4    -1      -2      -1
 5     5    -2      -1       0
 6     6    -1       0       0
 7     7     0       0       1
 8     8     0       1       1
 9     9     1       1       2
10    10     1       2       2
# ... with 89 more rows
> 
> plot_ly(data=df,
+         type="scatter3d",
+         mode="markers",
+         x=~s,
+         y=~lead_s1,
+         z=~lead_s2)
Error in plot_ly(data = df, type = "scatter3d", mode = "markers", x = ~s,  : 
  could not find function "plot_ly"

Thank you so much for your reply, I appreciate your help. When I ran your code, I got the error that I have pasted above. How should I resolve this?

you should install plotly if you want to use it as a library

install.packages("plotly")

please format your code with triple backticks so that it doesnt look messy, I will fix yours on this occasion.
check the edit to see how it looks.

Hi @nirgrahamuk, thank you so much again for letting me know the errors I made. I made the edits you suggested and the plot I got is this:

Is it the way it should be? Also, could you in shortcut walk me through how you solved it? I do not understand it completely! Again, thank you so much for taking your time to help me! I greatly appreciate it!

you plotted my data , but not yours.
If you are very much a beginner in R, then there's not many better ways to learn than sitting an reading an appropriate book.

If you don't mind to answer, I have a few questions about this code:
1.When I try to filter the data frame to extract the row for "New York" in D, I get a integer instead of a data frame. Why do you think this happens?
2. I need to then convert this vector to a matrix/dataframe of 3 consecutive values and plot them as a 3D scatterplot. Could you clarify how you did that using the lead function?
I am sorry for bothering you with these questions but I am stuck so I had to ask.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.