Any R function to solve this dates problem? - r

I have this column is date of birth. Is there any R function or any method to solve this date problem, the dates in Excel are ok. But when I read the file in R I am getting wrong dates as below:
2047-11-03
2064-06-08
1098639-10-01
377383-01-18
1979098-01-27
-177103-09-27
-186092-08-24
1612569600
-236822400
the original dates in excel as below
01/11/1977
07/06/1994
11/09/1982
07/05/1974
19/11/1992
06/12/1967
29/10/1967
06/02/2021
01/07/1962
I used the following to imports the data
df<- import_list("df.xlsx")

Related

Converting excel file to time series format

I have a data in excel with 13 columns defined as year and months. I want to run time series analysis but finds it difficult to plot the data. Please how can I convert this file such that I can analyse the data?
My excel file
Tried plotting but gets error. Error reads "can not plot morethan 10 series"
Say your data is df.
Using base R,
rownames(df) <- df$Year
df$Year <- NULL
ts(as.vector(t(as.matrix(dummy.df))),
start=..., end=..., frequency=12)
You may change start and end depends on your data.

Time and date format change when import excel data to R

I have an excel data which contains dates and times shown as 2019/8/24, and 09:00:00 and so on, I am using
my_data <- read_excel("../data.xls")
to read it but I get $libxls error: Unable to open file$ error.
When I export .xls data to .xlsx and then import to R, I dates and times loose the format and appear in numerical format, 4370, 0.37 and so on.
Is the problem in the way I read the data or my data has problems?

Date changes while importing excel file in R

I have multiple excel files (160) where one 'date' column about 100 observations is in not in proper format. While exporting all the files together the date column changes as follows
Date Column in Excel
Date
05-07-2015
04-07-2015
03-07-2015
02-07-2015
.......
Date column importing in R
Date
42190
42189
42188
42187
......
How to change "42191" to original date format?
Excel may save dates as numeric or maybe they imported in a numeric format. So you can try:
# from Windows Excel:
as.Date(42190, origin = "1899-12-30")
[1] "2015-07-05"
# from Mac Excel:
as.Date(42190, origin = "1904-01-01")
Interestingly, Excel support page define the origin date for Windows excel as "1900-01-01", but from here and here you can see that for R, date of "1899-12-30" should use as the origin date.
copy your date column and special paste it as value in other column and use that to import in R
I've found this to be quite helpful:
library(openxlsx)
dates <- c(42190,42189,42188,42187)
datesConverted <- convertToDate(dates); datesConverted
# "2015-07-05" "2015-07-04" "2015-07-03" "2015-07-02"
Gives you exactly what you're looking for.

R programming - date format

I have an Excel data file with two columns: the first is a column of dates in the format dd-mmm-yy and the second is a column of (jogging) times in minutes. I saved the Excel file as a .CSV file. The name of the file is RunningD.CSV.
First, I extracted the data in R with:
dataD<- read.csv("RunningD.csv")
I then plotted the data in R with the following code:
qplot(as.Date(dataD$Date,'%d-%b-%y'), dataD$Time, xlab="Date",
ylab="Time (mins)", color=dataD$Time)
However, the x-axis only shows the month of the dates, not, as desired, the day-month-year.
See data plot
Does anyone know why?
Thanks in advance.

Reading csv with date and time

I am working in R and reading csv which has date and time in its first column.
I want to import this csv file in R first and then convert it to zoo obect.
I am using the code in R
EURUSD <- as.xts(read.zoo("myfile.csv",sep=",",tz="",header=T))
My csv file contain data in the format:
Date,Open,Low,High,Close
2006-01-02 10:01:00,2822.9,2825.45,2822.1,2824.9
2006-01-02 10:02:00,2825,2825.9,2824,2824.95
2006-01-02 10:03:00,2824.55,2826.45,2824,2826.45
2006-01-02 10:04:00,2826.45,2826.45,2824.9,2825.5
2006-01-02 10:05:00,2825.15,2825.5,2824,2824.85
2006-01-02 10:06:00,2824.7,2825.5,2823.7,2823.8
2006-01-02 10:07:00,2823.95,2824.45,2823.55,2824
2006-01-02 10:08:00,2824,2824.85,2823.5,2824.85
2006-01-02 10:09:00,2824.25,2825.45,2824,2825.45
2006-01-02 10:10:00,2825.2,2827,2825,2827
When I run the above command to import the data in to R I get the folowwwing error :
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
I tried to find all the ways to sort out the issue. I read so many blogs over net but none of the method works for me.
I hope someone would help me.
Although this seems to be an old post, but I want to share my experience since I went through a similar very frustrating process trying to load time series csv data into R.
The problem above is that excel changes the format of the date and time to the following %m/%d/%Y %H:%M, basically it drops the seconds. If you read a file with this format and you have a second resolution data you get multiple date time combinations that are similar. so you cannot simply use the format that ignores seconds because it gives the following error message . "character string is not in a standard unambiguous format"
The solution is to go back to excel and change the format of the date time column to be %m/%d/%Y %H:%M:%S. You can do that by choosing the closest date time default formats to the desired format (in this case it is %m/%d/%Y %H:%M and then manually add :ss at the end. Save the file as a csv file and then read it using the following command:
Data<-read.zoo("file.csv", tz="", header=TRUE,format='%m/%d/%Y %H:%M:%S')
This worked for me and I read a file that has about 900K rows.
It looks like the error is due to R not recognising what format your date column is in (it can't work out -- date/month/year? month/date/year? etc).
You can tell R what format it is in using the format argument to read.zoo (see ?strptime for the specifiers you can use).
For example, if it was date/month/year hour(24-hour clock):minutes, you could do:
EURUSD <- as.xts(read.zoo(file_name,
sep=',',
tz='',
header=T,
format='%d/%m/%Y %H:%M:%S')) # see the 'format' argument?
(Note - in your question the snippet of csv data you showed isn't comma-delimited).
Read the file without using as.xtc, when the date column is like a character. And then convert the dates to POSIXlt class with this function:
library("chron")
DateConvert<-function(x){
dt<-strsplit(x,split = "T")
dt<-unlist(dt)
d1<-dt[1:length(dt) %% 2==1 ]
d2<-dt[1:length(dt) %% 2==0 ]
a<-as.POSIXlt(chron(dates.=d1, times.=d2, format = c(dates = "y-m-d", times = "h:m:s")))
return(a)
}
DateConvert('Your column')
and just then use the function as.xts on you data.

Resources