R. Convert TimeStamp column from DataFrame to Date Format column - r

I have got a question.
There are a column with timestamp records like '1643410273'(summary more than 1.2M records). How can I transform it into Date format?
I created this code (R Language):
mydata <- read.csv("summary_dataset.csv")
unique(mydata$Callsign)
flight <- mydata[mydata$Callsign == "AFR228",]
AltitudeValue <- flight$Altitude
UTC_Timestamp <- flight$Timestamp
Flight_Date <- vector()
for (i in 1:length(UTC_Timestamp)){
Flight_Date[i]=as.POSIXct(UTC_Timestamp[i], origin='1970-01-01', tz="UTC")
}
Flight_Date
But, in result, vector Flight_Date was filled Timestamp records. What's wrong?

Convert the Timestamp column first to numeric, change it to POSIXct format by passing origin and extract only the date from it.
flight$Flight_Date <- as.Date(as.POSIXct(as.numeric(flight$Timestamp),
origin='1970-01-01', tz="UTC"))
Example -
as.POSIXct(1643410273, origin='1970-01-01', tz="UTC")
#[1] "2022-01-28 22:51:13 UTC"
as.Date(as.POSIXct(1643410273, origin='1970-01-01', tz="UTC"))
#[1] "2022-01-28"

Related

Cannot Correctly Subset R DF

I have pulled some data via sql into a dataframe. I am now trying to subset such data and have had no luck.
I wish to loop through each row and identify the previous hour, after which I wish to select a subset of the DF where date == previous hour. (I understand there are other ways of doing this however i wish to understand why this isn't working). When I do this it returns an empty df. However If i directly paste the value of previous hour as a string I get the result I desire.
Both variables are POSIXCT and any attempt to convert to character fails. Can someone please tell me what on earth is going on? :S
My code:
for(row in 1:3){
PreviousHour <- as.POSIXct(Data$mydate[row] - hours(1), tz = "UTC")
Date <- Data$mydate[row]
print(c(Data$mydate[row],PreviousHour))
#"2019-11-20 23:00:00 GMT" "2019-11-20 22:00:00 GMT"
print(Data$mydate[row] == PreviousHour)
#FALSE
print(subset(Data,Data$mydate == PreviousHour))
# A tibble 0x5
print(subset(Data,Data$mydate == "2019-11-20 22:00:00 GMT"))
# A tibble 1x5
}
Code if I manually create the df (This works):
mydate <- c(as.POSIXct("2019-11-20 22:00:00", tz = "UTC"),as.POSIXct("2019-11-20 21:00:00", tz = "UTC"))
Data <- data.frame(mydate)
for(row in 1:1){
PreviousHour <- as.POSIXct(Data$mydate[row] - hours(1), tz = "UTC")
Date <- Data$mydate[row]
print(c(Data$mydate[row],PreviousHour))
#"2019-11-20 22:00:00 GMT" "2019-11-20 21:00:00 GMT"
print(Data$mydate[row] == PreviousHour)
#FALSE
print(subset(Data,Data$mydate == PreviousHour))
# A tibble 1x1
}

How to convert character and dates to dates?

My data comes from excel. The dates are in dd/mm/yyyy format:
certificado$fecha <- c("22/02/2019", "43679", "22/02/2019", "22/01/2019", "28/10/2019",
"18/09/2019")
However, R is reading some dates as mm/dd/yyyy. My code is supposed to convert all of them to an specific format.
certificados$Fecha <- as.Date(certificados$Fecha,format = "%d/%m/%Y")
But im getting NAs due to date format issues.
If you cannot fix this at the source, this code finds both formats:
vec <- c("22/02/2019", "43679", "22/02/2019", "22/01/2019", "28/10/2019", "18/09/2019")
out <- as.Date(vec, format = "%d/%m/%Y")
out
# [1] "2019-02-22" NA "2019-02-22" "2019-01-22" "2019-10-28" "2019-09-18"
isna <- is.na(out)
out[isna] <- as.Date(as.integer(vec[isna]), origin = "1900-01-01")
out
# [1] "2019-02-22" "2019-08-04" "2019-02-22" "2019-01-22" "2019-10-28" "2019-09-18"

Function on Date in R gives results as "NA"

My date value is in this format
02:27:16 05-Mar-2019, Tue stored in Assigned date column
Am converting
srdetails1$Assigned On GMT<-as.POSIXct(srdetails1$Assigned On GMT, tz="", format = "%H:%M:%S %m/%d/%Y")
srdetails$Assigned On GMT
the value get converted as
43497.067407407405
Instead of showing a date and any function i use on this column for
e.g :-
day(ymd_hms() etc gives me "NA"
How do i resolve this - Any help appreciated
When i trim the date with only m/d/y (without time) it works properly
Your format mask does not match the timestamp which you are trying to use with as.POSIXct. Consider the following version:
x <- "02:27:16 05-Mar-2019"
as.POSIXct(x, tz="", format = "%H:%M:%S %d-%b-%Y")
[1] "2019-03-05 02:27:16 CET"
We can use anytime
library(anytime)
addFormats("%H:%M:%S %d-%b-%Y")
anytime(x)
#[1] "2019-03-05 02:27:16 EST"
data
x <- "02:27:16 05-Mar-2019"

How can I convert timestamp strings in R to a number that I can do calculations with?

So I currently read in a .cvs file (as a dataframe ndata) that contains timestamp strings that are in a format like the following:
year-month-day hour:minute:second,millisecond
Example: "2016-08-26 19:06:11,380"
I contain the values of the timestamp in ndata$time.
I tried using
ndata$modifiedTime = as.numeric(ndata$time)
But this didn't work because R didn't acknowledge the timestamp as a timestamp but rather as any other string. Is there a way that I can alter/redefine the timestamp string so that I can store these values as a set amount of seconds after time 0 (0000-00-00 00:00:00,000) and actually use these dates in calculations?
Below we input a string s and then convert it to a POSIXct object p and then a number num representing the number of seconds since the Epoch (January 1, 1970).
We then reverse the process, creating a POSIXct object p2 and then formatting that back into a string s2.
We see that the original string s and the string obtained from num are identical.
No packages are used.
s <- "2016-08-26 19:06:11,380" # test input
# convert string to numeric
p <- as.POSIXct(chartr(",", ".", s))
num <- as.numeric(p)
# convert numeric to string
p2 <- as.POSIXct(num, origin = "1970-01-01")
s2 <- chartr(".", ",", format(p, "%Y-%m-%d %H:%M:%OS3"))
identical(s, s2)
## [1] TRUE
Convert to R date-time using as.POSIXct or other methods and then use as.numeric. For manipulating time you can use lubridate which is quite useful:
tstamp <- "2016-08-26 19:06:11,380"
z <- (as.POSIXct(tstamp, format = "%Y-%m-%d %H:%M:%S", tz = "UTC"))
> as.numeric(z)
[1] 1472238371

How to convert dates in a dataframe to a Date datatype?

I've imported one date value into R:
dtime <- read.csv("dtime.csv", header=TRUE)
It's output (7th Nov, 2013) is printed as:
> dtime
Date
1 07-11-2013 23:06
and also its class is 'factor'.
> class(dtime$Date)
[1] "factor"
Now, I want to extract the time details (hours, minutes, seconds) from the data. So, I was trying to convert the dataframe's date value to Date type. But none of the following commands worked:
dtime <- as.Date(as.character(dtime),format="%d%m%Y")
unclass(as.POSIXct(dtime))
as.POSIXct(dtime$Date, format = "%d-%m-%Y %H:%M:%S")
How do I achieve this in R???
Your attempts didn't work because the format specified was wrong.
With base R there are two possible ways of solving this, with as.POSIXlt
Res <- as.POSIXlt(dtime$Date, format = "%d-%m-%Y %H:%M")
Res$hour
Res$min
Also, for more options, see
attr(Res, "names")
## [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" "zone" "gmtoff"
Or a bit less conveniently with as.POSIXct
Res2 <- as.POSIXct(dtime$Date, format = "%d-%m-%Y %H:%M")
format(Res2, "%H") # returns a character vector
format(Res2, "%M") # returns a character vector
I would like to contribute solution utilising lubridate :
dates <- c("07-11-2013 23:06", "08-10-2012 11:11")
dta <- data.frame(dates)
require(lubridate)
dta$properDate <- dmy_hm(dta$dates)
If needed, lubridate will enable you to conveniently specify time zones or extract additional information.

Resources