This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 8 years ago.
this is likely really simple, but I am unable to find the answer (probably not searching for the right thing...)
How can I insert a lead 0 into a column of numeric values...
Example:
values <- c(91414, 80113)
Such that they appear like this afterward:
091414, 080113
Then I would like to transform to a date, and I assume I would just:
date <- as.Date(values, format = "%m%d%y")
Any and all help appreciated...
Try this, using the lubridate package for the date transformation:
X<-c(91414,80113)
X_date<-mdy(paste("0",as.character(X),sep=""))
X_date
[1] "2014-09-14 UTC" "2013-08-01 UTC"
This turns your numeric data to text and add a "0" to the the start of each value, then mdy does the date conversion.
Related
This question already has answers here:
as.Date with dates in format m/d/y in R
(4 answers)
Closed 2 years ago.
I am having an annoying issue in R that is not recognizing the correct date in my dataframe as the max date. I am creating a weighted average based on subtracting the date in the dataframe from the max date, so this is an annoying issue to deal with. I have tried reformatting all of the dates, this did not work.
Anything that I can try? The dates range from 1/1/2020 to 12/23/2020. But when I use max(PlayerData$Date) I am getting 3/9/2020.
Thanks
The issue is that max() is not working for characters. You need to transform it to date class. Here an example:
#Data
PlayerData <- data.frame(Date=c('3/9/2020','1/1/2020','12/23/2020'),stringsAsFactors = F)
Your approach:
#OP
max(PlayerData$Date)
Output:
max(PlayerData$Date)
[1] "3/9/2020"
Setting as date:
#Date
max(as.Date(PlayerData$Date,'%m/%d/%Y'))
Output:
max(as.Date(PlayerData$Date,'%m/%d/%Y'))
[1] "2020-12-23"
This question already has answers here:
How to convert Excel date format to proper date in R
(5 answers)
Closed 2 years ago.
I want to convert numeric date format to date. I.E. I have value 44109, but want to convert it to 2020-10-06. How is it possible?
Try something like below
> as.Date(44109,origin = "1900-01-01")
[1] "2020-10-07"
this works as well
x <- as.POSIXct(44109 * (60*60*24), origin="1899-12-30", tz="GMT")
"2020-10-05 GMT"
This question already has answers here:
How do I convert date to number of days in R
(7 answers)
Closed 4 years ago.
My question doesn't have to do with my own dataset, but since I'm new to R, I wanted to make sure I knew how to work with dates, so I'm searching up the different ways to manipulate and compare dates in R.
I recently read an answer to a question regarding converting a date into an integer date using the as.numeric () function. Here is the answer that was accepted: https://stackoverflow.com/a/8215581/10864249
So from that answer, my understanding is that the date was converted into seconds.
Why would anyone want to use the as.numeric() function if we're going to only get seconds?
Can we convert the integer date into a smaller integer, like # of days by just dividing by 365.25 or by months even by dividing by 12, then? I assume it'd be easier to compare dates that way, rather than in seconds.
Thanks!!
coercing a date object into a numeric object will give you "the number of days since 01/01/1970"
my_date = as.Date('2015-01-01')
my_date
#[1] "2015-01-01"
class(my_date)
# [1] "Date"
as.numeric(my_date)
# [1] 16436
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.
This question already has answers here:
Changing to Date Format in R
(2 answers)
Closed 8 years ago.
Hello, I cannot figure out where the problem is even after consulting the documentation for as.Date, although it must be a very silly thing.
I have a character vector DD that contains dates:
unique(DD)
[1] NA "1999-01" "2013-08" "2013-02" "2013-03" "2013-01" "2013-07" "2012-12" "2013-05"
[10] "2012-11" "2013-04" "2013-06" "2011-11" "2011-12" "2011-06" "2010-07" "2011-01" "2010-03"
I want to convert it into date format. I tried
DD2 = as.Date(DD, format = "%Y-%m")
but the result gives only NAs :
unique(DD2)
[1] NA
Can anyone see the problem?
I'm not really sure why this doesn't work, but it appears you do have to completely specify the date for as.Date to work. Instead, you could use as.yearmon from the zoo package.