Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete rows with na in certain columns, only if dummy = 0

I want the following statement:

If df$dummy=0 –> delete all rows with na values in column 2:5.

I try

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df[df$dummy==0] <- na.omit(df[2:5],)

But it does not function properly.

anyone that can help me?

>Solution :

It’s always better to include a little reproducible example, otherwise the folks here who answer your question will need to do it for you.

Suppose your data frame looks like this:

df <- data.frame(dummy = c(0,  1,  1, 0, 0, 0,  1, 1, 0, 0),
                 col2  = c(1,  NA, 3, 4, 5, NA, 7, 8, 9, 10),
                 col3  = c(1,  2,  3, 4, 5, 6,  7, 8, 9, NA),
                 col4  = c(NA, 2,  3, 4, 5, 6,  7, 8, 9, 10),
                 col5  = c(1,  2,  3, 4, 5, 6,  7, 8, 9, 10))
df
#>    dummy col2 col3 col4 col5
#> 1      0    1    1   NA    1
#> 2      1   NA    2    2    2
#> 3      1    3    3    3    3
#> 4      0    4    4    4    4
#> 5      0    5    5    5    5
#> 6      0   NA    6    6    6
#> 7      1    7    7    7    7
#> 8      1    8    8    8    8
#> 9      0    9    9    9    9
#> 10     0   10   NA   10   10

Then you can filter out the columns where dummy == 0 AND where any row in columns 2:5 have NA by doing:

df[-which(df$dummy == 0 & apply(df[2:5], 1, anyNA)), ]
#>   dummy col2 col3 col4 col5
#> 2     1   NA    2    2    2
#> 3     1    3    3    3    3
#> 4     0    4    4    4    4
#> 5     0    5    5    5    5
#> 7     1    7    7    7    7
#> 8     1    8    8    8    8
#> 9     0    9    9    9    9

You will see that the only NA that remains occurs in a row where dummy == 1, as expected.

Created on 2021-11-12 by the reprex package (v2.0.0)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading