In R:
How can I change the column of a data frame from yyyymmddHHMMSS to yyyy-mm-ss HH:MM:SS?
I tried
for(i in 1:nrow(tabla_eaq)){
tabla_eaq[i,'datetime'] = ymd_hms(tabla_eaq[i,'datetime'])
}
But it shows up as for example 1606514222 for input 20201127215702.
We don't need a for loop as the ymd_hms is vectorized
library(lubridate)
tabla_eaq$datetime <- ymd_hms(tabla_eaq$datetime)
data
tabla_eaq <- data.frame(datetime = c(20201127215702, 20201127215702, 20201127215702))
Related
I am trying to convert date objects into a date class using lubridate. These are the following dates in the "wrong" format"
wrong_format_date1 <- "01-25-1999"
wrong_format_date2 <- 25012005
wrong_format_date3 <- "2005-05-31"
But I would like them in this format:
"1999-01-25"
"2005-01-25"
"2005-05-31"
Can someone please assist me with this?
Try this. Use parse_date_time() from lubridate where you can define a vector with possible formats so that you get your strings parsed as dates. Here the code:
library(lubridate)
#Data
wrong_format_date1 <- "01-25-1999"
wrong_format_date2 <- 25012005
wrong_format_date3 <- "2005-05-31"
#Dataframe
df <- data.frame(v1=c(wrong_format_date1,wrong_format_date2,wrong_format_date3),stringsAsFactors = F)
#Code
df$Date <- as.Date(parse_date_time(df$v1, c("mdY", "dmY","Ymd")))
Output:
v1 Date
1 01-25-1999 1999-01-25
2 25012005 2005-01-25
3 2005-05-31 2005-05-31
I am trying to format a string column to a date-time serie.
The row in the column are like this example: "2019-02-27T19:08:29+000"
(dateTime is the column, the variable)
mutate(df,dateTime=as.Date(dateTime, format = "%Y-%m-%dT%H:%M:%S+0000"))
But the results is:
2019-02-27
What about the hours, minutes and seconds ?
I need it to apply a filter by date-time
Your code is almost correct. Just the extra 0 and the as.Date command were wrong:
library("dplyr")
df <- data.frame(dateTime = "2019-02-27T19:08:29+000",
stringsAsFactors = FALSE)
mutate(df, dateTime = as.POSIXct(dateTime, format = "%Y-%m-%dT%H:%M:%S+000"))
Strptime seems to be missing something in this scenario:
aDateInPOSIXct <- strptime("2018-12-31", format = "%Y-%m-%d")
someText <- "asdf"
df <- data.frame(aDateInPOSIXct, someText, stringsAsFactors = FALSE)
bDateInPOSIXct <- strptime("2019-01-01", format = "%Y-%m-%d")
df[1,1] <- bDateInPOSIXct
Assignment of bDate to the dataframe fails with:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
And a warning:
provided 11 variables to replace 1 variables
I want to use both POSIXct dates and POSIXct date-times to compare this and that. It's way less work than manipulating character strings -- and POSIX takes care of the time zone issues. Unfortunately, I'm missing something.
You only need to cast your calls to strptime to POSIXct explicitly:
aDateInPOSIXct <- as.POSIXct(strptime("2018-12-31", format = "%Y-%m-%d"))
someText <- "asdf"
df <- data.frame(aDateInPOSIXct, someText, stringsAsFactors = FALSE)
bDateInPOSIXct <- as.POSIXct(strptime("2019-01-01", format = "%Y-%m-%d"))
df[1,1] <- bDateInPOSIXct
Check the R documentation which says:
Character input is first converted to class "POSIXlt" by strptime: numeric input is first converted to "POSIXct".
My data has a start and end time stamp such as this:
200401010000 200401010030
200401010030 200401010100
200401010100 200401010130 and so on...
I'm trying to convert these fields into %YYYY%MM%DD%HH%MM format using lubridate and as.POSIXct but it I get only NAs. Any help will be appreciated.
My goal is to aggregate the data for each month.
The code I've used so far is as follows:
start_time = as.POSIXct(dat$TIMESTAMP_START, format = "%YYYY%MM%DD %HH%MM",origin = "2004-01-01 00:00", tz="EDT")
stop_time = as.POSIXct(dat$TIMESTAMP_END, format = "%YYYY%MM%DD%HH%MM",origin = "2004-01-01 00:30", tz="EDT")
dat$interval <- interval(start_time, stop_time)
Two problems I can see:
If you're using lubridate already, you should probably use the function ymd_hm(), which is just cleaner IMO.
You can't apply that function to a vector (which I presume dat$TIMESTAMP_START and dat$TIMESTAMP_END are); to do this, you can use:
start_time <- sapply(dat$TIMESTAMP_START, ymd_hm())
end_time <- sapply(dat$TIMESTAMP_END, ymd_hm())
That will apply the function to each item in your vector.
Please help as I have a csv file of large database with date column having various format of dates like 20080408 or 2008/04/08 or 08/04/2008.How do i change these format to one format of dd/mm/yyyy.In R Programing
You can do it with failure tests via lubridate dmy and mdy conversions as well (hence the suppressWarnings() calls. I don't think you're going to be able to ensure proper handling of things like "08/04/2008" if 08 is supposed to be the "day" component, tho, given that the functions can't read minds.
library(lubridate)
dat <- c("20080408", "2008/04/08", "08/04/2008")
dat.1 <- unlist(lapply(dat, function(x) {
suppressWarnings(res <- mdy(x))
if (is.na(res)) { suppressWarnings(res <- ymd(x)) }
return(as.character(res))
}))
dat.1
## [1] "2008-04-08" "2008-04-08" "2008-08-04"
The following should work for your data.frame. You may need to convert your date column to the class as.character in order that the string split function strsplit works correctly. After tha, the loop simply evaluates how many characters are in the string before the first "/" character, and adjusts the formatting accordingly.
Example:
df <- data.frame(DATE=as.character(c("20080408", "2008/04/08", "08/04/2008")), DATE2=as.Date(NA))
df$DATE=as.character(df$DATE)
for(i in seq(df$DATE)){
sp <- unlist(strsplit(df$DATE[i], "/"))
if(nchar(sp[1]) == 8){
df$DATE2[i] <- as.Date(df$DATE[i], format="%Y%m%d")
}
if(nchar(sp[1]) == 4){
df$DATE2[i] <- as.Date(df$DATE[i], format="%Y/%m/%d")
}
if(nchar(sp[1]) == 2){
df$DATE2[i] <- as.Date(df$DATE[i], format="%d/%m/%Y")
}
}
Result:
df
# DATE DATE2
#1 20080408 2008-04-08
#2 2008/04/08 2008-04-08
#3 08/04/2008 2008-04-08
You can read them as character values and convert them using as.Date.
x1 <- '20080408' ## class character (string)
x2 <- '2008/04/08'
x1.dt <- as.Date(x1, format='%Y%m%d')
x2.dt <- as.Date(x2, format='%Y/%m/%d') ## different format
print(c(x1, x2), format='%d/%m/%Y') ## you can return Date objects in any format you want
Check out ?strftime for all the formatting options.