Replace specific column values by NA

1

I would like to detect and replace values above 6 in just a few columns of a data.frame by NA. I did the syntax like this but it is giving error ... Could anyone give me a help? Thanks!

data <- apply(dados[14:128], 2, function(x) {x[x > 6] <- NA; x})
    
asked by anonymous 05.06.2018 / 22:56

2 answers

2

Danilo, how are you?

I suggest doing the following:

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 get only values greater than 6 from column b is as follows:

df$b[df$b > 6]

So, just take the same idea and directly assign the desired value, which in your case is 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
    
06.06.2018 / 02:10
1

In addition to its solution in commentary, which is completely vectorized, there is another one also vectorized which I believe to be 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 to such a solution. The base R function to use is the is.na<- function. This function assigns the NA value to the data corresponding to the indexes on the right side of the function (which are one of its arguments). These indexes can be logical or numeric. In this case they are logical indexes.

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

Easy, that's all.

    
06.06.2018 / 12:08