data <- with(data, data[!(customer_id == "" | is.na(customer_id)), ])
data$date_of_purchase = as.Date(data$date_of_purchase, "%Y/%m/%d")
data$year_of_purchase = as.numeric(format(data$date_of_purchase, "%Y"))
data$days_since = as.numeric(difftime(time1 = "2019/03/25",
time2 = data$date_of_purchase,
units = "days"))
After running: data$date_of_purchase = as.Date(data$date_of_purchase, "%y/%m/%d"); the first row as an example had an initial date of: 16/12/2017 then the value became 0016-12-20.
I want the date to be displayed as 2017/12/16 and not the obtained value of 0016-12-20.
Your data (16/12/2017) is in the format DD/MM/YYYY, and hence the format "%Y/%m/%d" that you have used here doesn't work correctly. You should use "%d/%m/%Y". You can learn about these formats in the documentation of strptime.
Hope this helps.
For your future posts, please provide a REPRoducible EXample of your problem. It provides more specifics of your problem, and it helps others to understand what problem you are facing.
If you don't know how to do it, take a look at this thread:
Hi. Thanks for your response. I still cannot convert my data in the desired format. I have changed the format to "%d/%m/%Y but still get awkward results:
data = read.delim(file = 'purchases.txt', header = FALSE, sep = '\t', dec = '.')
Careful here, strptime will give you a POSIXlt datetime object, which is not much used anymore (POSIXct has some advantages). In this case, you're not working with datetimes anyway, so as.Date will suffice.
Also be aware that format returns character strings, not date or datetime objects, so you normally don't want to call it until you're presenting your results.