22

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)))
zx8754
  • 52,746
  • 12
  • 114
  • 209
Jonny
  • 619
  • 4
  • 12
  • 31

2 Answers2

31
df3$DATETIME <- months(as.Date(df3$DATETIME))
seancarmody
  • 6,182
  • 2
  • 34
  • 31
9

Rather than replace a column, you can overwrite the data in the unwanted one (and rename it if you want).

And to convert the data, I would use month() along with ymd(), both from lubridate package:

#install and load lubridate if not already done
install.packages("lubridate",repos="http://cran.us.r-project.org")
library(lubridate)

#transform data to month names
df3$DATETIME <-month(ymd(df3$DATETIME), label = TRUE, abbr = FALSE)

#rename if desired
names(df3)[which(names(df3)=="DATETIME")]<-"MONTH"

Addendum: The reason I suggest the lubridate functions rather than months() from base R is that months() returns char class while lubridate's month() returns an ordered factor, which can be useful:

> #base function
> m2=months(as.Date("08/12/1990"))
> m1=months(as.Date("07/12/1990"))
> m1<m2
[1] FALSE
> 
> #lubridate
> m2=month(mdy("08/12/1990"),label=TRUE, abbr=FALSE)
 1 parsed with %m/%d/%Y
> m1=month(mdy("07/12/1990"),label=TRUE, abbr=FALSE)
 1 parsed with %m/%d/%Y
> m1<m2
[1] TRUE
> m1
[1] July
12 Levels: January < February < March < April < May < June < ... < December
> str(m1)
 Ord.factor w/ 12 levels "January"<"February"<..: 7
MattBagg
  • 10,268
  • 3
  • 40
  • 47