as.Date keeps on returning NAs [duplicate] - r

This question already has answers here:
How to convert a character string date to date class if day value is missing
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
So I've been through some Stack Exchange answers, and I can't resolve this.
I have a column in a dataframe that has dates as characters as follows
2011-12
2012-04
2011-10
etc
I would like to convert these to date formats which I have tried to do as follows:
Tots$DatesMerge<-as.Date(Tots$DatesMerge,"%Y-%m")
but I get NA's back all the time.
I tried to do as here but no joy. I'm really not sure what I'm doing wrong.

I'd say as.Date won't be able to work on values where there's no day of the month. You could try with zoo, as long as you don't mind it coming out as a yearmon class:
library( zoo )
as.yearmon( Tots$DatesMerge )
Alternatively, you can specify a day of the month to use as a dummy:
as.Date( paste0( Tots$DatesMerge, "-15" ) )

Edit: there is already an answer and it is a duplicate, but I suppose the explanation can be useful for further readers, so I'll leave it.
Explanation
This comes from the documentation in R, "Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates".
In R, dates are thus dependent on year, month and days or at least an integer that represent the span (in days) from or to 1970-01-01. As such, the base Dates package in R cannot convert the data formated in years and month into dates since there are no days.
Solution
As a consequence, you have the option, if you go with the base R package, to provide a a day that would be used to convert your data.
Tots$DatesMerge <- as.Date(paste0(Tots$DatesMerge,"01"),"%Y-%m-%d")

Related

Converting an integer date [duplicate]

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

%b-%Y date conversion gives NA [duplicate]

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.

Why does R impute the 12th of the month when formatting a year as a Date [duplicate]

This question already has answers here:
Convert four digit year values to class Date
(5 answers)
Closed 5 years ago.
I note in R if one calls as.Date(as.character(2002:2013), format='%Y') the output is
[1] "2002-01-12" "2003-01-12" "2004-01-12" ...
I would like R to give me the first of the month instead. I could supply the whole date, paste(2002, '01', '01', sep='-'), but am curious why the year-only format imputes the 12th of the month and also to see other solutions.
Ah, just found my answer: The missing sections of the Date object (month/day) are imputed from today's date (System Date).

How to get week starting date from a date in R [duplicate]

This question already has answers here:
How to find Previous Sunday in R
(4 answers)
Closed 5 years ago.
I have a dataset with a column containing dates. I want to find the week starting dates for those date values.
I get the week number using week function from lubridate.
For example,
week(as.Date("04/20/2017", "%m/%d/%Y"))
#Solution
[1] 16
Instead of weeknum, is there a way to get the starting date of the week? In this case I am expecting either "04/16/2017" or "04/17/2017". I am not very particular if the week starts from Sunday or Monday.
I looked at this question, but didn't get much from it.
Use the floor_date function from the lubridate package.
library("lubridate")
floor_date(as.Date("04/20/2017", "%m/%d/%Y"), unit="week")
You can use below
as.Date(format(as.Date("04/20/2017", "%m/%d/%Y"),"%Y-%W-1"),"%Y-%W-%u")
[1] "2017-04-17"

How to add a column and convert dates in R [duplicate]

This question already has an answer here:
How can I convert this to month?
(1 answer)
Closed 8 years ago.
So I have the following data set which shows temperatures for 365 days of the year. Each date is labeled by the day of the week in a y-m-d format. I'm trying to add a new column called month, which will display the current month which the date shows. Ex: 2014-01-01 will show January while 2014-10-25 will show October. Can anyone help? I'm trying to use Lubridate, but I'm still new at R, and am having a lot of trouble.
The format.Date function return a vector of character values and the "%B" parameter determine the result as full month values:
dfrm$Month <- format( as.Date(dfrm$yourDate) , "%B")
If yourDate-column were already an R Date-object then the as.Date is not needed. Consult the help page for strptime. There can be separators in between the (possibly multiple) format specs
?strptime

Resources