Home > OS >  My dataframe i changing when dropping observations conditionaly
My dataframe i changing when dropping observations conditionaly

Time:06-13

I have a df : 262 obs. of 69 variabels

one variabel, table(data$ex, useNA = "ifany")

0 1 2 3 NA

33 9 25 4 191

I want to drop all obs if data$ex == 0

I tryed different ways

        >   data <- data[data2$ex !=0, ]

I tryed the subset function

But every time alot of the dataframe changes to NA

How can I drop observation / rows (data$ex == 0) and not change anythin else in the dataframe?

Thanks, F

CodePudding user response:

This is a very basic use of subset.

data |> subset(ex != 0 | is.na(ex))

Output:

#>    ex
#> 1   2
#> 2   2
#> 3  NA
#> 4  NA
#> 6   1
#> 8  NA
#> 9   2
#> 10  3

PS - create reproducible example

data <- structure(list(ex = c(2, 2, NA, NA, 0, 1, 0, NA, 2, 3)),
                  class = "data.frame", row.names = c(NA, -10L))

data
#>    ex
#> 1   2
#> 2   2
#> 3  NA
#> 4  NA
#> 5   0
#> 6   1
#> 7   0
#> 8  NA
#> 9   2
#> 10  3

Created on 2022-06-13 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related