Related
I'm trying to merge two datasets I have.
df1:
day
month
year
lon
lat
month-year
3
5
2009
5.7
53.9
May 2009
8
9
2004
6.9
52.6
Sep 2004
15
9
2004
3.8
50.4
Sep 2004
5
5
2009
2.7
51.2
May 2009
28
7
2005
14.8
62.4
Jul 2005
18
9
2004
5.1
52.5
Sep 2004
df2:
nao-value
sign
month-year
- 2.1
Negative
Sep 2004
1.3
Positive
Jul 2005
- 1.1
Negative
May 2009
I want to merge this to add the NAO value for each month and year in the occurrence data, meaning i want the NAO value for each specific month repeated for all registrations of that month in the occurrence data.
Problem is I cannot get the NAO values to line up where it should by the occurrence data, its either placed just repetitive and not aligned with the date it should, given as month-year.x and month-year.y ,or it is given back as NA value.
I have tried a few different approaches:
df3 <- merge(df1, df2, by="month-year")
df3 <- merge(cbind(df1, X=rownames(df1)), cbind(df2, variable=rownames(df2)))
df3 <- merge(df1,df2, by ="month-year", all.x = TRUE,all.y=TRUE, sort = FALSE)
df3 <- merge(df1, df2, by=intersect(df1$month-year(df1), df2$month-year(df2)))
But not of those give the result I desire.
Edit to include dput:
dput(head(df1, 10)) :
structure(list(Day = c(29, 2, 14, 31, 16, 7, 25, 12, 21, 22),
Month = c(7, 7, 7, 8, 8, 7, 8, 6, 6, 9), Year = c(2010, 2015,
2010, 2018, 2016, 2018, 2019, 2004, 2015, 2019), Lon = c(-6.155014,
-5.820868, -5.509842, -5.495277, -5.469389, -5.469389, -5.469389,
-5.466995, -5.461942, -5.457127), Lat = c(59.09478, 59.125228,
57.959196, 57.96022, 57.986825, 57.986825, 57.986825, 57.874527,
57.95972, 58.07697), Date = c("Jul 2010", "Jul 2015", "Jul 2010",
"Aug 2018", "Aug 2016", "Jul 2018", "Aug 2019", "Jun 2004",
"Jun 2015", "Sep 2019")), row.names = c(NA, -10L), class =
c("tbl_df",
"tbl", "data.frame"))
dput(head(df2, 10)) :
structure(list(NAO = c(1.04, 1.41, 1.46, 2, -1.53, -0.02, 0.53,
0.97, 1.06, 0.23), Sign = c("Positive", "Positive", "Positive",
"Positive", "Negative", "Negative", "Positive", "Positive",
"Positive",
"Positive"), Date = c("jan 1990", "feb 1990", "mar 1990", "apr 1990",
"mai 1990", "jun 1990", "jul 1990", "aug 1990", "sep 1990", "okt
1990"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
merge function is case sensitive. You have different cases in two dataframes that you are merging. Make the case in both the dataframe same and then perform the merge. Try -
result <- merge(transform(df1, Date = tolower(Date)), df2, by = 'Date')
Using tidyverse
library(dplyr)
df1 %>%
mutate(Date = tolower(Date)) %>%
inner_join(df2, by = 'Date')
I have a date column that contains dates public opinion polls occur.
These polls occasionally run over several days (usually but not always continuously), the polls sometimes start in one month and finish in the next, and the year has occasionally been entered as YY and other times as YYYY.
Where there is date range it's normally separated using a -, but sometimes – has been used, there are sometimes spaces between dates in a range.
I need to clean this into a consistent date format with a start_date and end_date column. Where polls occur on a single day I'd the end_date columns should be NA or filled with the start date (if you have a solution that does either I can always work from there to do the reverse if needed). Where there are non-continuous date ranges, the earliest date and latest date and the intermediate stop and restarts can be discarded.
Because the formatting is so annoyingly inconsistent I've provided the full the data since any solution would need to work on all dates in the date set (or work on some of them and not break the others so we could solve the issue iteratively).
dates <- c("12-15 Feb 2019", "6–11 Feb 2019", "7–10 Feb 2019", "23–30 Jan 2019",
"24–27 Jan 2019", "9–13 Jan 2019", "13-16 Dec 2018", "13–15 Dec 2018",
"6–9 Dec 2018", "29 Nov – 2 Dec 2018", "23–25 Nov 2018", "15-18 Nov 2018",
"15–17 Nov 2018", "8–11 Nov 2018", "1–4 Nov 2018", "25–28 Oct 2018",
"19–21 Oct 2018", "10–13 Oct 2018", "10–13 Oct 2018", "5–7 Oct 2018",
"22–24 Sep 2018", "20–23 Sep 2018", "12–15 Sep 2018", "8–10 Sep 2018",
"6–9 Sep 2018", "25–26 Aug 2018", "24–26 Aug 2018", "24–25 Aug 2018",
"15-18 Aug 2018", "12-Aug-18", "06-Aug-18", "29-Jul-18", "17-Jul-18",
"16-Jul-18", "03-Jul-18", "02-Jul-18", "21–24 Jun 2018", "14–17 Jun 2018",
"14–17 Jun 2018", "02-Jun-18", "31 May – 3 Jun 2018", "24–27 May 2018",
"17–20 May 2018", "10–13 May 2018", "10–13 May 2018", "10–12 May 2018",
"3–6 May 2018", "30-Apr-18", "19–22 Apr 2018", "22-Apr-18", "5–8 Apr 2018",
"5–8 Apr 2018", "3–5 Apr 2018", "24 Mar – 1 Apr 2018", "28-Mar-18",
"22–25 Mar 2018", "22–25 Mar 2018", "17–25 Mar 2018", "8–11 Mar 2018",
"3–11 Mar 2018", "1–4 Mar 2018", "22–25 Feb 2018", "24-Feb-18",
"15–18 Feb 2018", "8–11 Feb 2018", "1–3 Feb 2018", "26–28 Jan 2018",
"25-Jan-18", "11–15 Jan 2018", "19-Dec-17", "14–17 Dec 2017",
"12-Dec-17", "7–10 Dec 2017", "05-Dec-17", "30 Nov ? 3 Dec 2017",
"29-Nov-17", "28-Nov-17", "23–27 Nov 2017", "21-Nov-17", "14-Nov-17",
"14-Nov-17", "13-Nov-17", "30-Oct-17", "26–29 Oct 2017", "24-Oct-17",
"12–15 Oct 2017", "04-Oct-17", "01-Oct-17", "26-Sep-17", "21–24 Sep 2017",
"19-Sep-17", "14–18 Sep 2017", "12-Sep-17", "6–9 Sep 2017", "05-Sep-17",
"31 Aug – 4 Sep 2017", "28 Aug – 2 Sep 2017", "29-Aug-17", "23-Aug-17",
"22-Aug-17", "17–21 Aug 2017", "17–20 Aug 2017", "15-Aug-17",
"08-Aug-17", "3–6 Aug 2017", "01-Aug-17", "25-Jul-17", "20–24 Jul 2017",
"20–23 Jul 2017", "19-Jul-17", "18-Jul-17", "6–11 Jul 2017",
"6–9 Jul 2017", "29-Jun-17", "22–27 Jun 2017", "15–18 Jun 2017",
"14-Jun-17", "26–29 May 2017", "23-May-17", "12–15 May 2017",
"11-May-17", "10–11 May 2017", "26–30 Apr 2017", "20–23 Apr 2017",
"13–16 Apr 2017", "6–9 Apr 2017", "1–4 Apr 2017", "30 Mar – 2 Apr 2017",
"24–27 Mar 2017", "22–25 Mar 2017", "17–20 Mar 2017", "16–19 Mar 2017",
"10–13 Mar 2017", "3–6 Mar 2017", "23–26 Feb 2017", "16–19 Feb 2017",
"9–12 Feb 2017", "2–5 Feb 2017", "20–23 Jan 2017", "13–16 Jan 2017",
"12-Jan-17", "9–12 Dec 2016", "1–4 Dec 2016", "25–28 Nov 2016",
"24–26 Nov 2016", "17–20 Nov 2016", "11–14 Nov 2016", "3–6 Nov 2016",
"20–23 Oct 2016", "14–17 Oct 2016", "7–10 Oct 2016", "6–9 Oct 2016",
"22–25 Sep 2016", "9–12 Sep 2016", "8–11 Sep 2016", "26–29 Aug 2016",
"25–28 Aug 2016", "19–22 Aug 2016", "12–15 Aug 2016", "5–8 Aug 2016",
"27 Jul – 1 Aug 2016", "20–24 Jul 2016", "13–17 Jul 2016", "6–10 Jul 2016",
"30 Jun – 3 Jul 2016", "28 Jun – 1 Jul 2016", "30-Jun-16", "27–30 Jun 2016",
"28–29 Jun 2016", "26–29 Jun 2016", "28 Jun – 1 Jul 2016", "30-Jun-16",
"27–30 Jun 2016", "28–29 Jun 2016", "26–29 Jun 2016", "23–26 Jun 2016",
"23–26 Jun 2016", "23-Jun-16", "20–22 Jun 2016", "16–19 Jun 2016",
"16–19 Jun 2016", "16-Jun-16", "14–16 Jun 2016", "9–12 Jun 2016",
"09-Jun-16", "2–5 Jun 2016", "2–5 Jun 2016", "02-Jun-16", "31 May – 2 Jun 2016",
"26–29 May 2016", "21–22,\n 28–29 May 2016",
"26-May-16", "19–22 May 2016", "19–22 May 2016", "19-May-16",
"17–19 May 2016", "14–15 May 2016", "12–15 May 2016", "6–8 May 2016",
"5–8 May 2016", "5–8 May 2016", "5–7 May 2016", "4–6 May 2016",
"05-May-16", "27 Apr – 1 May 2016", "23–24, 30 Apr – 1 May 2016",
"20–24 Apr 2016", "14–17 Apr 2016", "13–17 Apr 2016", "9–10,\n 16–17 Apr 2016",
"14–16 Apr 2016", "14-Apr-16", "6–10 Apr 2016", "31 Mar – 3 Apr 2016",
"26–27 Mar, 2–3 Apr 2016", "21-Mar-16", "17–20 Mar 2016", "16–20 Mar 2016",
"12–13,\n 19–20 Mar 2016", "10–12 Mar 2016",
"3–6 Mar 2016", "2–6 Mar 2016", "27–28 Feb, 5–6 Mar 2016", "24–28 Feb 2016",
"18–21 Feb 2016", "17–21 Feb 2016", "13–14, 20–21 Feb 2016",
"11–13 Feb 2016", "11-Feb-16", "3–7 Feb 2016", "30–31 Jan,\n 6–7 Feb 2016",
"28–31 Jan 2016", "16–17, 23–24 Jan 2016", "21-Jan-16", "15–18 Jan 2016",
"2–3, 9–10 Jan 2016", "15-Dec-15", "5–6, 12–13 Dec 2015", "08-Dec-15",
"4–6 Dec 2015", "01-Dec-15", "21–22, 28–29 Nov 2015", "26-Nov-15",
"24-Nov-15", "19–22 Nov 2015", "7–8, 14–15 Nov 2015", "12–14 Nov 2015",
"10-Nov-15", "6–8 Nov 2015", "03-Nov-15", "24–25 Oct,\n 1 Nov 2015",
"27-Oct-15", "23–25 Oct 2015", "22-Oct-15", "20-Oct-15", "10–11, 17–18 Oct 2015",
"15–17 Oct 2015", "13-Oct-15", "9–11 Oct 2015", "26–27 Sep, 1–5 Oct 2015",
"1–4 Oct 2015", "24–28 Sep 2015", "17–21 Sep 2015", "19–20 Sep 2015",
"17–20 Sep 2015", "15–16 Sep 2015", "15-Sep-15", "12–13 Sep 2015",
"5–6 Sep 2015", "4–6 Sep 2015", "26–30 Aug 2015", "27-Aug-15",
"22–23 Aug 2015", "20–23 Aug 2015", "13–15 Aug 2015", "11–14 Aug 2015",
"8–9 Aug 2015", "8–9 Aug 2015", "4–7 Aug 2015", "06-Aug-15",
"28–31 Jul 2015", "30-Jul-15", "25–26 Jul 2015", "16–19 Jul 2015",
"14–17 Jul 2015", "11–12 Jul 2015", "4–5 Jul 2015", "2–4 Jul 2015",
"27–28 Jun 2015", "16-Jun-15", "16-Jun-15", "13–14 Jun 2015",
"11–13 Jun 2015", "11–13 Jun 2015", "02-Jun-15", "02-Jun-15",
"23–24, 30–31 May 2015", "26-May-15", "18-May-15", "17-May-15",
"17-May-15", "13-May-15", "7–10 May 2015", "04-May-15", "04-May-15",
"28-Apr-15", "21-Apr-15", "11–12,\n 18–19 Apr 2015",
"14-Apr-15", "10–12 Apr 2015", "9–11 Apr 2015", "28–29 Mar, 3–6 Apr 2015",
"29-Mar-15", "20–22 Mar 2015", "14–15, 21–22 Mar 2015", "17-Mar-15",
"10-Mar-15", "7–8 Mar 2015", "28 Feb–1, 7–8 Mar 2015", "26–28 Feb 2015",
"20–22 Feb 2015", "20–22 Feb 2015", "6–8 Feb 2015", "31 Jan–1, 7–8 Feb 2015",
"05-Feb-15", "4–5 Feb 2015", "28–30 Jan 2015", "27-Jan-15", "r27 Jan 2015",
"20-Jan-15", "13-Jan-15", "12-Jan-15", "23–27 Dec 2014", "16-Dec-14",
"12–15 Dec 2014", "6–7, 13–14 Dec 2014", "4–6 Dec 2014", "2–4 Dec 2014",
"02-Dec-14", "29–30 Nov 2014", "22–23, 29–30 Nov 2014", "25-Nov-14",
"21-Nov-14", "18-Nov-14", "17-Nov-14", "17-Nov-14", "11-Nov-14",
"04-Nov-14", "04-Nov-14", "25–26 Oct,\n 1–2 Nov 2014",
"30 Oct–1 Nov 2014", "28-Oct-14", "23-Oct-14", "21-Oct-14", "21-Oct-14",
"20-Oct-14", "14-Oct-14", "07-Oct-14", "4–5 Oct 2014", "4–5 Oct 2014",
"23-Sep-14", "13–14,\n 20–21 Sep 2014",
"18-Sep-14", "30–31 Aug, 6–7 Sep 2014", "5–7 Sep 2014", "22–24 Aug 2014",
"16–17, 23–24 Aug 2014", "19-Aug-14", "9–10 Aug 2014", "8–10 Aug 2014",
"25–27 Jul 2014", "11–13 Jul 2014", "01-Jul-14", "30-Jun-14",
"27–29 Jun 2014", "13–15 Jun 2014", "30 May–1 Jun 2014", "27-May-14",
"20-May-14", "17–18 May 2014", "16–18 May 2014", "15–17 May 2014",
"04-May-14", "2–4 May 2014", "30-Apr-14", "22-Apr-14", "15-Apr-14",
"13-Apr-14", "08-Apr-14", "07-Apr-14", "4–6 Apr 2014", "25-Mar-14",
"25-Mar-14", "21–23 Mar 2014", "18-Mar-14", "13–15 Mar 2014",
"7–9 Mar 2014", "05-Mar-14", "23-Feb-14", "21–23 Feb 2014", "15-Feb-14",
"7–9 Feb 2014", "28-Jan-14", "23-Jan-14", "17–20 Jan 2014", "13-Jan-14",
"16-Dec-13", "15-Dec-13", "6–8 Dec 2013", "28 Nov–2 Dec 2013",
"30 Nov–1 Dec 2013", "22–24 Nov 2013", "21–23 Nov 2013", "8–10 Nov 2013",
"25–27 Oct 2013", "19–20 Oct 2013", "21–22 Sep 2013", "19–22 Sep 2013",
"12–15 Sep 2013", "4–6 Sep 2013", "05-Sep-13", "3–5 Sep 2013",
"4–6 Sep 2013", "05-Sep-13", "4–5 Sep 2013", "3–5 Sep 2013",
"04-Sep-13", "2–4 Sep 2013", "1–4 Sep 2013", "03-Sep-13", "30 Aug–1 Sep 2013",
"30 Aug–1 Sep 2013", "29 Aug–1 Sep 2013", "28–29 Aug 2013", "28–29 Aug 2013",
"26-Aug-13", "21–25 Aug 2013", "23–25 Aug 2013", "23–25 Aug 2013",
"18–22 Aug 2013", "16–18 Aug 2013", "16–18 Aug 2013", "16–18 Aug 2013",
"14–18 Aug 2013", "14–15 Aug 2013", "12–13 Aug 2013", "9–12 Aug 2013",
"9–11 Aug 2013", "9–11 Aug 2013", "10-Aug-13", "7–9 Aug 2013",
"6–8 Aug 2013", "04-Aug-13", "2–4 Aug 2013", "2–4 Aug 2013",
"1–4 Aug 2013", "26–28 Jul 2013", "25–28 Jul 2013", "23–25 Jul 2013",
"18–22 Jul 2013", "19–21 Jul 2013", "19–21 Jul 2013", "18-Jul-13",
"12–14 Jul 2013", "11–14 Jul 2013", "11–13 Jul 2013", "5–8 Jul 2013",
"5–7 Jul 2013", "5–7 Jul 2013", "4–7 Jul 2013", "28–30 Jun 2013",
"28–30 Jun 2013", "27–30 Jun 2013", "27–28 Jun 2013", "27-Jun-13",
"21–23 Jun 2013", "21–23 Jun 2013", "20–23 Jun 2013", "14–16 Jun 2013",
"13–16 Jun 2013", "13–15 Jun 2013", "11–13 Jun 2013", "7–10 Jun 2013",
"6–10 Jun 2013", "31 May–2 Jun 2013", "31 May–2 Jun 2013", "30 May–2 Jun 2013",
"24–26 May 2013", "23–26 May 2013", "17–19 May 2013", "17–19 May 2013",
"16–19 May 2013", "16–18 May 2013", "15–16 May 2013", "10–12 May 2013",
"9–12 May 2013", "3–5 May 2013", "3–5 May 2013", "2–5 May 2013",
"02-May-13", "26–28 Apr 2013", "25–28 Apr 2013", "18–22 Apr 2013",
"18–22 Apr 2013", "19–21 Apr 2013", "11–14 Apr 2013", "11–14 Apr 2013",
"11–13 Apr 2013", "9–11 Apr 2013", "02-May-13", "5–7 Apr 2013",
"4–7 Apr 2013", "4–7 Apr 2013", "29 Mar–1 Apr 2013", "28 Mar–1 Apr 2013",
"22–24 Mar 2013", "21–24 Mar 2013", "22–23 Mar 2013", "21–24 Mar 2013",
"22–25 Mar 2013", "14–17 Mar 2013", "14–17 Mar 2013", "14–16 Mar 2013",
"7–10 Mar 2013", "7–10 Mar 2013", "8–10 Mar 2013", "5–7 Mar 2013",
"28 Feb–3 Mar 2013", "28 Feb–3 Mar 2013", "21–24 Feb 2013", "16–17/23–24 Feb 2013",
"22–24 Feb 2013", "14–17 Feb 2013", "14–16 Feb 2013", "7–10 Feb 2013",
"9–10 Feb 2013", "1–4 Feb 2013", "2–3 Feb 2013", "1–3 Feb 2013",
"1–3 Feb 2013", "23–28 Jan 2013", "19–20/26–27 Jan 2013", "16–20 Jan 2013",
"9–13 Jan 2013", "11–13 Jan 2013", "5–6/12–13 Jan 2013", "12–16 Dec 2012",
"8–9/15–16 Dec 2012", "13–15 Dec 2012", "5–9 Dec 2012", "7–9 Dec 2012",
"28 Nov–2 Dec 2012", "24–25 Nov/1–2 Dec 2012", "29–30 Nov 2012",
"27–29 Nov 2012", "23–25 Nov 2012", "21–25 Nov 2012", "14–18 Nov 2012",
"10–11/17–18 Nov 2012", "15–17 Nov 2012", "9–11 Nov 2012", "7–11 Nov 2012",
"2–6 Nov 2012", "2–4 Nov 2012", "27–28 Oct/3–4 Nov 2012", "26–28 Oct 2012",
"25–28 Oct 2012", "13–14/20–21 Oct 2012", "17–21 Oct 2012", "18–20 Oct 2012",
"10–14 Oct 2012", "5–7 Oct 2012", "3–7 Oct 2012", "29–30 Sep/6–7 Oct 2012",
"26–30 Sep 2012", "22–23 Sep 2012", "19–23 Sep 2012", "17–20 Sep 2012",
"14–16 Sep 2012", "12–16 Sep 2012", "8–9/15–16 Sep 2012", "13–15 Sep 2012",
"29 Aug–2 Sep 2012", "31 Aug–2 Sep 2012", "1–2 Sep 2012", "22–26 Aug 2012",
"23–25 Aug 2012", "15–19 Aug 2012", "17–19 Aug 2012", "11–12/18–19 Aug 2012",
"8–12 Aug 2012", "3–5 Aug 2012", "1–5 Aug 2012", "28–29 Jul/4–5 Aug 2012",
"25–29 Jul 2012", "26–28 Jul 2012", "20–22 Jul 2012", "18–22 Jul 2012",
"14–15/21–22 Jul 2012", "11–15 Jul 2012", "6–8 Jul 2012", "4–8 Jul 2012",
"30 Jun–1/7–8 Jul 2012", "27 Jun–1 Jul 2012", "22–24 Jun 2012",
"20–24 Jun 2012", "16–17/23–24 Jun 2012", "13–17 Jun 2012", "15–17 Jun 2012",
"6–11 Jun 2012", "9–10 Jun 2012", "7–10 Jun 2012", "2–3 Jun 2012",
"31 May–2 Jun 2012", "30 May–3 Jun 2012", "26–27 May 2012", "23–27 May 2012",
"25–27 May 2012", "16–20 May 2012", "19–20 May 2012", "12–13 May 2012",
"11–13 May 2012", "9–13 May 2012", "9–10 May 2012", "9–10 May 2012",
"5–6 May 2012", "2–6 May 2012", "27–29 Apr 2012", "27–29 Apr 2012",
"25–29 Apr 2012", "21–22 Apr 2012", "18–22 Apr 2012", "17–19 Apr 2012",
"13–15 Apr 2012", "11–15 Apr 2012", "7–8/14–15 Apr 2012", "4–9 Apr 2012",
"31 Mar–1 Apr 2012", "28 Mar–1 Apr 2012", "29–31 Mar 2012", "21–25 Mar 2012",
"24–25 Mar 2012", "23–25 Mar 2012", "14–18 Mar 2012", "10–11/17–18 Mar 2012",
"9–11 Mar 2012", "7–11 Mar 2012", "3–4 Mar 2012", "29 Feb–4 Mar 2012",
"25–26 Feb 2012", "23–26 Feb 2012", "22–26 Feb 2012", "23–24 Feb 2012",
"22–23 Feb 2012", "15–19 Feb 2012", "11–12/18–19 Feb 2012", "10–12 Feb 2012",
"8–10 Feb 2012", "7–8 Feb 2012", "4–5 Feb 2012", "1–5 Feb 2012",
"2–4 Feb 2012", "28–29 Jan 2012", "27–29 Jan 2012", "25–29 Jan 2012",
"27–28 Jan 2012", "18–22 Jan 2012", "14–15/21–22 Jan 2012", "17–18 Jan 2012",
"11–15 Jan 2012", "7–8 Jan 2012", "14–18 Dec 2011", "10–11/17–18 Dec 2011",
"7–11 Dec 2011", "8–10 Dec 2011", "2–4 Dec 2011", "30 Nov–4 Dec 2011",
"26–27 Nov/3–4 Dec 2011", "23–27 Nov 2011", "19–20 Nov 2011",
"18–20 Nov 2011", "16–20 Nov 2011", "9–13 Nov 2011", "5–6/12–13 Nov 2011",
"10–12 Nov 2011", "3–6 Nov 2011", "2–6 Nov 2011", "2–3 Nov 2011",
"26–30 Oct 2011", "29–30 Oct 2011", "25–26 Oct 2011", "22–23 Oct 2011",
"21–23 Oct 2011", "19–23 Oct 2011", "15–16Oct 2011", "14–16 Oct 2011",
"12–16 Oct 2011", "13–15 Oct 2011", "8–9 Oct 2011", "7–9 Oct 2011",
"4–9 Oct 2011", "27 Sep–2 Oct 2011", "24–25 Sep/1–2 Oct 2011",
"20–25 Sep 2011", "16–18 Sep 2011", "13–18 Sep 2011", "10–11/17–18 Sep 2011",
"7–11 Sep 2011", "8–10 Sep 2011", "2–4 Sep 2011", "31 Aug–4 Sep 2011",
"27–28 Aug/3–4 Sep 2011", "24–28 Aug 2011", "19–21 Aug 2011",
"17–21 Aug 2011", "13–14/20–21 Aug 2011", "10–14 Aug 2011", "11–13 Aug 2011",
"9–10 Aug 2011", "5–7 Aug 2011", "3–7 Aug 2011", "30–31 Jul/6–7 Aug 2011",
"c. 3 Aug 2011", "27–31 Jul 2011", "22–24 Jul 2011", "20–24 Jul 2011",
"16–17/23–24 Jul 2011", "13–17 Jul 2011", "14–16 Jul 2011", "13–14 Jul 2011",
"9–10 Jul 2011", "8–10 Jul 2011", "6–10 Jul 2011", "29 Jun–3 Jul 2011",
"25–26 Jun/1–2 Jul 2011", "24–26 Jun 2011", "22–26 Jun 2011",
"11–12/18–19 Jun 2011", "15–19 Jun 2011", "14–16 Jun 2011", "8–13 Jun 2011",
"10–12 Jun 2011", "4–5 Jun 2011", "1–5 Jun 2011", "31 May–2 Jun 2011",
"25–29 May 2011", "27–29 May 2011", "21–22/28–29 May 2011", "18–22 May 2011",
"14–15 May 2011", "13–15 May 2011", "11–15 May 2011", "12–14 May 2011",
"7–8 May 2011", "4–8 May 2011", "3–4 May 2011", "29 Apr–1 May 2011",
"28 Apr–1 May 2011", "23–24/30 Apr–1 May 2011", "20–26 Apr 2011",
"13–17 Apr 2011", "9–10/16–17 Apr 2011", "14–16 Apr 2011", "6–10 Apr 2011",
"2–3 Apr 2011", "1–3 Apr 2011", "30 Mar–3 Apr 2011", "26–27 Mar 2011",
"23–27 Mar 2011", "22–24 Mar 2011", "19–20 Mar 2011", "18–20 Mar 2011",
"16–20 Mar 2011", "16–17 Mar 2011", "12–13 Mar 2011", "9–13 Mar 2011",
"10–12 Mar 2011", "8–10 Mar 2011", "5–6 Mar 2011", "4–6 Mar 2011",
"2–6 Mar 2011", "26–27 Feb 2011", "22–27 Feb 2011", "21–23 Feb 2011",
"18–20 Feb 2011", "15–20 Feb 2011", "12–13/19–20 Feb 2011", "8–13 Feb 2011",
"10–12 Feb 2011", "4–6 Feb 2011", "1–6 Feb 2011", "29–30 Jan/5–6 Feb 2011",
"1–3 Feb 2011", "25–30 Jan 2011", "18–23 Jan 2011", "15–16/22–23 Jan 2011",
"11–16 Jan 2011", "8–9 Jan 2011", "14–19 Dec 2010", "11–12 Dec 2010",
"8–12 Dec 2010", "7–12 Dec 2010", "4–5 Dec 2010", "3–5 Dec 2010",
"30 Nov–5 Dec 2010", "23–28 Nov 2010", "20–21/27–28 Nov 2010",
"19–21 Nov 2010", "16–21 Nov 2010", "18–20 Nov 2010", "9–14 Nov 2010",
"6–7/13–14 Nov 2010", "5–7 Nov 2010", "2–7 Nov 2010", "26–31 Oct 2010",
"23–24/30–31 Oct 2010", "22–24 Oct 2010", "19–24 Oct 2010", "21–23 Oct 2010",
"12–17 Oct 2010", "9–10/16–17 Oct 2010", "8–10 Oct 2010", "5–10 Oct 2010",
"2–3 Oct 2010", "30 Sep–1 Oct 2010", "21–26 Sep 2010", "18–19 Sep 2010",
"14–19 Sep 2010", "15–16 Sep 2010", "10–12 Sep 2010", "7–12 Sep 2010",
"31 Aug–5 Sep 2010", "28–29 Aug/4–5 Sep 2010", "24–29 Aug 2010",
"25–26 Aug 2010", "c. 21 Aug 2010", "17–19 Aug 2010", "13–19 Aug 2010"
)
Some particularly odd dates to look out for in the dataset
"30 Nov ? 3 Dec 2017"
"21–22,\n 28–29 May 2016"
"24–25 Oct,\n 1 Nov 2015"
"29–30 Sep/6–7 Oct 2012"
This was an interesting problem! But I think it can be solved with regex.
How about this:
library(tidyverse)
tibble(dates = dates) %>%
mutate(end_year = str_extract(dates, "[0-9]*$"),
end_year = ifelse(str_length(end_year) == 2, paste0("20", end_year), end_year),
month_one = str_extract(dates, "[A-Z][a-z][a-z]"),
month_two = str_sub(str_extract(dates, "[A-Z][a-z][a-z].*[A-Z][a-z][a-z]"), start = -3),
month_two = if_else(is.na(month_two), month_one, month_two),
day_one = str_extract(dates, "[0-9]+"),
dates_without_day_one = gsub("^[0-9]+", "", dates),
day_two = str_extract(dates_without_day_one, "[0-9]+"),
day_two = str_squish(gsub("[-–]", "", day_two)),
day_three_four = str_extract(dates, "/.+[-–] *[0-9]+"),
day_three = str_extract(day_three_four, "/ *[0-9]+"),
day_three = str_squish(gsub("/", "", day_three)),
day_four = str_extract(day_three_four, "[-–] *[0-9]+"),
day_four = str_squish(gsub("[-–]", "", day_four))
) %>%
# dates that are only a single day:
mutate(day_two = if_else(is.na(day_two), day_one, day_two)) %>%
# dates that actually have four days:
mutate(day_one = ifelse(is.na(day_three),
day_one,
round((as.numeric(day_one) + as.numeric(day_two)) / 2)),
day_two = ifelse(is.na(day_three),
day_two,
round((as.numeric(day_three) + as.numeric(day_four)) / 2))) %>%
select(-day_three_four, -dates_without_day_one) %>%
mutate(start_date = as.Date(paste(end_year,month_one, day_one, sep = "-"), format = "%Y-%b-%d"),
end_date = as.Date(paste(end_year,month_two, day_two, sep = "-"), format = "%Y-%b-%d")) %>%
select(dates, start_date, end_date, everything())
Delivers:
# A tibble: 838 x 10
dates start_date end_date end_year month_one month_two day_one day_two day_three day_four
<chr> <date> <date> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 12-15 Feb 2019 2019-02-12 2019-02-15 2019 Feb Feb 12 15 NA NA
2 6–11 Feb 2019 2019-02-06 2019-02-11 2019 Feb Feb 6 11 NA NA
3 7–10 Feb 2019 2019-02-07 2019-02-10 2019 Feb Feb 7 10 NA NA
4 23–30 Jan 2019 2019-01-23 2019-01-30 2019 Jan Jan 23 30 NA NA
5 24–27 Jan 2019 2019-01-24 2019-01-27 2019 Jan Jan 24 27 NA NA
6 9–13 Jan 2019 2019-01-09 2019-01-13 2019 Jan Jan 9 13 NA NA
7 13-16 Dec 2018 2018-12-13 2018-12-16 2018 Dec Dec 13 16 NA NA
8 13–15 Dec 2018 2018-12-13 2018-12-15 2018 Dec Dec 13 15 NA NA
9 6–9 Dec 2018 2018-12-06 2018-12-09 2018 Dec Dec 6 9 NA NA
10 29 Nov – 2 Dec 2018 2018-11-29 2018-12-02 2018 Nov Dec 29 2 NA NA
I was faced with the same issue having to deal with documents from all around the world. The best answer is to force the date format upstream when creating the input formular.
This answer will not solve your exact problem of date ranges, but you could tweak the solution I've brough here to deal with it. I used what's called regular expression patterns. I paste here the patterns i've used in a similar solution in python.
# 2019/02/20 or 2019-02-20
(?:|[\s\/\.:])+(\d{4})[\/\-\.\s](\d{2})[\/\-\.\s](\d{2})(?:$|[\s\/\.\-])+
# 02/20/2019 or 20/02/2019
(?:|[\/\s\.:])+(\d{2})[\/\-\.\s](\d{2})[\/\-\.\s](\d{4})(?:$|[\/\s\.\-])+
# 20 Feb 2019 or 20-Feb-2019
(?:^|[\s\.:])+(\d{2})[\/\-\.\s]?([a-zA-Z]{2,3})[\/\-\.\s]?(\d{4})(?:$|[\s\.\-])+
# 2019 Feb 20
(?:^|[\s\.:])+(\d{4})[\/\-\.\s]?([a-zA-Z]{2,3})[\/\-\.\s]?(\d{2})(?:$|[\s\.\-])+
# February 20th, 2019
(?:^|[\s\.:])+([a-zA-Z]{3,15})\s(\d{1,2})\s?[a-zA-Z]{2},\s?(\d{2,4})(?:$|[\s\.\-])+
# Feb 20 2019 or February 20 2019
(?:^|[\s\.:])*([a-zA-Z]{3,15})[ _\-\/\\\.]?(\d{1,2})[ _\-\/\\\.](\d{2,4})(?:$|[\s\.\-])+
#20-FEB-2019
(?:^|[\s\.\-:])+(\d{1,2})[ _\-\/\\\.]([a-zA-Z]{3,15})[ _\-\/\\\.](\d{2,4})(?:$|[\s\.\-])+
#2019.Feb.20
(?:^|[\s\.\-:])+(\d{4})[ _\-\/\\\.]([a-zA-Z]{3,15})[ _\-\/\\\.](\d{1,2})(?:$|[\s\.\-])+
# 20 Feb. 2019
(?:^|[\s\.\-:])+(\d{2})[\/\-\.\s]([a-zA-Z]{3,15})[\/\-\.\s]{1,2}(\d{4})(?:$|[\s\.\-])+
These regular expressions patterns contain three groups, that you can extract and manually test in case you parse ambiguous dates (e.g.: 02/01/2019).
Before implementing the solution, I recommend you to proceed as follows:
make a list of all your dates to parse, one per line
paste them all in a regex tester (e.g.: online regex tester)
try (and eventually modify) each of the following patterns
verify you properly catch groups 1 to 3
check out the dates properly caught until no one is left in the list, and that no conflicts occur
implement a date parsing function in R
secretly wish there would be an international standard for dates
I'm new in R.
I've data about about deep sea species and my sampling from July 2016 to July 2017.
I want take histogram by month with using ggplot.
ggplot create histogram by monthly but sort the months alphabetically.
How can I create a histogram start from July 2016 ?
Thank you.
My code:
ggplot(af, aes(x=cl)) +\
geom_histogram(binwidth=2, color="red") +\
facet_wrap(~date, ncol=2, nrow=7) +\
scale_x_continuous(name="Length Class (mm)") +\
scale_y_continuous(name ="Number of Specimens")
af: Species which is interested in study
cl: Carapace length
structure(list(date = structure(c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_), .Label = c("july 2016", "aug 2016 ",
"sept 2016", "oct 2016", "nov 2016", "dec 2016", "jan 2017",
"feb 2017", "march 2017", "apr 2017", "may 2017", "june 2017",
"july 2017"), class = "factor"), cl = c(38.33, 32.47, 33.52,
38.33, 34.84, 34.66, 31.67, 47.87, 36.46, 52, 41, 40.41, 32.71,
35.5, 36.56)), row.names = c(NA, 15L), class = "data.frame")
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
)
)
I have a factor variable which I would to transform in Date. When I try using as.Date() It shows me the error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
My data are these:
[1] Aug 31, 2018 Aug 30, 2018 Aug 29, 2018 Aug 28, 2018 Aug 27, 2018 Aug 26, 2018
[7] Aug 25, 2018 Aug 24, 2018 Aug 23, 2018 Aug 22, 2018 Aug 21, 2018 Aug 20, 2018
[13] Aug 19, 2018 Aug 18, 2018 Aug 17, 2018 Aug 16, 2018 Aug 15, 2018 Aug 14, 2018
[19] Aug 13, 2018 Aug 12, 2018 Aug 11, 2018 Aug 10, 2018 Aug 09, 2018 Aug 08, 2018
[25] Aug 07, 2018 Aug 06, 2018 Aug 05, 2018 Aug 04, 2018 Aug 03, 2018 Aug 02, 2018
How could I transform in Date in the format %d-%m-%Y?
Try with format = "%b %d, %Y":
ss <- c("Aug 31, 2018", "Aug 30, 2018", "Aug 29, 2018", "Aug 28, 2018", "Aug 27, 2018", "Aug 26, 2018")
as.Date(ss, format = "%b %d, %Y")
#[1] "2018-08-31" "2018-08-30" "2018-08-29" "2018-08-28" "2018-08-27"
#[6] "2018-08-26"
For details what the different conversion specifications (e.g. "%b", "%Y" etc.) mean, take a look at ?strptime.
By the way, there is no need to manually convert from factor to character, because as.Date has a method for factors which converts factors to character vectors, see as.Date.factor.
specify the format within the function as.Date
as.Date(MyData, format="%b %d, %Y")
I've solved the problem getting this code Sys.setlocale("LC_TIME", "English") into the console!