Home > Net >  Mapping substr of dataframe to new column [duplicate]
Mapping substr of dataframe to new column [duplicate]

Time:09-22

I would like to map the first/second characters of the column ï..Order.Date to a new column called Order.Month.

ï..Order.Date is a column in the data frame with string dates in M/D/YY or MM/DD/YY format:

"12/1/17" "9/1/17" "9/19/17" "9/1/17" "9/29/17"

I need to create a new column which uses the substring containing the first 1 or 2 characters, and map it to the month:

months = c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')

Still new to r and not sure the best way to approach this.

CodePudding user response:

We can convert the column to Date class, get the month extracted, convert to numeric and use as index to get the value that corresponds to from the built-in vector month.name

df1$Order.Month <- month.name[as.integer(format(as.Date(df1[["ï..Order.Date"]], 
        format =  "%m/%d/%y"), "%m"))]

Or use lubridate - convert to Date class with mdy, extract the month, use as index in month.name

library(lubridate)
month.name[month(mdy(df1[["ï..Order.Date"]]))]

CodePudding user response:

Once you create a date object you can use format(..., '%B') to get the month name.

x <- c("12/1/17", "9/1/17", "9/19/17", "9/1/17", "9/29/17")
format(lubridate::mdy(x), '%B')

#[1] "December"  "September" "September" "September" "September"
  • Related