Nash Avi Nash Avi - 12 days ago 5
R Question

Issues filtering dataframe since NA rows creep in

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 columns in my original data frame. I end up getting 18 row 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

Answer

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
Comments