-1

I only want to select the data between 2015-1 and 2018-1.

usefuldata1 <-usefuldata[usefuldata$zeit > "2014 - 12",]

usefuldata2 <-usefuldata1[usefuldata1$zeit < "2018-2",]

Bigger than 2014 works. However, smaller than 2018 does not. I get the same number as before.

My data looks as follows:

x <- sample(letters, size = 30, replace = T)
y <- paste(sample(2010:2020, size = 30, replace = T),
           sample(1:12, size = 30, replace = T), sep = "-")

df <- data.frame(name = x, date = y)
Mostafa90
  • 1,674
  • 1
  • 21
  • 39
SIDI
  • 7
  • 3

1 Answers1

0

There is a very userfull type in R called Date, by experience about 90% of all problems about dates can be solved by that.

   see: ?as.Date for help

In your case, the first thing to do is format y as a Date type adding the days.

   x <- sample(letters, size = 30, replace = T)
   y <- paste(sample(2010:2020, size = 30, replace = T),
       sample(1:12, size = 30, replace = T),
       "01", sep = "-") # <- this is the day (01) for all.

Then we have to format it as a Date type.

   y = as.Date(y, format = "%Y-%m-%d") # year complete (%Y), month (%m) and day (%d)
   df <- data.frame(name = x, date = y)

And subset them.

    dateIndex = which(y > "2014-12-31" & y < "2018-01-01")
    subset_df = df[dateIndex,]

I hope this helps you.