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

R: Remove rows if no follow-up value is found in specific range of columns

I’m trying to remove rows (subjects) of my dataframe in R, that do not have a value within a set of columns.

Subject     Baseline    Week 1      Week 2      Week 3        Week 4 
  1           50         51          50          51             51
  2           75         77          NA          NA             76
  3           66         NA          NA          NA             NA

So that in this example, Subject 3 is removed form the dataframe and Subject 2 remains, because it has as least one cell with value over the columns Week1:Week4

With dataframe[complete.cases(dataframe[,3:6]),] all rows with any NA are removed, which is not the solution.

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

>Solution :

You can use rowSums:

dataframe[rowSums(is.na(dataframe[3:6])) != 4,]

  Subject Baseline Week.1 Week.2 Week.3 Week.4
1       1       50     51     50     51     51
2       2       75     77     NA     NA     76

Or with apply + all + is.na:

dataframe[apply(dataframe[3:6], 1, \(x) !all(is.na(x))), ]
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