I don't see the date column in the output from str(), so I can't give you detailed advice. In the as.Date function, set the format argument to show how the character string represents the date, not to the format that you want for the resulting numeric date.
as.Date("3/21/23", format = "%m/%d/%y")
[1] "2023-03-21"
A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```
Your date column consists of character representations of integers. I'm guessing those are dates from a spreadsheet. You can convert them with as.Date using the origin argument.
POSIXct date-times are the number of seconds since 1970-01-01 00:00:00. It has been about 1.7 billion seconds since 1970-01-01 00:00:00, which is about 54 years
TODAY_Midnight <- as.POSIXct("2024-01-16 00:00:00")
> as.numeric(TODAY_Midnight)
[1] 1705388400
> 1705388400/60/60/24/365.25 #convert to years
[1] 54.0405
You can convert numeric values to dates with either as.Date or as.POSIXct
Spreadsheet dates or date-times are the number of days since 1899-12-30 (ignoring a problem with dates before 1900-03-01). That is why I used
as.Date(as.numeric(DATE), origin = "1899-12-30")
in my previous post. I think those integers are the underlying values of spreadsheet dates.
Your schedtime column has values like 1455 and 1640. If you pass that to as.POSIXct(), it translates 1455 to the date-time that is 1455 seconds after 1970-01-01 00:00:00 UTC and then adjusts for your time zone. That gives you a date-time of 1969-12-31 19:24:15.
I think it is more likely that 1455 represents the time of day 14:55 and the date information is stored separately in the date column as 37987, which is 2004-01-01 according to my previous calculation. Does a date-time of 2004-01-01 14:55 make sense for the first row of your data?
Thank you so much for helping me to understand it. I have a few more doubts but I can't take advantage of your goodness. Where can I refer to know these all?
I was going through CRAN to understand it. I checked some books to figure it out but I wasn't able to understand it. I appreciate it.
Hi there...
To obtain a strong foundation in the R language
what should I do? Which books do I need to read? I am interested in pursuing data analytics. I am a cook to date. I am new to this field. Trying to learn R from various sources like books, especially and few websites.
I am not getting the date format (%Y%m%d) after using the strptime (). It's the same dataset.
$ date : chr [1:2201] "37987" "37987" "37987" "37987" ...
release_date <- strptime("flightdata$date", format = %Y%m%d)
Error: unexpected SPECIAL in "release_date <- strptime("flightdata$date", format = %Y%"
release_date <- strptime("flightdata$date", format = "%Y%m%d")
print(release_date)
[1] NA --------------------- Why am I getting NA.
class(release_date)
[1] "POSIXlt" "POSIXt"
release_date <- strptime(flightdata$date,format = "%Y%m%d")
class(release_date)
[1] "POSIXlt" "POSIXt"
View(release_date)..............output is NA
What is wrong with me? Please help me.
To convert the class character date column to the class date column I am using the strptime().
strptime() takes in a character and outputs a date-time but the format of the input character has to be provided.
strptime("21/3/23", format = "%d/%m/%y")
[1] "2023-03-21 MDT"
"37987" is not a representation of a date-time using that uses day, month, and year or any of the other formatting codes of strptime() (see the Details section of the help file for strptime()). "37987" is a character version of the number of days since 1899-12-30. strptime() does not have a method to deal with that.
Why are you using strptime() instead of as.Date()?
flightdata$date <- as.Date(as.numeric(flightdata$date,origin = "1970-01-01")) ........How did I get this "2074-01-02"? I also get NA values in the last few columns.
Can I use any arithmetic operations-oriented functions to get 2004-01-01? or am I asking an invalid question?
Now, I want to convert schedtime column which is in the class numeric to class time. I have used as.POSIXct () and the output is as follows...
flightdata$schedtime <- as.POSIXct(flightdata$schedtime)
output:- "1969-12-31 19:24:15" "1969-12-31 19:27:20"................I have a feeling it is wrong because of the year 1969 and the timing too.
I have used as. data.table package to remove the year..... I don't know whether it is a right or wrong way to do it.
flightdata$schedtime <- as.ITime(flightdata$schedtime)
I'm not aware of a class time in base R. The hms package allows converting numbers or characters to a class "hms", which is basically stored as seconds but displayed as a time. If your values in schedtime are really times stored as integers, that is, 1454 represents 14:54, you can write a simple function to convert those to hms values.