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"
Related
I have a column of dates I need to reformat, they are in the format m/d/yyyy, but as the dates grow the format becomes mm/dd/yyyy. When I try to run
as.Date(x, format = '%m/%d/%y')
I get NA's
How can I reformat the dates to mm/dd/yyyy?
Here is an example:
# convert date info in format 'mm/dd/yyyy'
strDates <- c("01/05/1965", "08/16/1975")
strDates
Output:
[1] "01/05/1965" "08/16/1975"
Convert the dates using this code:
dates <- as.Date(strDates, "%m/%d/%Y")
Output:
[1] "1965-01-05" "1975-08-16"
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"
I am trying to convert a numeric column "DATE" to date column "NEW_DATE" in format YYYYMM:
DATE NEW_DATE
1.2020 202001
I have tried as.Date but it returns the following error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
The structure function says that the column is num so not sure how to proceed.
We can use zoo's as.yearmon and then convert it with format.
format(zoo::as.yearmon('1.2020', "%m.%Y"), "%Y%m")
#[1] "202001"
So for the dataframe, we can do :
df$NEW_DATE <- format(zoo::as.yearmon(df$DATE, "%m.%Y"), "%Y%m")
Or in base R we can paste an arbitrary date and convert to date.
df$NEW_DATE <- format(as.Date(paste0(df$DATE, ".1") "%m.%Y.%d"), "%Y%m")
Base R solution:
# Isolate the month:
date_month <- gsub("\\..*", "", df$DATE)
# Store the NEW_DATE vector in desired format:
df$NEW_DATE <- paste0(gsub(".*\\.", "", df$DATE),
ifelse(nchar(date_month) == 1, paste0("0", date_month), date_month))
# Data:
df <- data.frame(DATE = "1.2020", stringsAsFactors = FALSE)
I imported a csv file with dates to R. The dataframe is named DT, and one of the column called date has year and month in it.
class(DT$date)
[1] "factor"
head(DT$date)
[1] 2013年1月 2013年1月 2013年1月 2013年1月 2013年1月 2013年1月
60 Levels: 2013年10月 2013年11月 2013年12月 2013年1月 ... 2017年9月
And I tried to use as.Date to convert it to date format:
date <- as.Date(DT$date, format = "%Y/%m")
date <- as.Date(as.factor(DT$date), format = "%Y/%m")
date <- as.Date(as.factor(DT$date), format = "%Y/%m/%d")
During this operation I lose all my dates. Then I tried the lubridate package:
date <- ymd(DT$date)
date <- as.yearmon( DT$date)
However, I lose all my dates again. Can anyone help me to change this factor to Date in R?
Thanks.
The following seems to work:
DT = data.frame(date = c("2013年1月", "2013年11月", "2017年9月"))
lubridate::parse_date_time(DT$date, orders = "ym")
You should generally start with the parse_date_time function.
I have a dataframe as bellow
library(lubridate)
Date <- c("18.11.2016 21:03:41", "19.11.2016", "20.11.2016","21.11.2016")
df = data.frame(Date)
df
I to get
df$Date
[1] "2016-11-18" "2016-11-19" "2016-11-20" "2016-11-21"
& try to convert it to date like this
df$Date = dmy(df$Date)
and I get
Warning message:
1 failed to parse.
How to fix it?
Try this:
s <- c("2004-03-21 12:45:33.123456", # ISO
"2004/03/21 12:45:33.123456", # variant
"20040321", # just dates work fine as well
"Mar/21/2004", # US format, also support month abbreviation or full
"rapunzel") # will produce a NA
p <- toPOSIXct(s)
options("digits.secs"=6) # make sure we see microseconds in output
print(format(p, tz="UTC")
Read about it more here.