Related
I'm trying to forecast the year-month data with one grouping variable which is "Actual Delivery location". I'm using ARIMA from Fable package for the forecasting but i'm getting the following error when I run the model
Error message : 3 errors (1 unique) encountered for ARIMA(Sales.Units, stepwise = F, approximation = F)
[3] .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using tsibble::fill_gaps() if required.
I have replaced the missing values with zero and I did the group_by with Actual Delivery location variable.
Could you please help to resolve this.
library(tidyverse)
library(tibbletime)
library(lubridate)
library(norm)
library(tibble)
library(tsibble)
library(fpp3)
Global.df<-structure(list(Actual.Delivery.Location = c("A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A324", "A324", "A324",
"A324", "A324", "A324", "A324", "A324", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A482", "A482", "A482",
"A482", "A482", "A482", "A482", "A482", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999", "A999", "A999", "A999",
"A999", "A999", "A999", "A999", "A999"), YearMonth = c("Jan 2016",
"Feb 2016", "Mar 2016", "Apr 2016", "May 2016", "Jun 2016", "Jul 2016",
"Aug 2016", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Jan 2019",
"Feb 2019", "Mar 2019", "Apr 2019", "May 2019", "Jun 2019", "Jul 2019",
"Aug 2019", "Sep 2019", "Oct 2019", "Nov 2019", "Dec 2019", "Jan 2020",
"Feb 2020", "Mar 2020", "Apr 2020", "May 2020", "Jun 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jan 2021",
"Feb 2021", "Mar 2021", "Apr 2021", "May 2021", "Jun 2021", "Jul 2021",
"Aug 2021", "Sep 2021", "Oct 2021", "Nov 2021", "Dec 2021", "Jan 2016",
"Feb 2016", "Mar 2016", "Apr 2016", "May 2016", "Jun 2016", "Jul 2016",
"Aug 2016", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Jan 2019",
"Feb 2019", "Mar 2019", "Apr 2019", "May 2019", "Jun 2019", "Jul 2019",
"Aug 2019", "Sep 2019", "Oct 2019", "Nov 2019", "Dec 2019", "Jan 2020",
"Feb 2020", "Mar 2020", "Apr 2020", "May 2020", "Jun 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jan 2021",
"Feb 2021", "Mar 2021", "Apr 2021", "May 2021", "Jun 2021", "Jul 2021",
"Aug 2021", "Sep 2021", "Oct 2021", "Nov 2021", "Dec 2021", "Jan 2016",
"Feb 2016", "Mar 2016", "Apr 2016", "May 2016", "Jun 2016", "Jul 2016",
"Aug 2016", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Jan 2019",
"Feb 2019", "Mar 2019", "Apr 2019", "May 2019", "Jun 2019", "Jul 2019",
"Aug 2019", "Sep 2019", "Oct 2019", "Nov 2019", "Dec 2019", "Jan 2020",
"Feb 2020", "Mar 2020", "Apr 2020", "May 2020", "Jun 2020", "Jul 2020",
"Aug 2020", "Sep 2020", "Oct 2020", "Nov 2020", "Dec 2020", "Jan 2021",
"Feb 2021", "Mar 2021", "Apr 2021", "May 2021", "Jun 2021", "Jul 2021",
"Aug 2021", "Sep 2021", "Oct 2021", "Nov 2021", "Dec 2021"),
Sales.Units = c(2422, 1176, 2398, 2540, 3608, 4915, 3267,
4315, 2112, 3106, 1868, 2867, 2884, 555, 3208, 1732, 1180,
5911, 4082, 2992, 2173, 1974, 1665, 1997, 1989, 1382, 2074,
1433, 2537, 1869, 2106, 1923, 3410, 2035, 1174, 1462, 2372,
982, 4115, 2370, 3332, 983, 816, 2008, 3107, 4657, 1208,
1569, 1459, 1835, 1952, 337, 677, 4876, 2966, 2718, 6459,
1384, 1082, 2533, 1568, 1055, 1948, 1332, 2324, 3935, 2340,
1153, 1136, 491, 736, 802, 1303, 917, 683, 47, 847, 876,
196, 124, 620, 334, 114, 363, 592, 348, 842, 139, 747, 2242,
1653, 831, 105, 832, 400, 1026, 1053, 932, 998, 188, 334,
1418, 609, 300, 1097, 135, 549, 339, 1506, 1293, 76, 261,
97, 92, 678, 44, 697, 535, 203, 21, 135, 132, 259, 48, NA,
NA, 986, 985, 1340, 541, 211, 1018, 1810, 1028, 443, 1960,
114, 1342, 740, 73, 1512, 137, 169, 2308, 8324, 9000, 10006,
11553, 13637, 21684, 17027, 14328, 13915, 3828, 7620, 9405,
12086, 5963, 28456, 7933, 10039, 19783, 13488, 14828, 18563,
8864, 9995, 12951, 11156, 13203, 19383, 15198, 18466, 24225,
12761, 13580, 10387, 7638, 5584, 11093, 10959, 5781, 22894,
19559, 11428, 19135, 14153, 13864, 10961, 13355, 9454, 16976,
4837, 1077, 13443, 23364, 3667, 7469, 9465, 14030, 25455,
21555, 12228, 21354, 11829, 11518, 29550, 20653, 8949, 35923,
9466, 22290, 18310, 11170, 10732, 17269)), row.names = c(NA,
216L), class = "data.frame")
Global.df[is.na(Global.df)]<-0
Global.df$YearMonth<-lubridate::my(Global.df$YearMonth)
Global.df <- as_tibble(Global.df)
Global.df<- Global.df %>%
group_by(Actual.Delivery.Location) %>%
as_tsibble(key = Actual.Delivery.Location,
index = YearMonth)
Fit.Global.df<- Global.df %>%
group_by(Actual.Delivery.Location) %>%
model(ARIMA(Sales.Units,stepwise = F, approximation = F))
I have figured this out.. I have to convert the year month using yearmonth function instead of converting it into dates.
Global.df <- Global.df %>%
mutate(Month = yearmonth(YearMonth))
I want to reshape my Date column in the same months-wise. For example, the first 12 rows in the Date column of the dataframe contains a character of all the months in the year 2001 and the respective year itself. The next rows are the same, until the year 2017.
Therefore. I want to reshape my data like the following. For example, I want all Januaries of the all years to be in the first rows of Date column, then all februaries in the next rows, Marches of the all years and so on, until December of all years (2001-2017)
I need like this format:
1 2001 1
2 2002 1
3 2003 1
4 2004 1
5 2005 1
6 2006 1
7 2007 1
8 2008 1
9 2009 1
10 2010 1
11 2011 1
12 2012 1
13 2013 1
14 2014 1
15 2015 1
16 2016 1
17 2017 1
18 2001 2
19 2002 2
20 2003 2
21 2004 2
22 2005 2
23 2006 2
24 2007 2
This is my dataframe I generated using dput()
#load packages that might be required
> library(tidyverse)
> library(xts)
> # Read NDVI
> NDVI <- read.csv("processed//NDVI.csv")
> dput(NDVI)
structure(list(Date = c("Jan 1, 2001", "Feb 1, 2001", "Mar 1, 2001",
"Apr 1, 2001", "May 1, 2001", "Jun 1, 2001", "Jul 1, 2001", "Aug 1, 2001",
"Sep 1, 2001", "Oct 1, 2001", "Nov 1, 2001", "Dec 1, 2001", "Jan 1, 2002",
"Feb 1, 2002", "Mar 1, 2002", "Apr 1, 2002", "May 1, 2002", "Jun 1, 2002",
"Jul 1, 2002", "Aug 1, 2002", "Sep 1, 2002", "Oct 1, 2002", "Nov 1, 2002",
"Dec 1, 2002", "Jan 1, 2003", "Feb 1, 2003", "Mar 1, 2003", "Apr 1, 2003",
"May 1, 2003", "Jun 1, 2003", "Jul 1, 2003", "Aug 1, 2003", "Sep 1, 2003",
"Oct 1, 2003", "Nov 1, 2003", "Dec 1, 2003", "Jan 1, 2004", "Feb 1, 2004",
"Mar 1, 2004", "Apr 1, 2004", "May 1, 2004", "Jun 1, 2004", "Jul 1, 2004",
"Aug 1, 2004", "Sep 1, 2004", "Oct 1, 2004", "Nov 1, 2004", "Dec 1, 2004",
"Jan 1, 2005", "Feb 1, 2005", "Mar 1, 2005", "Apr 1, 2005", "May 1, 2005",
"Jun 1, 2005", "Jul 1, 2005", "Aug 1, 2005", "Sep 1, 2005", "Oct 1, 2005",
"Nov 1, 2005", "Dec 1, 2005", "Jan 1, 2006", "Feb 1, 2006", "Mar 1, 2006",
"Apr 1, 2006", "May 1, 2006", "Jun 1, 2006", "Jul 1, 2006", "Aug 1, 2006",
"Sep 1, 2006", "Oct 1, 2006", "Nov 1, 2006", "Dec 1, 2006", "Jan 1, 2007",
"Feb 1, 2007", "Mar 1, 2007", "Apr 1, 2007", "May 1, 2007", "Jun 1, 2007",
"Jul 1, 2007", "Aug 1, 2007", "Sep 1, 2007", "Oct 1, 2007", "Nov 1, 2007",
"Dec 1, 2007", "Jan 1, 2008", "Feb 1, 2008", "Mar 1, 2008", "Apr 1, 2008",
"May 1, 2008", "Jun 1, 2008", "Jul 1, 2008", "Aug 1, 2008", "Sep 1, 2008",
"Oct 1, 2008", "Nov 1, 2008", "Dec 1, 2008", "Jan 1, 2009", "Feb 1, 2009",
"Mar 1, 2009", "Apr 1, 2009", "May 1, 2009", "Jun 1, 2009", "Jul 1, 2009",
"Aug 1, 2009", "Sep 1, 2009", "Oct 1, 2009", "Nov 1, 2009", "Dec 1, 2009",
"Jan 1, 2010", "Feb 1, 2010", "Mar 1, 2010", "Apr 1, 2010", "May 1, 2010",
"Jun 1, 2010", "Jul 1, 2010", "Aug 1, 2010", "Sep 1, 2010", "Oct 1, 2010",
"Nov 1, 2010", "Dec 1, 2010", "Jan 1, 2011", "Feb 1, 2011", "Mar 1, 2011",
"Apr 1, 2011", "May 1, 2011", "Jun 1, 2011", "Jul 1, 2011", "Aug 1, 2011",
"Sep 1, 2011", "Oct 1, 2011", "Nov 1, 2011", "Dec 1, 2011", "Jan 1, 2012",
"Feb 1, 2012", "Mar 1, 2012", "Apr 1, 2012", "May 1, 2012", "Jun 1, 2012",
"Jul 1, 2012", "Aug 1, 2012", "Sep 1, 2012", "Oct 1, 2012", "Nov 1, 2012",
"Dec 1, 2012", "Jan 1, 2013", "Feb 1, 2013", "Mar 1, 2013", "Apr 1, 2013",
"May 1, 2013", "Jun 1, 2013", "Jul 1, 2013", "Aug 1, 2013", "Sep 1, 2013",
"Oct 1, 2013", "Nov 1, 2013", "Dec 1, 2013", "Jan 1, 2014", "Feb 1, 2014",
"Mar 1, 2014", "Apr 1, 2014", "May 1, 2014", "Jun 1, 2014", "Jul 1, 2014",
"Aug 1, 2014", "Sep 1, 2014", "Oct 1, 2014", "Nov 1, 2014", "Dec 1, 2014",
"Jan 1, 2015", "Feb 1, 2015", "Mar 1, 2015", "Apr 1, 2015", "May 1, 2015",
"Jun 1, 2015", "Jul 1, 2015", "Aug 1, 2015", "Sep 1, 2015", "Oct 1, 2015",
"Nov 1, 2015", "Dec 1, 2015", "Jan 1, 2016", "Feb 1, 2016", "Mar 1, 2016",
"Apr 1, 2016", "May 1, 2016", "Jun 1, 2016", "Jul 1, 2016", "Aug 1, 2016",
"Sep 1, 2016", "Oct 1, 2016", "Nov 1, 2016", "Dec 1, 2016", "Jan 1, 2017",
"Feb 1, 2017", "Mar 1, 2017", "Apr 1, 2017", "May 1, 2017", "Jun 1, 2017",
"Jul 1, 2017", "Aug 1, 2017", "Sep 1, 2017", "Oct 1, 2017", "Nov 1, 2017",
"Dec 1, 2017"), NDVI = c(0.722, 0.418, 0.435, 0.568, 0.616, 0.75,
0.62, 0.516, 0.389, 0.484, 0.65, 0.7, 0.533, 0.327, 0.279, 0.43,
0.701, 0.797, 0.748, 0.569, 0.597, 0.662, 0.794, 0.706, 0.537,
0.338, 0.404, 0.461, 0.759, 0.76, 0.578, 0.408, 0.56, 0.62, 0.684,
0.785, 0.539, 0.442, 0.307, 0.461, 0.593, 0.739, 0.674, 0.495,
0.603, 0.712, 0.776, 0.665, 0.522, 0.333, 0.398, 0.28, 0.713,
0.746, 0.757, 0.549, 0.711, 0.685, 0.693, 0.716, 0.617, 0.476,
0.407, 0.325, 0.555, 0.757, 0.622, 0.544, 0.452, 0.586, 0.761,
0.649, 0.635, 0.472, 0.351, 0.402, 0.662, 0.757, 0.712, 0.69,
0.618, 0.632, 0.797, 0.778, 0.543, 0.401, 0.393, 0.556, 0.68,
0.784, 0.659, 0.654, 0.412, 0.653, 0.694, 0.727, 0.715, 0.55,
0.641, 0.443, 0.709, 0.818, 0.646, 0.59, 0.39, 0.562, 0.67, 0.56,
0.476, 0.312, 0.349, 0.405, 0.705, 0.766, 0.665, 0.577, 0.614,
0.518, 0.718, 0.779, 0.792, 0.547, 0.585, 0.373, 0.605, 0.715,
0.779, 0.758, 0.751, 0.77, 0.752, 0.805, 0.521, 0.471, 0.561,
0.595, 0.688, 0.74, 0.77, 0.69, 0.704, 0.613, 0.653, 0.803, 0.63,
0.472, 0.589, 0.512, 0.562, 0.699, 0.814, 0.637, 0.395, 0.462,
0.419, 0.742, 0.498, 0.35, 0.308, 0.321, 0.597, 0.677, 0.729,
0.526, 0.394, 0.567, 0.806, 0.752, 0.703, 0.495, 0.453, 0.48,
0.593, 0.731, 0.799, 0.619, 0.403, 0.396, 0.556, 0.743, 0.717,
0.504, 0.408, 0.356, 0.215, 0.68, 0.487, 0.594, 0.392, 0.566,
0.547, 0.632, 0.604, 0.528, 0.424, 0.627, 0.448, 0.788, 0.764,
0.592, 0.517, 0.274, 0.577, 0.754)), class = "data.frame", row.names = c(NA,
-204L))
library(dplyr)
library(tidyr)
library(lubridate)
df %>%
mutate(Date = mdy(Date)) %>%
separate(Date, into = c("year", "month", "day"), remove = FALSE) %>%
arrange(month, day, year)
# Date year month day NDVI
# 1 2001-01-01 2001 01 01 0.722
# 2 2002-01-01 2002 01 01 0.533
# 3 2003-01-01 2003 01 01 0.537
# 4 2004-01-01 2004 01 01 0.539
# 5 2005-01-01 2005 01 01 0.522
# 6 2006-01-01 2006 01 01 0.617
# ...
This is not generally called a "reshaping" operation - this is sorting/ordering/arranging rows. Reshaping/pivoting is when something that is a column becomes a row, or vice versa.
I'm writing code in R that uses ggplot2 to generate several bar graphs based on test data from several trainings, spanning (at present) about a year, like the following:
Currently all the dates are formatted "mon DD YYYY" ("%b %d %Y" as a Date format):
c("Oct 05 2020", "Nov 02 2020", "Nov 30 2020", "Jan 11 2021", "Feb 22 2021",
"Mar 08 2021", "Mar 29 2021", "Apr 12 2021", "May 03 2021", "May 17 2021")
But I'd like to only display the year on the first date, and any subsequent dates that are the first date in a year:
c("Oct 05 2020", "Nov 02", "Nov 30", "Jan 11 2021", "Feb 22", "Mar 08", "Mar 29",
"Apr 12", "May 03", "May 17")
Is there a way to do this, either via some kind of filtering of the Date column or something in ggplot?
We could convert the vector of date into Date class ('v1'), extract the 'Year' component, create a logical vector with duplicated, use that index ('i1') to replace the values in the original vector with formatted Dates from 'v2'
v2 <- as.Date(v1, '%b %d %Y')
i1 <- duplicated(format(v2, '%Y'))
v1[i1] <- format(v2[i1], '%b %d')
-ouptut
v1
[1] "Oct 05 2020" "Nov 02" "Nov 30"
[4] "Jan 11 2021" "Feb 22" "Mar 08" "Mar 29"
[8] "Apr 12" "May 03" "May 17"
data
v1 <- c("Oct 05 2020", "Nov 02 2020", "Nov 30 2020", "Jan 11 2021", "Feb 22 2021",
"Mar 08 2021", "Mar 29 2021", "Apr 12 2021", "May 03 2021", "May 17 2021")
This question already has answers here:
How to convert a String to Date in R
(3 answers)
Closed 2 years ago.
I have a data.frame that looks like this:
samples data can be build using codes:
df<-structure(list(LSAF..Date.acted.On. = structure(c(33L, 18L, 7L,
32L, 51L, 47L, 2L, 45L, 38L, 15L), .Label = c("Aug 13, 2019, 10:44:07 AM GMT-04:00",
"Aug 13, 2019, 10:45:47 AM GMT-04:00", "Aug 13, 2019, 10:47:20 AM GMT-04:00",
"Aug 13, 2019, 10:53:29 AM GMT-04:00", "Aug 13, 2019, 10:54:54 AM GMT-04:00",
"Aug 13, 2019, 10:57:11 AM GMT-04:00", "Aug 13, 2019, 10:58:13 AM GMT-04:00",
"Feb 13, 2020, 1:13:07 PM GMT-05:00", "Feb 13, 2020, 1:17:07 PM GMT-05:00",
"Feb 22, 2019, 11:13:51 AM GMT-05:00", "Jan 28, 2020, 11:19:19 AM GMT-05:00",
"Jan 28, 2020, 11:21:28 AM GMT-05:00", "Jan 7, 2020, 9:59:28 AM GMT-05:00",
"Jul 13, 2020, 9:17:01 PM GMT-04:00", "Jul 15, 2020, 9:08:54 PM GMT-04:00",
"Jul 18, 2019, 11:15:26 AM GMT-04:00", "Jul 23, 2020, 10:50:32 AM GMT-04:00",
"Mar 5, 2020, 10:00:07 AM GMT-05:00", "Mar 5, 2020, 10:01:22 AM GMT-05:00",
"Mar 5, 2020, 9:56:36 AM GMT-05:00", "Mar 5, 2020, 9:58:31 AM GMT-05:00",
"Mar 6, 2020, 1:17:17 PM GMT-05:00", "May 13, 2019, 11:07:52 AM GMT-04:00",
"May 14, 2019, 2:32:40 PM GMT-04:00", "May 16, 2019, 3:46:08 PM GMT-04:00",
"May 16, 2019, 4:23:27 PM GMT-04:00", "May 16, 2019, 4:25:40 PM GMT-04:00",
"May 16, 2019, 4:29:12 PM GMT-04:00", "May 16, 2019, 4:40:58 PM GMT-04:00",
"May 21, 2019, 2:22:07 PM GMT-04:00", "Nov 13, 2020, 4:14:55 PM GMT-05:00",
"Nov 13, 2020, 4:21:06 PM GMT-05:00", "Nov 13, 2020, 4:24:07 PM GMT-05:00",
"Nov 13, 2020, 9:10:19 AM GMT-05:00", "Nov 24, 2020, 10:51:52 AM GMT-05:00",
"Oct 15, 2020, 3:46:52 PM GMT-04:00", "Oct 15, 2020, 3:48:35 PM GMT-04:00",
"Oct 17, 2019, 9:09:20 PM GMT-04:00", "Oct 18, 2019, 11:15:51 AM GMT-04:00",
"Oct 20, 2020, 1:28:57 PM GMT-04:00", "Oct 26, 2020, 1:52:26 PM GMT-04:00",
"Oct 26, 2020, 2:15:11 PM GMT-04:00", "Oct 29, 2019, 1:55:53 PM GMT-04:00",
"Oct 9, 2019, 10:39:10 AM GMT-04:00", "Oct 9, 2019, 10:45:14 AM GMT-04:00",
"Sep 11, 2020, 2:40:34 PM GMT-04:00", "Sep 19, 2019, 2:45:46 PM GMT-04:00",
"Sep 23, 2019, 2:24:01 PM GMT-04:00", "Sep 9, 2019, 10:16:01 AM GMT-04:00",
"Sep 9, 2019, 10:22:23 AM GMT-04:00", "Sep 9, 2019, 10:25:03 AM GMT-04:00",
"Sep 9, 2019, 10:32:44 AM GMT-04:00"), class = "factor")), row.names = c(NA,
10L), class = "data.frame")
How can I modify the data so it can be used as a date variable?
Thanks.
You can use lubridate's mdy_hms
df$date_time <- lubridate::mdy_hms(df$LSAF..Date.acted.On.)
df$date <- as.Date(df$date_time)
head(df)
#. LSAF..Date.acted.On. date_time date
#1 Nov 13, 2020, 4:24:07 PM GMT-05:00 2020-11-13 21:24:07 2020-11-13
#2 Mar 5, 2020, 10:00:07 AM GMT-05:00 2020-03-05 15:00:07 2020-03-05
#3 Aug 13, 2019, 10:58:13 AM GMT-04:00 2019-08-13 14:58:13 2019-08-13
#4 Nov 13, 2020, 4:21:06 PM GMT-05:00 2020-11-13 21:21:06 2020-11-13
#5 Sep 9, 2019, 10:25:03 AM GMT-04:00 2019-09-09 14:25:03 2019-09-09
#6 Sep 19, 2019, 2:45:46 PM GMT-04:00 2019-09-19 18:45:46 2019-09-19
You could try
as.Date(df$LSAF..Date.acted.On.,
format = "%b %d, %Y, %I:%M:%S %p")
I am trying to use the highcharter in R to create a motion map like this one http://jkunst.com/r/adding-motion-to-choropleths/
But I have a problem: the data is not visualized
df<-structure(list(fips = c("ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG",
"ARG", "ARG", "ARG", "ARG", "AUS", "AUS", "AUS", "AUS", "AUS",
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS",
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS",
"AUS", "AUS", "AUS", "AUS", "AUS", "CAN", "CAN", "CAN", "CAN",
"CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN",
"CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "CAN",
"CAN", "CAN", "CAN", "CAN", "CAN", "CAN", "DEU", "DEU", "DEU",
"DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU",
"DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU",
"DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "DEU", "GBR", "GBR",
"GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR",
"GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR",
"GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "GBR", "ITA",
"ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA",
"ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA",
"ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA", "ITA",
"ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU",
"ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU",
"ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU", "ROU",
"ROU", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS",
"RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS",
"RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS", "RUS",
"RUS", "RUS", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",
"USA", "USA", "USA"), sm = c("Sep 2016", "Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017",
"Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018",
"Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018",
"Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016",
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017",
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017",
"Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018",
"Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017",
"Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018",
"Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018",
"Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016",
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017",
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017",
"Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018",
"Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017",
"Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018", "Apr 2018", "May 2018",
"Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018",
"Dec 2018", "Sep 2016", "Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017",
"Feb 2017", "Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017",
"Aug 2017", "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018",
"Feb 2018", "Mar 2018", "Apr 2018", "May 2018", "Jun 2018", "Jul 2018",
"Aug 2018", "Sep 2018", "Oct 2018", "Nov 2018", "Dec 2018", "Sep 2016",
"Oct 2016", "Nov 2016", "Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017",
"Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017",
"Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018", "Mar 2018",
"Apr 2018", "May 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Oct 2018", "Nov 2018", "Dec 2018"), value = c(0, 13, 1397, 12134,
6938, 13193, 6947, 6990, 7112, 7233, 13838, 18109, 9700, 19612,
18048, 6441, 7528, 9947, 8073, 5862, 5991, 5501, 6294, 6960,
5350, 4479, 3612, 3143, 0, 12845, 28060, 35813, 37810, 35687,
39469, 37079, 34033, 29262, 31483, 29198, 22719, 19749, 17035,
18740, 19646, 16118, 18970, 18930, 23074, 22650, 25215, 23299,
21329, 19246, 20893, 17106, 0, 11964, 33654, 54440, 51427, 55324,
51136, 41176, 40035, 33106, 34155, 34207, 26175, 21376, 16997,
17213, 18211, 15071, 17266, 15598, 15431, 16155, 16082, 18322,
17276, 17654, 17282, 14001, 0, 737, 3225, 9009, 9324, 9780, 7361,
7584, 12033, 24794, 25236, 27493, 27189, 20672, 17579, 17706,
19280, 17461, 18950, 14288, 15485, 15241, 15148, 16124, 14720,
15279, 16888, 13580, 0, 4020, 21926, 27982, 32928, 39744, 46413,
34679, 35026, 32160, 33611, 33234, 29394, 23364, 20977, 23407,
26501, 21561, 23497, 20090, 21115, 18817, 18255, 21492, 21852,
19908, 18862, 16447, 0, 218, 2611, 8126, 5907, 10862, 5104, 4956,
13169, 22457, 25120, 18480, 15580, 14720, 13146, 16750, 16321,
18067, 15336, 13460, 12949, 9010, 10691, 12153, 13791, 16114,
12893, 10122, 0, 60, 1143, 4251, 3754, 6211, 3291, 2570, 5673,
8171, 9279, 12914, 9675, 7521, 5621, 9473, 7104, 12808, 10212,
8591, 6588, 5055, 6374, 6811, 8072, 11054, 9431, 9558, 0, 122,
1004, 2713, 2554, 2642, 3946, 4355, 3526, 3945, 3792, 5631, 7642,
11354, 13887, 12825, 16926, 15920, 14848, 13762, 13429, 10786,
11194, 11214, 9251, 11578, 13808, 14115, 2, 43253, 213319, 332907,
366366, 368012, 392814, 302207, 283924, 260065, 274796, 269966,
235781, 190387, 166872, 195390, 207261, 166208, 176403, 178703,
192598, 191750, 207203, 211225, 184818, 192932, 214297, 179867
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-252L))
my_ds <- df %>%
group_by(fips) %>%
do(item = list(
fips = first(.$fips),
sequence = .$value,
value = first(.$value))) %>%
.$item
url <- "https://code.highcharts.com/mapdata/custom/world.js"
tmpfile <- tempfile(fileext = ".json")
download.file(url, tmpfile)
us <- readLines(tmpfile)
us <- gsub(".* = ", "", us)
map <- jsonlite::fromJSON(us, simplifyVector = FALSE)
highchart(type = "map") %>%
hc_add_series(data = my_ds,
mapData = map,
joinBy = "fips",
borderWidth = 0.01) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_title(text = "How the Epidemic of Drug Overdose Deaths Ripples") %>%
hc_legend(layout = "vertical", reversed = TRUE,
floating = TRUE, align = "right") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_motion(
enabled = TRUE,
axisLabel = "year",
labels = unique(as.character(df$sm)),
series = 0,
updateIterval = 50,
magnet = list(
round = "floor",
step = 0.1
)
)
The issue is that the map data of the world you use has no fips (these are US county codes) so joinBy="fips" cannot work the way you expect it to.
The country codes of your dataset correspond to the worldmaps iso-a3 codes. If you use these instead of fips it will work as expected:
my_ds <- df %>% rename(`iso-a3` = fips) %>%
group_by(`iso-a3`) %>%
do(item = list(
`iso-a3` = first(.$`iso-a3`),
sequence = .$value,
value = first(.$value))) %>%
.$item
highchart(type = "map") %>%
hc_add_series(data = my_ds,
mapData = map,
joinBy = "iso-a3",
borderWidth = 0.01) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_title(text = "How the Epidemic of Drug Overdose Deaths Ripples") %>%
hc_legend(layout = "vertical", reversed = TRUE,
floating = TRUE, align = "right") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_motion(
enabled = TRUE,
axisLabel = "year",
labels = unique(as.character(df$sm)),
series = 0,
updateIterval = 50,
magnet = list(
round = "floor",
step = 0.1
)
)