I have a current DataFrame that looks like this:
DATETIME MEAS_AVG TARG_MIN TARG_AVG TARG_MAX DESPORT_NOTE 1 2012/04/10 14:03:37 0.2888 0.22 0.25 0.27 GOOD_PT 2 2012/03/30 07:48:17 0.2544 0.22 0.25 0.27 GOOD_PT 3 2012/03/24 19:23:08 0.2333 0.22 0.25 0.27 GOOD_PT 4 2012/03/25 16:10:17 0.2111 0.22 0.25 0.27 GOOD_PT 5 2012/04/10 00:58:29 0.2222 0.22 0.25 0.27 GOOD_PT 6 2012/04/14 18:32:52 0.2888 0.22 0.25 0.27 GOOD_PT 7 2012/04/21 14:47:47 0.2777 0.22 0.25 0.27 GOOD_PT
The data frame is called df3
and the specific column I am looking to replace the dates for are df3$DATETIME
.
I have this function in my code already in order to strip the datetime:
date <- strptime(df3$DATETIME, "%Y/%m/%d %H:%M:%S")
All I am looking to replace all the datetime information with simple month names. This is what it should look like after the replace function:
DATETIME MEAS_AVG TARG_MIN TARG_AVG TARG_MAX DESPORT_NOTE 1 April 0.2888 0.22 0.25 0.27 GOOD_PT 2 March 0.2544 0.22 0.25 0.27 GOOD_PT 3 March 0.2333 0.22 0.25 0.27 GOOD_PT 4 March 0.2111 0.22 0.25 0.27 GOOD_PT 5 April 0.2222 0.22 0.25 0.27 GOOD_PT 6 April 0.2888 0.22 0.25 0.27 GOOD_PT 7 April 0.2777 0.22 0.25 0.27 GOOD_PT
I am been looking all over for a simple replace column function, but can't seem to find it. I know that I can use the as.Date()
function with the formated %B
to return the unabreviated month. The only problem is I do not know how to use this to replace the column values already existing.
I can list the months using this function:
list(month=months(as.Date(df3$DATETIME)))
You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.
To format = , provide a character string (in quotes) that represents the current date format using the special “strptime” abbreviations below. For example, if your character dates are currently in the format “DD/MM/YYYY”, like “24/04/1968”, then you would use format = "%d/%m/%Y" to convert the values into dates.
R has developed a special representation for dates and times. Dates are represented by the Date class and times are represented by the POSIXct or the POSIXlt class. Dates are stored internally as the number of days since 1970-01-01 while times are stored internally as the number of seconds since 1970-01-01.
df3$DATETIME <- months(as.Date(df3$DATETIME))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With