Home > Net >  How to remove rows having a specific entry value from a data table in R
How to remove rows having a specific entry value from a data table in R

Time:09-11

To clearly state my question, consider the following table:

A   B   C  D 
12  3  -1  2
421 4  12  13
11  4  -1  55
4   36 44  18

I have a data set whose entries must be positive and I also have all of the missing value being labelled as -1. The missing values in my data set are only located in column C.

Question: How can I remove all the rows in this example table which contain the value -1 in R?

What I expect after this operation is to be left with rows 2 and 4 only.

CodePudding user response:

either

subset(df1, C >0) 

as suggested or

dataset[dataset$C >0,]

CodePudding user response:

You could also use filter from dplyr like this:

df <- read.table(text = "A   B   C  D 
12  3  -1  2
421 4  12  13
11  4  -1  55
4   36 44  18", header = TRUE)

library(dplyr)
filter(df, C > 0)
#>     A  B  C  D
#> 1 421  4 12 13
#> 2   4 36 44 18

Created on 2022-09-10 with reprex v2.0.2

  • Related