0

I am removing rows with certain values with

dat14a <- dat14[dat14${MYVARIABLE}<80, ]

dat14 looks like this

dat14

However, after using a/m code dat14a is also affected in other colums showing then the following

dat14a

How can I avoid that? Thanks!!

r2evans
  • 141,215
  • 6
  • 77
  • 149
Simon
  • 1
  • 1
  • Welcome to Stack Overflow. Please don’t use images of data as it cannot be used without a lot of unnecessary effort. It’s really helpful if your question is reproducible. [Guidance on asking questions and how to include data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Paste the output of `dput(dat14)` and `dput(dat14a)` into the question. – Peter Sep 20 '21 at 19:48
  • 1
    Does that code actually work without error? I tried including curly-braces such as `mtcars[mtcars${cyl} < 5,]` and it produces an error for me. Perhaps you are mixing tidyverse `{varname}` use with base R? It doesn't work that way, perhaps you want `dat14a <- dat14[dat14[[MYVARIABLE]]<80, ]`. – r2evans Sep 20 '21 at 19:55
  • @r2evans Sorry, the {} was just a placeholder, the code says dat14a<-dat14[dat14$MYVARIAVLE<80, ]. Unfortunately, your suggestion does not work either. – Simon Sep 21 '21 at 12:04
  • (1) Please do not post an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. (2) If your code has a known typo in it, you need to [edit] your question and fix it. It can be difficult parsing through typos to find real issues. (3) Sample data would be nice, this question is not [reproducible](https://stackoverflow.com/q/5963269). – r2evans Sep 21 '21 at 13:37

1 Answers1

1

That is most likely because you have NA's in MYVARIABLE column.

Using a reproducible example from mtcars.

df <- mtcars[1:5, 1:5]
df$mpg[1:2] <- NA
df

#                   mpg cyl disp  hp drat
#Mazda RX4           NA   6  160 110 3.90
#Mazda RX4 Wag       NA   6  160 110 3.90
#Datsun 710        22.8   4  108  93 3.85
#Hornet 4 Drive    21.4   6  258 110 3.08
#Hornet Sportabout 18.7   8  360 175 3.15

df[df$mpg > 22, ]

#            mpg cyl disp hp drat
#NA           NA  NA   NA NA   NA
#NA.1         NA  NA   NA NA   NA
#Datsun 710 22.8   4  108 93 3.85

To fix the issue use an additional !is.na(..)

df[df$mpg > 22 & !is.na(df$mpg), ]

#            mpg cyl disp hp drat
#Datsun 710 22.8   4  108 93 3.85
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213