Reputation: 183
I have the following DataFrame:
df <- data.frame(
"DateValue" = c("2016-07-01", "2016-07-02", "2016-07-03", "2016-07-04", "2016-07-05", "2016-07-06","2016-07-07"),
"Age1" = c(rep(NA, 2), seq(14,18,2), NA, NA),
"Age2" = c(rep(NA, 2), seq(14,22,2))
)
I am trying to delete all rows that contain NAs in all columns that begin with the string "Age". However, to not delete the rows where even one of the "age" column has a non missing value. The output I am aiming for is the following:
> df
DateValue Age1 Age2
1 2016-07-03 14 14
2 2016-07-04 16 16
3 2016-07-05 18 18
4 2016-07-06 NA 20
5 2016-07-07 NA 22
I am trying the below R script to achieve the same but the NAs don't drop:
df <- df[!(is.na(grep("^Age", names(df)))), ]
Any guidance would be appreciated.
Upvotes: 1
Views: 62
Reputation: 886948
We can use rowSums
to create a logical vector and subset the rows based on that
df[rowSums(!is.na(df[grep("^Age", names(df))])) > 0, ]
Upvotes: 2