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),]
I am trying to filter so that I have no negative values in any of the rows in my original data frame. I end up getting 18 rows df with multiple NA rows.
I tried using sapply on my df to check if the logical operation works fine. But if I wrap with "which" I get all 18 rows.
sapply(names(df), function(x) df[x]>=0)
My target is to get a df with no negative values in any of the columns.
EDIT: In my case I wouldnt know how many columns the resulting df would have before I filter them. So filter individually on columns with & operator is out of the question. That is exactly why I was trying to apply the logical operator or the entire df