cat_88
cat_88

Reputation: 21

How to recode dataframe values to NA if row meet specific column criteria

I want to convert rows to "NA" if all the rows within specific columns meet a criteria, in this case the row within certain columns are "0" as well.

I have a data frame with hundreds of columns, but only interested in if a row in five columns are all "0", then I want that row in just those columns to be "NA".

At first i tried this line:

surveyclean$Col1[surveyclean$Col1 == 0 & 
                             surveyclean$Col2 == 0 &
                             surveyclean$Col3 == 0 & surveyclean$Col4 == 0 &
                             surveyclean$Col5 == 0] <-NA

It works for Col1, but when I try to the same for the next column, it doesnt work because Col1 is now "NA". What code can i use to make sure its done at the same time?

I'm working with only five columns of a larger data frame:

surveyclean


> Col1        Col2      Col3       Col4   Col5
> 0           0          0          0      0
> 1           0          0          0      0
> 0           0          0          0      0
> 0           1          0          0      0
> 0           0          0          0      0
> 0           1          0          0      0

What it should look like:

> Col1        Col2      Col3       Col4   Col5
> NA          NA         NA         NA     NA
> 1           0          0          0      0
> NA          NA         NA         NA     NA
> 0           1          0          0      0
> NA          NA         NA         NA     NA
> 0           1          0          0      0

Upvotes: 1

Views: 30

Answers (1)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8826

Here an approach using across and c_across, so that I can check rowwise and then apply to the columns.

Data

structure(list(Col1 = c(0L, 1L, 0L, 0L, 0L, 0L), Col2 = c(0L, 
0L, 0L, 1L, 0L, 1L), Col3 = c(0L, 0L, 0L, 0L, 0L, 0L), Col4 = c(0L, 
0L, 0L, 0L, 0L, 0L), Col5 = c(0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-6L))

Code

library(dplyr)

data %>% 
  rowwise() %>% 
  mutate(
    across(
      .cols = Col1:Col5,
      .fns = ~ifelse(all(c_across(cols = Col1:Col5) == 0),NA,.)
    )
  )  %>% 
  ungroup()

Output

# A tibble: 6 x 5
   Col1  Col2  Col3  Col4  Col5
  <dbl> <dbl> <dbl> <dbl> <dbl>
1    NA    NA    NA    NA    NA
2     1     0     0     0     0
3    NA    NA    NA    NA    NA
4     0     1     0     0     0
5    NA    NA    NA    NA    NA
6     0     1     0     0     0

Upvotes: 1

Related Questions