Change format of column in R - r

In my table I have a timestamp column in which i want to change the format. I already tried multiple things but it doesnt work. I also can not find examples to change the format in the whole column. First i converted the column to POSIXct then Ive tried to adjsut the format:
#timestamp as POSIXct and lane as numeric
Flows_ALM_2019 %>%
mutate(timestamp = as.POSIXct(timestamp),lane = as.numeric(lane)) -> Flows_ALM_2019
#remove seconds from time in timestamp column
df <- strftime(timestamp, format= "%Y-%m-%d %H:%m")
after trying:
df_2019_tsb$timestamp <- format(df_2019_tsb$timestamp,format= "%Y-%m-%d %H:%m")
This is what I had:
and now it changed to:
What am i doing wrong?

You can use format :
Flows_ALM_2019$timestamp <- format(Flows_ALM_2019$timestamp, "%Y-%m-%d %H:%m")
Or with strftime.
Flows_ALM_2019$timestamp <- strftime(Flows_ALM_2019$timestamp, "%Y-%m-%d %H:%m")
This is assuming that timestamp column is of class POSIXct in your data.

Related

How to reformat dates without NA's

I have a column of dates I need to reformat, they are in the format m/d/yyyy, but as the dates grow the format becomes mm/dd/yyyy. When I try to run
as.Date(x, format = '%m/%d/%y')
I get NA's
How can I reformat the dates to mm/dd/yyyy?
Here is an example:
# convert date info in format 'mm/dd/yyyy'
strDates <- c("01/05/1965", "08/16/1975")
strDates
Output:
[1] "01/05/1965" "08/16/1975"
Convert the dates using this code:
dates <- as.Date(strDates, "%m/%d/%Y")
Output:
[1] "1965-01-05" "1975-08-16"

How to change date format from YYYY/MM/DD to DD/MM/YYYY

I have a column of dates which were read in as character values (yes, they are supposed to be the same):
str(df$date)
$ date : chr "30/08/2017" "30/08/2017" "30/08/2017" "30/08/2017"
I then convert the values to Date format:
str(df$date)
$ date : Date, format: "2017-08-30" "2017-08-30" "2017-08-30"
The problem is that no matter which method I choose to use, the resulting dates are always in YYYY/MM/DD format, which is not what I want; they should be in DD/MM/YYYY format.
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
I have read numerous similar Stack Overflow posts as well as some guides and have tried things like getting and setting my system locale (United Kingdom) and all is correct in that respect.
Where am I going wrong?
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
R has two very similarly named functions: strptime, which converts from character strings to Date data, and strftime, which converts Dates to formatted strings. To make matters worse, the documentation for these two functions is combined, so it can be very hard to keep their uses straight. You want strftime, in this case.
You can also use format:
date = c("30/08/2017", "30/08/2017", "30/08/2017", "30/08/2017")
date <- as.Date(date, format = "%d/%m/%Y")
# > date
# [1] "2017-08-30" "2017-08-30" "2017-08-30" "2017-08-30"
date = format(date, "%m/%d/%Y")
# > date
# [1] "08/30/2017" "08/30/2017" "08/30/2017" "08/30/2017"
Turns into character class:
# > class(date)
# [1] "character"
This will help you:
$variable = '2018/09/18';
$date = str_replace('/', '/', $variable);
echo date('d/m/Y', strtotime($date));
Please check and let me know if you need any more help.

Remove timezone during POSIXlt Conversion in R

I have a column in my dataframe as datetime (factor) with the values as "15-10-2017 16:41:00".
I wanted this data to be converted as "2017-10-15 16:41:00".
When i try to convert this, I'm getting the timezone also as output.
I tried using tz="", usetz=F but no use.
Any suggestions ?
Code:
as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
[1] "2017-10-15 16:41:00 IST"
From the help page of as.POSIXlt:
"" is the current time zone
which is the default.
That's why it does not work. You could remove the timezone information this way, and it will not show while printing:
my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
my_datetime$zone <- NULL
my_datetime
but I don't understand why you would want to do that. You should convert to GMT if you don't want to worry about the timezone. Also lubridate package has a nice force_tz function if you have to force some specific timezones.
If you are ok storing the datetime as a character instead of as a POSIXlt, then you can use strftime():
my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strftime(my_datetime)
I do it like this:
strip.tz <- function(dt) {
fmt <- "%Y-%m-%d %H:%M:%S"
strptime(strftime(dt, format = fmt, tz=""), format = fmt, tz="UTC")
}
and you would use it like this:
my_datetime <- as.POSIXct("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strip.tz(my_datetime)

Converting date format from d/m/y time to d/m/y

I have data input coming in the form of d/m/y time eg: "3/27/2017 1:20:15 AM" and I want to just have d/m/y to display 3/27/2017.
I have seen different techniques on how to achieve this and have tried 3 different types however I recieve NA as the new output and for the later I get the "Error in charToDate(x) :
character string is not in a standard unambiguous format"
error.
Here is an my example code:
df <- data.frame(Date = "3/27/2017 1:20:15 AM")
df$Date <- as.POSIXct(as.character(df$Date), format = "%d/%m/%Y")
df$Date <- as.Date(df$Date, "%d/%m/%Y")
df$Date <- format(as.Date(df$Date), "%d/%m/%Y")
Looks like you want format = %m/%d/%Y instead of %d/%m/%Y.
If specialized date formats are still giving you problems with the time after that, you can first use strsplit() to break up the character string on spaces, then grab the first item to get just the date.
date <- "3/27/2017 1:20:15 AM"
date <- strsplit(date," ")
date <- unlist(date)[1]
date <- as.POSIXct(date,format="%m/%d/%Y")
You need to provide complete format string in order to identify it as a date-time.
In your case it would be something like this:
as.Date("3/27/2017 1:20:15 AM", format = "%m/%d/%Y %I:%M:%S %p")

how to convert factor into DateTime in R?

This question might be simple for some of you but bear with me since I'm a beginner in R.
I have a dataframe that has a factor column (called time) containing DateTime data as the following:
time
01/01/2011 00:10
02/01/2011 03:00
03/01/2011 05:00
04/01/2011 10:03
I want to convert this column into DateTime column in R. I searched and tried some functions but it gives me 'NA' results. The following functions are those I tried:
> dataframe1$datetime <- as.POSIXlt(as.character(dataframe1$time), format="%d/%m/%Y %H:%M")
> dataframe1$datetime <- as.POSIXlt(strptime(dataframe1$time), format="%d/%m/%Y %H:%M")
> dataframe1$datetime <- as.POSIXlt(dataframe1$time, format="%d/%m/%Y %H:%M")
> dataframe1$datetime <- as.chron(dataframe1$time, "%d/%m/%Y %H:%M")
I don't know what else to try. I want ideally to add three columns namely datetime, date, and time.
Try:
dataframe1$datetime <- strptime(x = as.character(dataframe1$datetime),
format = "%d/%m/%Y %H:%M")
Probably the easiest thing to do is use the lubridate packages which has a large number of functions for date manipulation. The following will convert your time into a POSIXct object:
library(lubridate)
mdy_hm( as.character(dataframe1$time) )
See ?mdy to see the variety of date parsing functions.
For a slightly more verbose version that does not rely on lubridate
strptime(x = as.character( dataframe1$datetime ), format = "%d/%m/%Y %H:%M")

Resources