Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as.Date from 'YYYY.mm' format [duplicate]

Tags:

date

r

as.date

I have a data frame where date is stored as a double e.g., 1993.09 1993.10 1993.11 1993.12

I want to convert this into a date format '%Y %m %d' (with days always 1).

As far as I understand, as.Date() wants a string input. However, for some reason when I convert my dates into string sapply(dates, as.character) the zeros after ones disappear, effectively converting October to January, resulting into two Januaries per year.

dates
1993.07 1993.08 1993.09 1993.10 1993.11 1993.12
sapply(dates, as.character)
sub("[.]", " ", dates)
"1993 07" "1993 08" "1993 09" "1993 1"  "1993 11" "1993 12"

Is there a more straightforward way of converting the dates? Or where do I mess up?

dput:

c(1993.01, 1993.02, 1993.03, 1993.04, 1993.05, 1993.06, 1993.07, 
1993.08, 1993.09, 1993.1, 1993.11, 1993.12)
like image 318
Zlo Avatar asked Nov 12 '15 14:11

Zlo


People also ask

How do I convert a date in Excel without losing formatting?

Copy the dates that are in the cells that you want to convert. Copy the text and paste it into Notepad. Return to Excel and pick the cells into which you wish to paste the dates. Go to Home –> Number and pick the Text format with the cells chosen (from the drop down).

How do I convert YYYY date to mm in Excel?

Create a custom date format Select the cells you want to format. Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


1 Answers

Your problem is that you have something that is a character string, but looks like a numeric and you didn't take care of this during import. R doesn't distinguish between 1993.1 and 1993.10. Both are the same number. Thus, as.character(1993.10) returns "1993.1". You need to use a formating function to make sure that you get two digits after the period, because to as.Date "1993.1" and "1993.01" are the same month.

x <- c(1993.09, 1993.10, 1993.11, 1993.12)
as.Date(sprintf("%.2f.01", x), format = "%Y.%m.%d")
#[1] "1993-09-01" "1993-10-01" "1993-11-01" "1993-12-01"

Of course, x should be imported as a character to begin with.

like image 74
Roland Avatar answered Oct 20 '22 14:10

Roland