Converting factor date time columns into POSIXct in R - r

I have another problem. I have a date time column in a data frame, which when i upload it comes as factor and I want it to be POSIXct
str(ida$DATA_TRAMA)
Factor w/ 1122932 levels "1-1-2010 00:00:51",..: 629101 629120 629128 629132 629139 629149
And i want it to be POSIXct (%Y-%m-%d %H:%M:%S) format. I already tried all of these methods but none of them seem to work. Whichever i apply it gets NA values.
ida$DATA_TRAMA<- as.POSIXct(ida$DATA_TRAMA,format='%d/%m/%Y %H:%M:%S')
ida$DATA_TRAMA<- as.POSIXct(as.character(ida$DATA_TRAMA), format = "%d/%m/%Y %H:%M")
ida$DATA_TRAMA <-format(ida$DATA_TRAMA, "%Y-%m-%d")
ida$DATA_TRAMA <- as.POSIXct(ida$DATA_TRAMA, format = '%Y-%m-%d:%H:%M:%S')
ida$DATA_TRAMA <- as.POSIXlt(as.character(ida$DATA_TRAMA), format="%m/%d/%Y %H:%M:%S")
ida$DATA_TRAMA <- strptime(ida$DATA_TRAMA,"%Y-%m-%d %H:%M:%S")
Do you know how to do it?

With a "factor" argument as.POSIXct will invoke as.POSIXct.default and that uses as.POSIXlt which has a "factor" method so just do:
DF <- data.frame(d = "1-1-2010 00:00:51") # test data. d has factor class.
transform(DF, d = as.POSIXct(d, format = "%m-%d-%Y %T"))
giving:
d
1 2010-01-01 00:00:51

Related

Formatting Time without decimals

I currently have a large data set with the variable time formatted as such: "15:36:44.874541+0000", for one example.
I want to remove the decimals and just have it as "15:36:44" for every variable in the set.
You can convert it into POSIXct type and then extract only time with format.
x <- '15:36:44.874541+0000'
format(as.POSIXct(x, format = "%T"), "%T")
#[1] "15:36:44"
I don't think it's the perfect and most elegant way of doing it, but you can split by "." and keep the first item.
You can write:
c <- "15:36:44.874541+0000"
df <- data.frame(Time = rep(c,5))
library(rebus)
library(dplyr)
df %>% mutate(Time2 = unlist(strsplit(as.character(Time),"\\."))[1])
Time Time2
1 15:36:44.874541+0000 15:36:44
2 15:36:44.874541+0000 15:36:44
3 15:36:44.874541+0000 15:36:44
4 15:36:44.874541+0000 15:36:44
5 15:36:44.874541+0000 15:36:44
You just need to strip that time and format it.
current_time <- "15:36:44.874541+0000"
formated_time <- format(strptime(time, format = "%H:%M:%S"), "%H:%M:%S")
formated_time
[1] "15:36:44"
We can use strptime with strftime
strftime(strptime(x, "%T"), "%T")
#[1] "15:36:44"
data
x <- '15:36:44.874541+0000'

Convert different date-time-formats at once (strptime)

Hi :) I have a column of my data.frame which contains dates in two formats. Here is an short minimal example:
D = data.frame(dates = c("3/31/2016", "01.12.2015"))
dates
1 3/31/2016
2 01.12.2015
With the nice function strptime I can easily get date-times for each format:
D$date1 <- strptime(D$dates, format = "%m/%d/%Y")
D$date2 <- strptime(D$dates, format = "%d.%m.%Y")
I already managed a workaround with:
D$date12 <- do.call(pmin, c(D[c("date1","date2")], na.rm=TRUE) )
To achieve this:
dates date1 date2 date12
1 3/31/2016 2016-03-31 <NA> 2016-03-31
2 01.12.2015 <NA> 2015-12-01 2015-12-01
Is there are more sophisticated way to do this transformation (from dates to date12) at once?
Regards
You can use the anytime package.
library(anytime)
anytime::addFormats("%d.%m.%Y")
anydate(D$dates)
Note that the argument in anydate has to be a vector, so just select the coloumn dates.
Or use lubridate
parse_date_time(D$dates, c("mdy", "dmy"))

Converting date column to day of month and month in a new column

I have this kind of data frame
dat <- read.table(text = " count date
0 10/05/2012
1 10/05/2013
1 10/05/2014
",header = TRUE)
I would like to have a new variable that will contain the following format:
> dat
count date DayMonth
1 0 10/05/2012 10-05
2 1 10/05/2013 10-05
3 1 10/05/2014 10-05
I tried some versions of strptime function like dat$DayMonth<-strptime(dat$date, "%d/%m") but got strange resluts.
How can I get the desired result
Same solution using the packages lubridate or anydate.
library(lubridate)
dat$DayMonth <- format(dmy(dat$date), "%d-%m")
# dmy stands for day,month,year, can you use ymd etc.
library(anytime)
dat$DayMonth <- format(anydate(dat$date), "%d-%m")
We can do this with as.Date and format
dat$DayMonth <- format(as.Date(dat$date, "%d/%m/%Y"), "%d-%m")
dat$DayMonth
#[1] "10-05" "10-05" "10-05"
Using strptime converts to POSIXlt/POSIXct class, from which we can change to the format using format
NOTE: No external packages used

chr to date class in R

I have a data frame containing dates as characters,dd.mm.yyyy format. want to convert those in date class, format yyyy-m-d. as.date() is not working returning error, do not know how to convert 'dates' to class “Date”
dates <- data.frame(cbind(c("5.1.2015", "6.1.2014", "17.2.2014", "28.10.2014")))
colnames(dates) <- c("dates")
as.Date(dates, format = "%Y-%m-%d")
new_format_dates <- cbind(gsub("[[:punct:]]", "", dates[1:nrow(dates),1]))
as.Date(new_format_dates, format = "%Y-%m-%d")
So I tried to replace the . and reformat those dates under new_format_dates, returning result like [1] NA NA NA NA
Firstly, make your data.frames properly; don't use cbind in data.frame. Next, set the format argument of as.Date to the format you've got, including separators. If you don't know the symbol you need, check out ?strptime.
dates <- data.frame(dates = c("5.1.2015", "6.1.2014", "17.2.2014", "28.10.2014"))
dates$dates_new <- as.Date(dates$dates, format = "%d.%m.%Y")
dates
# dates dates_new
# 1 5.1.2015 2015-01-05
# 2 6.1.2014 2014-01-06
# 3 17.2.2014 2014-02-17
# 4 28.10.2014 2014-10-28
dates <- data.frame(cbind(c("5.1.2015", "6.1.2014", "17.2.2014", "28.10.2014")))
colnames(dates) <- c("dates")
dates$new_Dates <- gsub("[.]","-",dates$dates)
dates$dates <- NULL
dates_new <- as.Date(dates$new_Dates, format = "%d-%m-%Y")
dates_new <- data.frame(dates_new)
print(dates_new)

Converting date in seconds since origin in R

I have a series of dates that appear to be defined in seoncds since Jan 1, 1960.
'data.frame': 5 obs. of 1 variable:
$ original: int 1624086000 1624086000 1508137200 1508137200 1508137200
(for reproduction:)
data <- as.data.frame(c(1624086000,1624086000,1508137200,1508137200,1508137200))
setnames(data, c("original"))
I would like to convert these to dates in the format %Y-%m-%d.
I wrote the following code for this:
uniqueDates <- as.data.frame(unique(data))
uniqueDates$converted <- sapply(uniqueDates$original, function(x) as.Date(as.POSIXct(x, origin="1960-01-01", tz = "GMT"), "GMT", "%Y-%m-%d"))
The result are dates in a five-digit numeric format:
> str(uniqueDates$converted)
num [1:2] 15144 13802
If I just run
as.Date(as.POSIXct(1624086000, origin="1960-01-01", tz = "GMT"), "GMT", "%Y-%m-%d")
I get the desired result:
[1] "2011-06-19"
What am I doing wrong that results in the five-digits numeric type values instead of the date objects?
as.Date(as.POSIXct(data[,1], origin="1960-01-01", tz = "GMT"), "GMT", "%Y-%m-%d")
[1] "2011-06-19" "2011-06-19" "2007-10-16" "2007-10-16" "2007-10-16"
The function is already vectorized. There is no need for the lapply function. Use the apply family if you have multiple columns of dates. If you want to avoid the long anonymous function, you can create the function first and use it in the way that works for your cases:
as.ymd <- function(x) {
as.Date(as.POSIXct(x, origin="1960-01-01", tz = "GMT"), "GMT", "%Y-%m-%d")
}
So now with either a single vector or array with multiple dimensions, you can convert the dates for those cases:
data2 <- data.frame(c(1624086000,1624086000,1508137200,1508137200,1508137200), c(1624086000,1624086000,1508137200,1508137200,1508137200))
setnames(data2, c("original", "second"))
as.ymd(data2[,1])
[1] "2011-06-19" "2011-06-19" "2007-10-16" "2007-10-16" "2007-10-16"
data2[] <- lapply(data2, as.ymd)
data2
original second
1 2011-06-19 2011-06-19
2 2011-06-19 2011-06-19
3 2007-10-16 2007-10-16
4 2007-10-16 2007-10-16
5 2007-10-16 2007-10-16
The five-digit numeric output from sapply is due to its simplification process. The dates are being converted to class numeric. Try adding the argument simplify=FALSE to the first function that you tried for comparison.
You can work around it with strftime since it outputs vectors with the class character. With sapply there will not be any problem simplifying it, but then you're left with character strings instead of the chosen date classes (POSIXct, POSIXlt, Date, zoo, xts, ...).

Resources