R deletes rows containing a specific value

So, he got the csv, which I read in the R framework, it looks like

clientx,clienty,screenx,screeny 481,855,481,847 481,784,481,847 481,784,481,847 879,292,879,355 

The first line is, of course, the title. Thus, we have 4 columns with numerical data in it from 1 to 4 digits. There are no negative numbers in the set, except -1, which means the missing value. I want to delete every row containing -1 in any of the 4 columns.

Thanks in advance for your help

+4
source share
2 answers

The most efficient way would be to use the na.strings read.csv() argument to encode all -1 values ​​as NA , and then to read.csv() 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 

+9
source

Straight way:

 df <- df[!apply(df, 1, function(x) {any(x == -1)}),] 

UPDATE: this approach will fail if data.frame contains character columns, because apply implicitly converts data.frame to a matrix (which contains data of only one type), and the character type takes precedence over numeric types, so data.frame will be converted to character matrix.

Or replace -1 with NA , and then use na.omit :

 df[df==-1] <- NA df <- na.omit(df) 

They should work, I did not check. Please always try to provide a reproducible example to illustrate your question.

+8
source

Source: https://habr.com/ru/post/1446948/


All Articles