Help Deleting value in a list greater than 10

I am constructing a program where I need a to construct a vector of values. These values are constructed from other vectors. A particular vector M has thousands of values, but I need to delete/create a vector that contains all those values below 10. It should be noted that there is values missing (NA) with in M. I have tried everything I can think of.

Examples of codes I have tried:

  1. if (M<10,D==M) , where D is an empty vector

  2. if (M<10 || M%in%NA) {D==M}

I have tried others, such as the use of list.append. I am absolutely stuck.

Like this?

M <- sample(1:20,size = 20, replace = TRUE)
M
#>  [1] 16 16 12  3 13  6  9 19 11  2  7 18  8 20 17  9 10  3  3 19
D <- M[M < 10]
D
#> [1] 3 6 9 2 7 8 9 3 3

Created on 2020-02-07 by the reprex package (v0.3.0)

M < 10 returns 20 TRUE/FALSE values and only the elements where the value is TRUE are kept.

1 Like

Thank You!!!!! I was making that so much harder than it was. Just so I understand the code. What exactly is M[M<10] doing? If I am correct it is selecting all the index's values which are less than 10, correct?

The comparison M < 10 simply returns TRUE and FALSE values. When used as the indexes to subset a vector, TRUE mean "keep" and FALSE means "don't keep". Here is some code that might help explain that. Notice that the M vector here is not the same as in my previous post.

M <- sample(1:20,size = 20, replace = TRUE)
M
#>  [1] 17 16 18  1 17 10 18 20  8  5  7  7  4 18  1  5 20  4 13 20
M < 10
#>  [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
#> [12]  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
Idx <- M < 10
M[Idx]
#> [1] 1 8 5 7 7 4 1 5 4
M[M < 10]
#> [1] 1 8 5 7 7 4 1 5 4

Created on 2020-02-08 by the reprex package (v0.2.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.