1

I'm trying to use an if else statement and the filter function to clean some data and get rid of values that are too high.

This is an example of my tibble:

animals.data <- c("pig",99,"dog",30,"pig",20,"pig",200,"dog",120,"dog",40)

animals <- matrix(animals.data, nrow=6,ncol=2, byrow=TRUE)

colnames(animals) <- c("Variable","Value")

This is the code I'm trying to run:

animals <- animals %>% 
  filter(
    if (animals$Variable=="pig") {
      animals$Value < 100 
    } else {
      animals$Value < 140
    }
  )

This is the error I keep getting:

the condition has length > 1 and only the first element will be used

I'm wanting to iterate through both columns in the tibble. Thank you in advance! I also welcome if you also have any comments about bad practice things I'm doing, I'm a beginner.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • `filter()` is used to subset rows. If you want to change values, you should be using `mutate()`. You can't do both at once with `filter()` – MrFlick Oct 20 '21 at 19:00
  • After setting the colnames, then you need `as_tibble(animals)` to make it so it works with `mutate`/`filter`, etc. I am unable to guess what it is you're trying to do from your code. – Mossa Oct 20 '21 at 19:03
  • 1
    `animals %>% filter((Variable == "pig" & Value < 100) | (Variable != "pig" & Value < 140))` – Jon Spring Oct 20 '21 at 23:36
  • Or you could use `animals %>% mutate(threshold = if_else(Variable == "pig", 100, 140)) %>% filter(Value < threshold)` – Jon Spring Oct 20 '21 at 23:38
  • @JonSpring Thank you so much, this is exactly what I wanted – brown_squirrel Oct 23 '21 at 10:03

0 Answers0