Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace numeric dates with month names for a column in R

Tags:

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))) 
like image 964
Jonny Avatar asked Nov 05 '12 20:11

Jonny


People also ask

How do I change a date format into a column in R?

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.

How do I change date format in R?

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.

How are dates represented in R?

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.


1 Answers

df3$DATETIME <- months(as.Date(df3$DATETIME)) 
like image 182
seancarmody Avatar answered Oct 07 '22 22:10

seancarmody