This question already has an answer here:
format year-month to POSIXct [duplicate]
(1 answer)
Closed 3 years ago.
I have two different dataframe with different date formats
date1<-c('2001-01-30', '2001-02-25')
data2 <- c('200101','200102')
I want to convert these dates in the same format so I can merge my two different dataframes by date.
The frequency is on a monthly basis.
Can someone help me with this task?
EDIT(Caution): This is not best practice and the use of standard parsers is recommended. However, different requirements may come up and therefore this answer simply shows how to get the expected format in the OP and in a comment from OP.
We could use:
paste0(substring(data2,nchar(data2)-1,nchar(data2)),"/",substring(data2,1,4))
#[1] "01/2001" "02/2001"
To get the reverse:
paste0(substring(data2,1,4),"-",substring(data2,nchar(data2)-1,nchar(data2)))
[1] "2001-01" "2001-02
Related
This question already has answers here:
How to convert Excel date format to proper date in R
(5 answers)
Closed 4 years ago.
I am new to R.
How to format a column which contains over 1000 records of dates(this is the format i am looking for 2016-01-05, 2016-04-12).
After importing the excel file in to R, above dates have changed to 16896 16903 and so on, what is the best way to format it.
library(lubridate)
date <- ymd("2016-01-05")
this will do the trick for you. change the date with you column name
This question already has answers here:
Extract time from timestamp?
(4 answers)
Closed 4 years ago.
So I have a variable which measures the time that an entry was logged into the database, currently the form of these entries is:
"2018-09-22T14:00:09Z"
"2018-09-26T08:21:16Z"
Basically I need to extract from these strings only the time section of the entry. So the 8 characters between T and Z. By the way it is always letters T and Z that surround the time.
I would also like to store these times in a new variable called "times" and this be of a time class.
Is this at all possible in R??
Thanks in advance.
One base R option uses sub:
ts <- c("2018-09-22T14:00:09Z", "2018-09-26T08:21:16Z")
out <- sub("^.*T(.*)Z$", "\\1", ts)
[1] "14:00:09" "08:21:16"
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")
This question already has answers here:
How to format a number as percentage in R?
(10 answers)
Represent numeric value with typical dollar amount format
(4 answers)
Closed 5 years ago.
I want to know if it's possible to display values on a table as percentages but still be able to do arithmetic operations with them.
I have tried using percent() from scales package, but it seems like it transforms the values from numeric to character. I have tried to convert them back using as.numeric(), but it will not work either.
Any solutions? Thank you.
One very hacky way to do this would be as follows
x <- runif(10)
class(x) <- "percent"
print.percent <- function(x) print(scales::percent(as.numeric(x)))
This is probably useful for quick analyses or short scripts but I wouldn't put this into any kind of package or shared code.
This question already has answers here:
Understanding the order() function
(7 answers)
Closed 6 years ago.
I'm pretty new to r but picking it up gradually.
My question is, I want to make my date vector start from the earliest date rather from the newest. I have about 50 odd rows and want it in order of earliest first.
head(dates1)
[1] "2016-03-04" "2016-02-26" "2016-02-19" "2016-02-12" "2016-02-05" "2016-01-29"
I've tried order() but it gives back numeric values, I want to keep them as dates.
Thanks if you can help.
Try the following:
dates1 <- c("2016-03-04", "2016-02-26",
"2016-02-19", "2016-02-12",
"2016-02-05", "2016-01-29")
dates1 <- as.Date(dates1)
sort(dates1)
Order returns the indices, to get the same result you can do the following:
dates1[order(dates1)]