R Extract day from date and time
Hi I only need to stay with the day of each date:
df<-data.frame(x=c("2014-07-24 00:00:00",
"2014-07-24 00:00:00", "2014-07-11", "2014-07-11" ,"2014-07-16" ,"2014-07-14"))
as.Date(df$x,format="%Y-%m-%d" )
I've tried this:
df$dia<-as.Date(df$x, format="%d")
But I am getting the full date and is different from the original.
I don't want to install another package for this. How can I solve this? Thanks to
+5
source to share
6 answers
If your dates are always in the same position and format, you have another option with substr()
. The call below starts from the 9th position - the beginning of the day - and ends on the 10th - the second day of the day.
substr(x = df$x, start = 9, stop = 10)
[1] "24" "24" "11" "11" "16" "14"
+3
source to share