0

I have a data set and want to make a list (or whatever works best) to then use it later for a for-loop. However, it throws me the error message

"character string is not in a standard unambiguous format".

I don't know what this means... Sorry, if this is a basic question. I am quite new to coding.

data <- read.csv("~/atx.csv")
assets <- data[,-(5:18)]
date <- as.Date(data$Date,"%Y-%m-%d")
l <- cbind(assets,date)

list(
  q1 = l[l$date >= "2012-01-01" & l$date <= "2012-03-31",],
  q2 = l[l$date >= "2012-04-01" & l$date <= "2012-06-30",],
  q3 = l[l$date >= "2012-07-01" & l$date <= "2012-09-30",],
  q4 = l[l$date >= "2012-10-01" & l$date <= "2012-12-31",],
  q5 = l[l$date >= "2013-01-01" & l$date <= "2013-03-31",],
  q6 = l[l$date >= "2013-04-01" & l$date <= "2013-06-31",],
  q7 = l[l$date >= "2013-07-01" & l$date <= "2013-09-31",],
  q8 = l[l$date >= "2013-10-01" & l$date <= "2013-12-31",]
)

this is what my data looks like after running l <- cbind(assets,date)

Katharina Böhm
  • 125
  • 1
  • 8
  • 2
    Hi, the error message probably means that R can't recognize your date in the standard ISO format which is `"2020-06-04"`. How does `data$Date` look like, could you give an example? – jay.sf Jun 04 '20 at 05:52
  • At what line do you get this error? – kangaroo_cliff Jun 04 '20 at 05:52
  • @jay.sf ok thank you. So how do I change that now? Bc i actually thought I specified the format in date <- as.Date(data$Date,"%Y-%m-%d") ? – Katharina Böhm Jun 04 '20 at 05:56
  • @Suren when running the list – Katharina Böhm Jun 04 '20 at 05:56
  • @KatharinaBöhm I need an example. – jay.sf Jun 04 '20 at 05:57
  • @KatharinaBöhm Thanks for your efforts to share some data. However, from the image nobody can examine the structure of your data, which is important to help you. Please use `dput(data)` as is standard on Stack Overflow to share data. For help on this, you may want to read: [how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – jay.sf Jun 04 '20 at 06:05
  • You get this type of error if you do `as.Date(1,"%Y-%m-%d")`. Thus, your data might not be what you think it is. Possibly it got turned into numbers during Excel export? – Roland Jun 04 '20 at 06:06

1 Answers1

0

Verify first if class(l$Date) is of type "Date", you can use cut/findInterval to divide this data into 3 months interval.

list_data <- split(l, findInterval(l$date, seq(as.Date("2012-01-01"), 
                       as.Date("2014-01-01"), by = "3 months")))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213