Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Extract day from datetime

Hi I need to remain only 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 tried this:

df$dia<-as.Date(df$x, format="%d")

But I get a full date and different from the orginal.

I don't want to install another package to do this. How can I solve it? Thanks

like image 663
GabyLP Avatar asked Sep 25 '14 16:09

GabyLP


People also ask

How do I extract the day from a date in R?

You can simply use weekdays(df$date) to extract the day of the week from the date. Only make sure the column is of type Date or convert using as. Date() .

How do I get the day of the month in R?

Finding Day and Month on a Specific Date in R Language – weekday() and month() function. weekdays() function in R Language is used to find out a day on a specific date and return as a string.


1 Answers

Are you looking for format?

format(as.Date(df$x,format="%Y-%m-%d"), format = "%d")
# [1] "24" "24" "11" "11" "16" "14"
like image 115
Sven Hohenstein Avatar answered Sep 19 '22 15:09

Sven Hohenstein