Convert Factor to Date/Time in R

Convert Factor to Date/Time in R

You need to insert an as.character() before parsing as a Datetime or Date.

A factor will always come back first as a number corresponding to its level.

You can save the conversion from factor to character by telling read.csv() etc to no store as a factor: stringsAsFactors=FALSE. You can also set that as a global option.

Once you have it as character, make sure you match the format string to your data:

R> as.POSIXct("2013-06-01 08:07:00", format="%Y-%m-%d %H:%M:%S")
[1] "2013-06-01 08:07:00 CDT"
R>

Note the %Y-%m-%d I used, as opposed to your %m/%d/%y.

Edit on 3 Jan 2016: This is now much easier thanks to the anytime package which automagically converts from many types, including factor, and does so without requiring a format string.

R> as.factor("2013-06-01 08:07:00")
[1] 2013-06-01 08:07:00
Levels: 2013-06-01 08:07:00
R>
R> library(anytime)
R> anytime(as.factor("2013-06-01 08:07:00"))
[1] "2013-06-01 08:07:00 CDT"
R>
R> class(anytime(as.factor("2013-06-01 08:07:00")))
[1] "POSIXct" "POSIXt"
R>

As you can see we just feed the factor variable into anytime() and out comes the desired POSIXct type.

how to convert factor into DateTime in R?

Try:

dataframe1$datetime <- strptime(x = as.character(dataframe1$datetime),
format = "%d/%m/%Y %H:%M")

Convert factor to datetime in R

How about this with lubridate ?

x <- as.factor("2022-05-16T20:38:19")

library(lubridate)

y <- ymd_hms(x)

str(y)

POSIXct[1:1], format: "2022-05-16 20:38:19"

convert factor to time format in r

In base R, you can use as.POSIXct or strptime along with format to convert the factor variable to datetime class (POSIXlt/POSIXct).

transform(df, session_start_text = as.POSIXct(session_start_text, format = "%T"), 
session_end_text = as.POSIXct(session_end_text, format = "%T"))

In lubridate, we can use hms to convert the columns to "Period" class

library(dplyr)
library(lubridate)
df %>%
mutate(session_start_text = hms(session_start_text),
session_end_text = hms(session_end_text))

Moreover, there is also anytime::anytime() function which helps in converting to date time values.

Using R to convert factor to date

I think you need to supply a day value for the conversion to work:

cd$dates <- as.Date(
paste0(as.character(cd$StartDate), "-01"),
format = "%b-%y-%d")
##
R> str(cd)
#'data.frame': 3 obs. of 4 variables:
#$ StartDate: Factor w/ 3 levels "Dec-89","Jul-89",..: 1 2 3
#$ Phase : chr "Phase_2" "Phase_2" "Phase_1"
#$ Cancer : chr "breast" "breast" "breast"
#$ dates : Date, format: "1989-12-01" "1989-07-01" "1992-09-01"

Data:

cd <- read.table(
text = " StartDate Phase Cancer
1 Dec-89 Phase_2 breast
2 Jul-89 Phase_2 breast
3 Sep-92 Phase_1 breast",
header = TRUE,
stringsAsFactors = FALSE)
##
cd$StartDate <- as.factor(cd$StartDate)

How do I convert a factor into date format?

You were close. format= needs to be added to the as.Date call:

mydate <- factor("1/15/2006 0:00:00")
as.Date(mydate, format = "%m/%d/%Y")
## [1] "2006-01-15"

Quarterly Data - Convert Factor to Date

Apply as.Date to the Date column, not the entire data.frame. No packages are used.

transform(df, Date = as.Date(Date))

giving:

        Date
1 2008-07-31
2 2008-07-31
3 2008-07-31
4 2008-07-31
5 2008-07-31
6 2008-07-31


Related Topics



Leave a reply



Submit