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.
>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))), ]