0

I am trying to read a date column from a dataframe, which is stored as a string |(see column 'DateString'. This is how my data looks like:

  X. Date_String       ASIN Stars positive_rating
1  0    20150430 B00GKKI4IE     5               0
2  1    20150430 B00GKKI4IE     5               0
3  2    20150430 B00GKKI4IE     5               0
4  3    20150429 B00GKKI4IE     5               0
5  4    20150428 B00GKKI4IE     5               0
6  5    20150428 B00GKKI4IE     5               0

and this is what I am using to format this column as Date

data$date.of.review  <- as.Date(data$Date_String, "%Y%m%d")

and getting an error message

Error in charToDate(x) : 
  character string is not in a standard unambiguous format

any ideas how to solve it? thanks

Here is the structure of the dataframe:

structure(list(X. = 0:5, Date_String = c(20150430L, 20150430L, 
20150430L, 20150429L, 20150428L, 20150428L), ASIN = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("B00GKKI4IE", "B00I4OBXWI", "B00IB17BFM", 
"B00IN2WD5C", "B00J58F0IA", "B00K7NCS9G", "B00KJEZIBS", "B00KZER5GS", 
"B00MK39H68", "B00O1GTTWY"), class = "factor"), Stars = c(5L, 
5L, 5L, 5L, 5L, 5L), positive_rating = c(0, 0, 0, 0, 0, 0)), .Names = c("X.", 
"Date_String", "ASIN", "Stars", "positive_rating"), row.names = c(NA, 
6L), class = "data.frame")
Nirke
  • 173
  • 1
  • 5
  • 12
  • 2
    `as.Date("20150430", "%Y%m%d")` seems to work. Are you sure that "Date_String" is a string and not a numeric value? What exactly is the structure of your data.frame. How about including a `dput()` as described in [how to make a great reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick May 04 '15 at 06:09
  • 1
    If you use `library(lubridate)`, it would also take numeric values. ie. `ymd(data$Date_String)#[1] "2015-04-30 UTC" "2015-04-30 UTC" "2015-04-30 UTC" "2015-04-29 UTC" [5] "2015-04-28 UTC" "2015-04-28 UTC"` Otherwise, as @MrFlick mentioned, convert the column to character. i.e. `as.Date(as.character(data$Date_String), format="%Y%m%d")` – akrun May 04 '15 at 06:13

1 Answers1

2

The class is integer if you wrap the variable in as.character as.date can read it.

sapply(data, class)
data$date.of.review  <- as.Date(as.character(data$Date_String), "%Y%m%d")
data
Jonno Bourne
  • 1,931
  • 1
  • 22
  • 45