0

I am trying to subset a range of dates from a dataframe in R.

The command I'm using is

maumee952000 = maumeedata[maumeedata$Datetime..date.and.time.of.sample.collection. >= "1995-01-01" &
                            maumeedata$Datetime..date.and.time.of.sample.collection. <= "2000-12-31",]

Using this command, however, results in a dataframe that consists only of NA values under every column. I've made sure that my dates are classed properly.

Here is a sample of my date data

> dput(maumeedata$Datetime..date.and.time.of.sample.collection.[1000:1050])
structure(c(18139, 18140, 18141, 18142, 18143, 18144, 18145, 
18145, 18146, 18147, 18148, 18149, 18150, 18151, 18152, 18152, 
18153, 18153, 18154, 18154, 18155, 18155, 18156, 18156, 18157, 
18157, 18158, 18158, 18159, 18159, 18159, 18160, 18160, 18161, 
18161, 18162, 18162, 18163, 18164, 18164, 18165, 18165, 18166, 
18166, 18166, 18167, 18167, 18168, 18168, 18169, 18169), class = "Date")

I would appreciate anyone who could tell me what I'm doing wrong.

  • try using lubridate::ymd('1995-01-01') and same for other date – Karthik S Oct 23 '20 at 17:32
  • As @KarthikS suggested, some languages (e.g., SQL) will let you do a comparison between a `Date` and a string that resembles a date. R is not one of them. If one of the objects being compared is a string (such as `"1995-01-01"`), then the comparison is done as strings, not as dates; unfortunately, `"2000-01-01" >= "1995-01-01" & "2000-01-01" <= "2000-12-31"` does work, because string-comparison in this format is doing what is initially desired. The fact that it doesn't work suggests that something is wrong with your data. – r2evans Oct 23 '20 at 17:51
  • Please provide a sample of your data, using `dput(...)` or `data.frame(...)`. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Oct 23 '20 at 17:51
  • Hi r2evans. I edited my post. I used the dput command to provide a sample of my date data. I am not sure why, but the output from that looks much different than if I simply use `maumeedata$Datetime..date.and.time.of.sample.collection.` – LHWilliamson Oct 27 '20 at 21:49

0 Answers0