POSIxt and POSIXct Date Comaprison Count in R - r

I created a data table called ltfs from a table I loaded in through a ODBC connection that was named ltfs
ltfs<-data.table(ltfs)
In this table there is a column with a date field in it. When I check the class of that column it returns "POSIXct" "POSIXt". When I check what the dates look like they are in the form of ex. 2014-01-01. I am trying to count the number of entries with a date in this field less than a certain date. I tried using the .N feature but am getting an error that has to do with the way the date formats are. My code is below any help would be much appreciated.
ltfs<-data.table(ltfs)
datecount<-ltfs[Eligibility_Date<=as.Date("2011-01-01"),.N]
I know it has to do with the date format (either the criteria or the format of the dates in the table) just not sure how to handle them.

Related

How can I convert a column of date/time data from numeric to character in R?

I have a column of data compiled from Excel files. Some of the values in the date column have changed upon binding and are now numeric date format (despite their starting out character) whilst others remain as they were (yyyy-mm-dd hh:mm). How can I change the entire column to the same date format (yyyy-mm-dd hh:mm)?
Thanks in advance.
Try strptime:
df$column <- strptime(df$column, format='%Y-%m-%d %H:%M')
OK so I finally cracked it. This is probably very obvious to everyone but just in case a newb like myself has this same issue this is what solved it for me.
I had two sets of data that I'd bound into the same table. One set of data came from XLSX files and the other from CSV files. They both presented fine in R but when combined the CSV-derived lost formatting and reverted to numerical dates. I discovered that the 'date' columns in the xlsx-derived tables were 'character' whilst the 'date' columns in the csv-derived tables were 'factor with 1 level'. When combined, the character data preserved format (i.e. looked like a date - yyyy-mm-dd hh:mm) and the factor data turned into numeric dates
So to rectify I used the following on the .csv (factor) tables before binding:
myfile$Date <- as.character(myfile$Date)
This changed the columns to character to match the others and the bind was successful and all date formatting was preserved. Thank you for your help!

Mixed Timed Data

I have a vector that contains time data, but there's a problem: some of the entries are listed as dates (e.g., 10/11/2017), while other entries are listed as dates with time (e.g., 12/15/2016 09:07:17). This is problematic for myself, since as.Date() can't recognize the time portion and enters dates in an odd format (0012-01-20), while seemingly adding dates with time entries as NA's. Furthermore, using as.POSIXct() doesn't work, since not all entries are a combination of date with time.
I suspect that, since these entries are entered in a consistent format, I could hypothetically use an if function to change the entries in the vector to a consistent format, such as using an if statement to remove time entirely, but I don't know enough about it to get it to work.
use
library(lubridate)
Name of the data frame or table-> x
the column that has date->Date
use the ymd function
x$newdate<-ydm(x$Date)

Sqlite format date from hour to hour

I want store in my sqlite database date in format DD/MM/YYYY HH:MM-HH:MM there is any solution of this ? I found only that YYYY-MM-DDTHH:MM answer.
SQLite has no specal data type for dates.
You can store dates in any format you want, but if you want to use any of the built-in date functions, you have to use one of the supported date formats.
What you want to store is not a date (a point in time), but a time interval.
There is no built-in support for intervals; the best you can do is to store the start and the end of the interval in two separate columns.
Please note that storing a date and displaying a date are two different things.
It would be easier to compute the length of an interval when the two date values are stored in one of the numeric date formats.

R date conversion to POSIX format in Tableau

I am trying to connect R-Impala-tableau.
I have an R code scripted in tableau calculated field which runs on a date field.
R code is:
script_str('date<- as.POSIXct(strptime(.arg1,"%m%d%y"))',attr([date_val]))
I want this code to run on the date values extracted from impala.
The date_val here is extracted from impala and its converted to date type in tableau.
However, the above code returns NULL. I also tried keeping the date_val field to string, date_time, numeric data types.
I am doing this to perform a cut operation on the dates obtained(eg:intervals<-cut(date,"1 day")). To do this, I need the field date in numeric format.
I even tried script_str('date<- as.POSIXct(strptime(as.character(.arg1),"%m%d%y"))',attr([date_val]))
Has anybody faced similar issue?
Any help in this regard will be appreciated.

Parsing out time-of-day from a date to a separate column

I have, per cell, a date value in the format 2013-01-05 11:21.
Is there a way to separate the time of day (ie 11:21) and put it in a new column, without having to manually cut and paste?
I have a lot of date values in one column, and I want to separate the time-of-day portion of these dates into a new adjacent column.
Yes - the TIMEVALUE function should do this. You may need to format the result cells (in my examle: B1:B8) as time values. Using cell formatting, you can set the output to a hh:mm syntax, too.

Resources