R remove rows containing a certain value
dataframe, r
Solution
Your most efficient way will be to use the `na.strings` argument of `read.csv()` to code all `-1` values as `NA`, then to drop incomplete cases.
Step 1: set `na.strings=-1` in `read.csv()`:
x <- read.csv(text="
clientx,clienty,screenx,screeny
481,855,481,847
481,784,481,847
481,784,481,847
-1,292,879,355", header=TRUE, na.strings=-1)
x
clientx clienty screenx screeny
1 481 855 481 847
2 481 784 481 847
3 481 784 481 847
4 NA 292 879 355
Step 2: Now use `complete.cases` or `na.omit`:
x[complete.cases(x), ]
clientx clienty screenx screeny
1 481 855 481 847
2 481 784 481 847
3 481 784 481 847
na.omit(x)
clientx clienty screenx screeny
1 481 855 481 847
2 481 784 481 847
3 481 784 481 847
Problem
So it got a csv I'm reading into an R dataframe, it looks like this ``` clientx,clienty,screenx,screeny 481,855,481,847 481,784,481,847 481,784,481,847 879,292,879,355 ``` First line is of course the header. So we have 4 columns with numeric data in it, ranging from 1 to 4 digits. There are no negative numbers in the set except -1 which marks a missing value. I want to remove every row that contains a -1 in any of the 4 columns. Thanks in advance for the help