I am trying to filter a dataframe using logical operators on the entire dataframe and somehow NA rows are creeping in the subsetted dataframe. I have gone through Subsetting R data frame results in mysterious NA rows and Subsetting R data frame results in mysterious NA rows but I am unable to find or reach at a solution.
df <- data.frame(number1 = c(1:5,-13,-2,-34,24,33), number2 = c(10:3, -73, -82))
df
df[df>=0 & !is.na(df$number2),]
sapply(names(df), function(x) df[x]>=0
First think you need is to reduce your matrix to a single vector. If you want to generalize this to any number of columns, you could do either
df[Reduce(`&`, lapply(df, `>=`, 0)), ]
# number1 number2
# 1 1 10
# 2 2 9
# 3 3 8
# 4 4 7
# 5 5 6
OR
df[rowSums(df >= 0) == ncol(df), ]
# number1 number2
# 1 1 10
# 2 2 9
# 3 3 8
# 4 4 7
# 5 5 6