I'm working on a problem where I need to merge two datasets. The first dataset is from SQL and imported using the RODBC library, while the second dataset is imported from Excel. I want to merge the two dataframes by month and year, however in order to do that, I need to convert the first DF's date column into year-month, from year-month-date.
I have tried to use as.Date(df$postingdate, format = '%Y %M'
or strftime(df$postingdate,"%Y %m")
as I normally would do, however the first doesn't work and the second changes the column to character. It has been a problem for days, and I have tried a number of things, mainly suggestions from the following link: [https://stackoverflow.com/questions/6242955/converting-year-and-month-yyyy-mm-format-to-a-date][1]
In the bottom I have created a df from output I get when using `dput()´ (df2) and I noticed that under posting date, the data is converted to a number, rather than the actual date (“2020-05-28”, “2020-10-09”, "2021-10-19"). Therefor I’m also unsure whatever I have problem because I use the wrong functions, or because the data is of a “unknown” data type.
A sample of the first dataset where I want to transform date into year – month:
df <- data.frame(
Posting_Date = c("2020-05-28", "2020-10-09", "2021-10-19"), Sales = c(2702.5, 369, 4134),
Sales_person_code = c(6L, 10L, 10L), EDI = c(1L, 1L, 1L),
City = c(141L, 4L, 6L), Kæde = c(12L, 12L, 12L),
Vinter = c(0, 0, 0), Forår = c(1, 0, 0), Sommer = c(0, 0, 0),
Efterår = c(0, 1, 1), Fredag = c(0, 1, 0), Lørdag = c(0, 0, 0),
Mandag = c(0, 0, 0), Onsdag = c(0, 0, 0), Søndag = c(0, 0, 0),
Tirsdag = c(0, 0, 1), Torsdag = c(1, 0, 0),
year_month = c("2020-05-28", "2020-10-09", "2021-10-19"))
df2 <- data.frame(
Posting_Date = c(18410, 18544, 18919), Sales = c(2702.5, 369, 4134),
Sales_person_code = c(6L, 10L, 10L),EDI = c(1L, 1L, 1L),
City = c(141L, 4L, 6L), Kæde = c(12L, 12L, 12L),
Vinter = c(0, 0, 0), Forår = c(1, 0, 0), Sommer = c(0, 0, 0),
Efterår = c(0, 1, 1), Fredag = c(0, 1, 0), Lørdag = c(0, 0, 0),
Mandag = c(0, 0, 0), Onsdag = c(0, 0, 0), Søndag = c(0, 0, 0),
Tirsdag = c(0, 0, 1), Torsdag = c(1, 0, 0),
year_month = c(18410, 18544, 18919))
Thanks in advance for any help. Plz let me know if i can do anything to help you guys, helping me