chr to date class in R - r

I have a data frame containing dates as characters,dd.mm.yyyy format. want to convert those in date class, format yyyy-m-d. as.date() is not working returning error, do not know how to convert 'dates' to class “Date”
dates <- data.frame(cbind(c("5.1.2015", "6.1.2014", "17.2.2014", "28.10.2014")))
colnames(dates) <- c("dates")
as.Date(dates, format = "%Y-%m-%d")
new_format_dates <- cbind(gsub("[[:punct:]]", "", dates[1:nrow(dates),1]))
as.Date(new_format_dates, format = "%Y-%m-%d")
So I tried to replace the . and reformat those dates under new_format_dates, returning result like [1] NA NA NA NA

Firstly, make your data.frames properly; don't use cbind in data.frame. Next, set the format argument of as.Date to the format you've got, including separators. If you don't know the symbol you need, check out ?strptime.
dates <- data.frame(dates = c("5.1.2015", "6.1.2014", "17.2.2014", "28.10.2014"))
dates$dates_new <- as.Date(dates$dates, format = "%d.%m.%Y")
dates
# dates dates_new
# 1 5.1.2015 2015-01-05
# 2 6.1.2014 2014-01-06
# 3 17.2.2014 2014-02-17
# 4 28.10.2014 2014-10-28

dates <- data.frame(cbind(c("5.1.2015", "6.1.2014", "17.2.2014", "28.10.2014")))
colnames(dates) <- c("dates")
dates$new_Dates <- gsub("[.]","-",dates$dates)
dates$dates <- NULL
dates_new <- as.Date(dates$new_Dates, format = "%d-%m-%Y")
dates_new <- data.frame(dates_new)
print(dates_new)

Related

How can I delete numbers and characters from my date and convert a character column to a date column

I have a dataframe with the column name perioden. This column contains the date but it is written in this format: 2010JJ00, 2011JJ00, 2012JJ00, 2013JJ00 etc..
This column is also a character when I look at the structure. I've tried multiple solutions but so far am still stuck, my qeustion is how can I convert this column to a date and how do I remove the JJ00 part so that you only see the year format of the column.
You can try this approach. Using gsub() to remove the non desired text (as said by #AllanCameron) and then format to date using paste0() to add the day and month, and as.Date() for date transformation:
#Data
df <- data.frame(Date=c('2010JJ00', '2011JJ00', '2012JJ00', '2013JJ00'),stringsAsFactors = F)
#Remove string
df$Date <- gsub('JJ00','',df$Date)
#Format to date, you will need a day and month
df$Date2 <- as.Date(paste0(df$Date,'-01-01'))
Output:
Date Date2
1 2010 2010-01-01
2 2011 2011-01-01
3 2012 2012-01-01
4 2013 2013-01-01
We can use ymd with truncated option
library(lubridate)
library(stringr)
ymd(str_remove(df$Date, 'JJ\\d+'), truncated = 2)
#[1] "2010-01-01" "2011-01-01" "2012-01-01" "2013-01-01"
data
df <- data.frame(Date=c('2010JJ00', '2011JJ00', '2012JJ00', '2013JJ00'), stringsAsFactors = FALSE)

Converting factor date time columns into POSIXct in R

I have another problem. I have a date time column in a data frame, which when i upload it comes as factor and I want it to be POSIXct
str(ida$DATA_TRAMA)
Factor w/ 1122932 levels "1-1-2010 00:00:51",..: 629101 629120 629128 629132 629139 629149
And i want it to be POSIXct (%Y-%m-%d %H:%M:%S) format. I already tried all of these methods but none of them seem to work. Whichever i apply it gets NA values.
ida$DATA_TRAMA<- as.POSIXct(ida$DATA_TRAMA,format='%d/%m/%Y %H:%M:%S')
ida$DATA_TRAMA<- as.POSIXct(as.character(ida$DATA_TRAMA), format = "%d/%m/%Y %H:%M")
ida$DATA_TRAMA <-format(ida$DATA_TRAMA, "%Y-%m-%d")
ida$DATA_TRAMA <- as.POSIXct(ida$DATA_TRAMA, format = '%Y-%m-%d:%H:%M:%S')
ida$DATA_TRAMA <- as.POSIXlt(as.character(ida$DATA_TRAMA), format="%m/%d/%Y %H:%M:%S")
ida$DATA_TRAMA <- strptime(ida$DATA_TRAMA,"%Y-%m-%d %H:%M:%S")
Do you know how to do it?
With a "factor" argument as.POSIXct will invoke as.POSIXct.default and that uses as.POSIXlt which has a "factor" method so just do:
DF <- data.frame(d = "1-1-2010 00:00:51") # test data. d has factor class.
transform(DF, d = as.POSIXct(d, format = "%m-%d-%Y %T"))
giving:
d
1 2010-01-01 00:00:51

Convert to date format error: character string is not in a standard unambiguous format

I want to calculate the number of months between two dates but before that I have a problem when loading the data in r. In csv sheet the format is mm/dd/yyyy but in R the variable is classified as character.
I tried
data$issue_d <- format(as.Date(data$issue_d), "%m/%d/%Y")
and to convert as date first but it gives the following error
character string is not in a standard unambiguous format
Any suggestion for this?
Example input:
issue_d <- c("Dec,2011","Nov,2014","Apr,2015")
Try below:
# example data
df1 <- data.frame(
issue_d1 = c("Dec,2011","Nov,2014","Apr,2015"),
issue_d2 = c("Nov,2015","Sep,2017","Apr,2018"))
library(zoo)
df1$Months <-
(as.yearmon(df1$issue_d2, "%b,%Y") -
as.yearmon(df1$issue_d1, "%b,%Y")) * 12
df1
# issue_d1 issue_d2 Months
# 1 Dec,2011 Nov,2015 47
# 2 Nov,2014 Sep,2017 34
# 3 Apr,2015 Apr,2018 36

Convert different date-time-formats at once (strptime)

Hi :) I have a column of my data.frame which contains dates in two formats. Here is an short minimal example:
D = data.frame(dates = c("3/31/2016", "01.12.2015"))
dates
1 3/31/2016
2 01.12.2015
With the nice function strptime I can easily get date-times for each format:
D$date1 <- strptime(D$dates, format = "%m/%d/%Y")
D$date2 <- strptime(D$dates, format = "%d.%m.%Y")
I already managed a workaround with:
D$date12 <- do.call(pmin, c(D[c("date1","date2")], na.rm=TRUE) )
To achieve this:
dates date1 date2 date12
1 3/31/2016 2016-03-31 <NA> 2016-03-31
2 01.12.2015 <NA> 2015-12-01 2015-12-01
Is there are more sophisticated way to do this transformation (from dates to date12) at once?
Regards
You can use the anytime package.
library(anytime)
anytime::addFormats("%d.%m.%Y")
anydate(D$dates)
Note that the argument in anydate has to be a vector, so just select the coloumn dates.
Or use lubridate
parse_date_time(D$dates, c("mdy", "dmy"))

R some time stamps changing to NA

My Code is reading in a CSV file and converting the time stamp column to the R time format
DF <- read.csv("DF.CSV",head=TRUE,sep=",")
DF[51082,1]
[1] 03/01/2012 19:29
DF[1,1]
[1] 02/24/12 00:29
It reads it in properly and the above 2 rows are displayed as expected
DF$START <- as.POSIXct(strptime(paste(DF$START),format="%m/%d/%y %H:%M"))
DF[1,1]
[1] "2012-02-24 00:29:00 GMT"
DF[51082,1]
[1] NA
After converting them to the R time format using strptime and then displaying them again some of the values have NA and there was no error message displayed or reason for it that I can figure out
You have (at least) two different date formats,
one in %Y (4-digit years), one in %y (2-digit years).
Unless 12 really means 12AD, you need to try both.
DF <- data.frame(
START = c(
"03/01/2012 19:29",
"02/24/12 00:29"
),
stringsAsFactors = FALSE
)
coalesce <- function (x, ...) {
z <- class(x)
for (y in list(...)) {
x <- ifelse(is.na(x), y, x)
}
class(x) <- z
x
}
DF$START <- coalesce(
as.POSIXct(strptime(DF$START, format="%m/%d/%y %H:%M")),
as.POSIXct(strptime(DF$START, format="%m/%d/%Y %H:%M"))
)
# START
# 1 2012-03-01 19:29:00
# 2 2012-02-24 00:29:00
Try to use this:
> DF$START <- as.POSIXct(strptime(paste(DF$START),format="%m/%d/%Y %H:%M"))
This adds year with century.

Resources