-2

So I have some data and in a column there are some values like this:

df <- matrix(c(1,0,3,1,0,0,99,2))

Now I want to delete the whole case if the value is bigger than 0 and 1 or not 0 and 1.

So I have:

1 0 3 1 0 0 99 2

And I want to delete the rows bigger than 0 and 1 This would then look like:

1 0 1 0 0

How would I do that?

nero7
  • 1
  • 3

2 Answers2

1

Do you mean by?

> df[df %in% c(0, 1)]
[1] 1 0 1 0 0
> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

This method will maintain the matrix class in your result (which is technically only a one-dimensional vector):

(df <- matrix(c(1,0,3,1,0,0,99,2)))
#      [,1]
# [1,]    1
# [2,]    0
# [3,]    3
# [4,]    1
# [5,]    0
# [6,]    0
# [7,]   99
# [8,]    2

(df <- as.matrix(df[df %in% c(0,1),]))
#      [,1]
# [1,]    1
# [2,]    0
# [3,]    1
# [4,]    0
# [5,]    0
Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • Yes I only made a vector, but my data consists of many columns. So I would like to do that for exmaple with the column df$age. How would I do that? – nero7 Nov 11 '21 at 09:36
  • Please add a [reproducible example](https://stackoverflow.com/q/5963269/1199289) – Marc in the box Nov 11 '21 at 11:57