0

I have a df with a columns date and observations. I want to delete all observations before the 2020-01-01. I decided to use while loop. Interestingly sometimes it works. But whenever I put it into function it does not work, returning "argument is of length zero"

subset1 <- data.frame("date"= c("2019-12-31",  "2020-01-01", "2020-01-02", "2020-01-03"), "observations" = c(0,0,12,10))
while(subset1$date < "2020-01-01") {
    subset1 = subset1[-1,]
}

What may be wrong?

Help is appreciated!!!

  • 2
    It is better to show a small reproducible example with expected output. thanks – akrun May 25 '20 at 18:35
  • 1
    While it's mostly style and aesthetics, it appears that you tried to format as code but mis-read https://stackoverflow.com/editing-help. Code "fences" are at a minimum three backticks *on a line of their own*, as in `\`\`\``. One can optionally include a language specification on the opening code fence, which for R would be that the complete line is `\`\`\`lang-r`, and then the closing fence is just `\`\`\``. (This is as opposed to the single-quotes that were originally in your question.) Thanks! – r2evans May 25 '20 at 18:37
  • My guess is that `subset1$date` resolves to `NULL` because `date` is not a present column name. I'd check spelling and case, since variables (and therefore column names) are case-sensitive. – r2evans May 25 '20 at 18:39
  • 1
    Thanks for the help r2evans! – Tigran Danielyan May 25 '20 at 18:39
  • I have checked all the names everything is ok. Initially I formatted date as POSIXct. I thought it may be the cause. However, even after removing that line it keeps returning the same problem message. – Tigran Danielyan May 25 '20 at 18:42
  • There are a lot of questions (and answers) on SO that include *"argument is of length zero"*, and they pretty much all point back to the condition returning something like `logical(0)`, `integer(0)`, or `NULL`. In this case, it could also be one of two things(1) `nrow(subset1)` returns 0, indicating that there are no rows to process; or (2) you will have a problem if `subset1` has more than 1 row, getting `the condition has length > 1 and only the first element will be used` instead since your comparison will return a `logical `of each row. – r2evans May 25 '20 at 18:43
  • 1
    Regardless, for us to know any better, you'll need to make this a minimal but self-contained and reproducible question, including sample data (`dput(head(subset1))` or `data.frame(...)`) and just enough code (including non-base packages) for us to be able to see more context and possibly test on our own R consoles. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans May 25 '20 at 18:44
  • I have just edited the code. This is a small part of my data but the logic is the same. I have a column with unique dates and observations. Thank you for informing about the rules... it's my first day here :D – Tigran Danielyan May 25 '20 at 18:53
  • Related: [R: How to filter/subset a sequence of dates](https://stackoverflow.com/questions/28335715/r-how-to-filter-subset-a-sequence-of-dates) (no need for `while`, if I understood you correctly). – Henrik May 25 '20 at 19:07
  • (1) Your sample code generates other warnings and errors, not what you list here. (2) Your code is still problematic in that `while` expects a conditional of length 1, and your code will return a vector of length 4 (one for each row of the frame). (3) Your sample `"date"` is `factor`, and `'<' not meaningful for factors`, so you'll get `NA` values from the comparison. (Again, a different error.) I suggest what you want is `subset1[as.character(subset1) < "2020-01-01",]`.) – r2evans May 25 '20 at 21:20

0 Answers0