Reputation: 349
How do I get the month from the below dates by just extracting all the characters left to the /
.
Some example data:
10/1/2015 10:30:00
10/15/2015 13:32:00
2/12/2012
Upvotes: 1
Views: 120
Reputation: 83215
You can use a combination of the functions as.Date
and months
for that. This will give you the names of the months:
months(as.Date(str1, format = "%m/%d/%Y"))
which gives:
[1] "October" "October" "February"
If you just want the number of the months, you can also use the month
function from the data.table
package:
library(data.table)
month(as.Date(str1, format = "%m/%d/%Y"))
which returns an integer vector:
[1] 10 10 2
Used data:
str1 <- c('10/1/2015 10:30:00', '10/15/2015 13:32:00', '2/12/2012')
Upvotes: 5