Replace specific column values with NA

Asked

Viewed 3,207 times

1

I would like to detect and replace values above 6 in just a few columns of a data.frame by NA. I made the syntax like this, but it is giving error... Could someone give me a hand? Thank you!

data <- apply(dados[14:128], 2, function(x) {x[x > 6] <- NA; x})
  • 1

    I was able to resolve using date[,14:128][date[,14:128] > 6] <-NA

2 answers

2

Danilo, it’s all right?

I suggest we do it this way:

Creating a data frame to serve as an example:

a <- c(1,2,3,4,5,6,7,8,9,10)
b <- c(10,9,8,7,6,5,4,3,2,1)

df <- data.frame(a,b)

> print(df)
    a  b
1   1 10
2   2  9
3   3  8
4   4  7
5   5  6
6   6  5
7   7  4
8   8  3
9   9  2
10 10  1

One way to obtain only values greater than 6 of column b is as follows::

df$b[df$b > 6]

So, just take advantage of the same idea and directly assign the desired value, which in your case is the NA:

df$b[df$b > 6] <- NA

> print(df)
    a  b
1   1 NA
2   2 NA
3   3 NA
4   4 NA
5   5  6
6   6  5
7   7  4
8   8  3
9   9  2
10 10  1

1

In addition to your solution in comment, which is completely vectorized, there is another vector that I believe is more readable.

First an example dataset.

set.seed(5139)    # Torna os resultados reprodutíveis
dados <- as.data.frame(matrix(sample(20, 180, TRUE), 10))

Now such a solution. The base function R to use is the function is.na<-. This function assigns the value NA the data corresponding to the indexes on the right hand side of the function (which are one of its arguments). These indexes can be logical or numerical. In this case they are logical indices.

is.na(dados[12:18]) <- dados[12:18] > 6

Easy, that’s all it is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.