3

I would think this should be an extremely easy solve; but, I can't find it on here; and, the instructions that I've found elsewhere haven't worked. All I'm trying to do is use a simple paste function.

In my data frame, I have a date variable formatted "yymmdd":

> str(g.2015.1990$DATE)
 int [1:60464] 150410 150411 150412 150420 150421 150422 150423 150424 150425 150426 ...

R interprets this as an integer, but I need to format it as a date. My problem arises when I get to the decade 2000-2009. R drops the leading 0s. So, 2001-2009 are formatted "ymmdd"; October through December of 2000 are formatted "mmdd"; and, January through September of 2000 are formatted "mdd".

I figured I could break the vector up into four sections (I had to rbind it year-by-year to assemble it anyways) and paste either none, one, two, or three 0s in front, as appropriate, to create a consistent, 6-digit character string that I can then convert to a date.

I've not yet taken the time to break this variable up into the aforementioned sections, as I've not yet found a successful solution to my problem; however, here is what I've tested on the variable in its entirety:

datex = paste("0", g.2015.1990$DATE, sep = "")
datex = paste(0, g.2015.1990$DATE, sep = "")
datex = paste("0", as.character(g.2015.1990$DATE), sep = "")
datex = paste(0, as.character(g.2015.1990$DATE), sep = "")

Each one returns the same error:

Error in View : 'names' attribute [1254] must be the same length as the vector [1]

Please tell me what I'm doing wrong! I swear this should be such as easy fix.

2 Answers2

2

You want sprintf() here, instead of paste. Then you can use any date conversion func, but I like lubridate

# say you have 2009-10-11 and 2010-10-11, but yymmdd and numeric, so leading
# 0 is dropped on '09...
your_vec <- c(91011, 101011)

# convert to 6 char string (result: "091011" "101011")
new_vec <- sprintf('%06d', your_vec)

# but if you must use paste... (same result: "091011" "101011")
ifelse(nchar(your_vec) == 5, paste0('0', your_vec), your_vec)

# either way, now you can make it a date
library(lubridate)
ymd(new_vec)

# result:
# "2009-10-11 UTC" "2010-10-11 UTC"

(I picked new example data because yours above doesn't actually have the problem of the missing leading zero)

arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • Thanks for the help! You're solutions make sense logically; unfortunately, I'm still having issues: > datex <- sprintf("%06d", g.2015.1990$DATE) > View(datex) Error in View : 'names' attribute [1186] must be the same length as the vector [1] This is the same error I was getting before. Any suggestions on how to solve it? – Brandon McCormick Jul 23 '16 at 14:46
1

to create a consistent, 6-digit character string that I can then convert to a date.

So you need:

datex <- sprintf("%06d", g.2015.1990$DATE)
## formatted string: 6-digit integer, padding 0 ahead

Example

sprintf("%06d", 150410)    ## 2015-04-10
# "150410"

sprintf("%06d", 90410)    ## 2009-04-10
# "090410"

sprintf("%06d",410)    ## 2000-04-10
[1] "000410"

If you later want to convert datex to Date object, do:

datex <- as.Date(datex, "%y%m%d")

Example

as.Date("150410", "%y%m%d")
# [1] "2015-04-10"

as.Date("090410", "%y%m%d")
# [1] "2009-04-10"

as.Date("000410", "%y%m%d")
# [1] "2000-04-10"
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • this works if the data is character, with leading zeros. But the poster has integers with no leading zeros. `format(as.Date(090410,"%y%m%d"), "%Y-%m-%d")` `Error in charToDate(x) : character string is not in a standard unambiguous format` – billspat Jul 23 '16 at 04:41
  • Thanks for the help! You're solutions make sense logically; unfortunately, I'm still having issues: > datex <- sprintf("%06d", g.2015.1990$DATE) > View(datex) Error in View : 'names' attribute [1186] must be the same length as the vector [1] This is the same error I was getting before. Any suggestions on how to solve it? – Brandon McCormick Jul 23 '16 at 14:49
  • 1
    ok, it looks like i can work with it, but not view it... i don't understand R. i guess, problem solved though! thanks for the help. – Brandon McCormick Jul 23 '16 at 15:05
  • oh, this is intersting: > attributes(g.2015.1990$DATE) NULL – Brandon McCormick Jul 23 '16 at 15:07
  • 1
    when i do datex <- sprintf("%06d", g.2015.1990$DATE[1:10]) it works exactly as it should. when i "view" it, everything shows up exactly as i would expect. maybe i should get away from "View" as well. it seems to work fine in further manipulations. just unable to view it for some reason – Brandon McCormick Jul 23 '16 at 15:25