Converting values to dates in R [duplicate] - r

This question already has answers here:
Convert integer to class Date
(3 answers)
Closed 2 years ago.
I have a very simple question
I have this value:
x <- 19630730
with class numeric. The as.Date() does not give me the expected result: 1963-07-30
what is the potential remedy to convert x to a date?
Thanks

Convert to character first
x <- 19630730
as.Date(as.character(x), "%Y%m%d")
#[1] "1963-07-30"
If you use lubridate::ymd you don't need to convert to character.
lubridate::ymd(x)

Related

Date to Month in R data [duplicate]

This question already has answers here:
Converting date in Year.decimal form in R
(2 answers)
Closed 3 years ago.
I have one column(date) in numeric format. I want to extract date in the format of (day, month) from numeric numbers in the table. The table name in ABC and column name is Date.
Date(sample)
95.00000
95.08333
95.16667
95.25000
95.33333
95.41667
Q: Shall I first convert numeric numbers to date class and then extract date?
Can anyone help with this?
This should do the trick in base R :
format(as.Date(as.numeric(Sys.Date()), origin = "1970-01-01"), "(%d, %m)")
#(26, 02)

Change character vector 2000Q1, 2000Q2,.. etc. to date type in R [duplicate]

This question already has an answer here:
Convert date field to Quarter in R
(1 answer)
Closed 3 years ago.
Is it possible to change a vector of character values 2000Q1, 2000Q2,2000Q3, 2000Q4,2001Q1, ..., i.e. a vector of quarterly dates, to date values? I have already tried something like this:
Dat <- c(2000Q1, 2000Q2,2000Q3, 2000Q4,2001Q1)
Dat<-as.Date(Dat ,format="%Y%Q")
But that didn't work. Could someone help me out?
In base R, you can do
as.Date(
sapply(strsplit(Dat, 'Q'),
function(x) paste(1, seq(1,10,3)[as.integer(x[2])], x[1], sep = '-')),
format = '%d-%m-%Y')

Convert string to numeric in a list in R [duplicate]

This question already has answers here:
Convert currency with commas into numeric
(4 answers)
removing particular character in a column in r
(3 answers)
Closed 4 years ago.
I am new to R. I am working on a dataset which is a large list, there is a column with numbers and strings. I want to remove the string and convert to numeric.
I have 100,000+
I want 100,000 and numeric
You can use gsub to transform the string:
as.numeric(gsub("[^0-9.]", "", "100,000+"))
# [1] 1e+05
Here, all characters, except digits and ., are removed before applying as.numeric.

Date Conversions in R [duplicate]

This question already has answers here:
integer data frame to date in R [duplicate]
(3 answers)
Closed 4 years ago.
My raw data file have the date columns with numbers like "180410". These are read as integers when I import the data into R. Now I tried converting them to dates using as.date but I got only N/A in these columns. Is there a way to covert them to actual dates? Please help
You need to check the format part of as.Date, it needs to match the input (2 digit year, and no spacing). If you have the wrong input you get NA. The following example produces NA because there are no hyphens in your input:
as.Date("180412", "%y-%m-%d")
Use this instead:
as.Date("180412", "%y%m%d")

Incorrect Date Format In R [duplicate]

This question already has answers here:
R Read abbreviated month form a date that is not in English
(2 answers)
Closed 4 years ago.
I try to change the format of "20FEB12:00:00:00" to Date and R gives me "NA"
DonĀ“t know what I am doing wrong:
strptime("20FEB12:00:00:00", "%d%b%y:%H:%M:%S")
Thanks!
Sys.setlocale(category = "LC_TIME",locale = "C")

Resources