Using R 3.1.2, dplyr 0.4.0.
I try to use filterinside filter, which sounds very simple, and I don’t understand why it does not give me the expected result. This is the code that I wrote about 6 months ago, and I'm sure it worked, so either it stops working due to an updated version of R, dplyror some other dependency. Anyway, here is some simple code that filters the rows from df1 based on the condition that is filterin the column in df2.
df1 <- data.frame(x = c("A", "B"), stringsAsFactors = FALSE)
df2 <- data.frame(x = "A", y = TRUE, stringsAsFactors = FALSE)
dplyr::filter(df1, x %in% (dplyr::filter(df2, y)$x))
I expect this to show the first line df1, but instead I get
# [1] x
# <0 rows> (or 0-length row.names)
which i'm not sure what to do. Why does it return a vector AND an empty data.frame?
If I break the filter code into two separate statements, I get what I expect
xval <- dplyr::filter(df2, y)$x
dplyr::filter(df1, x %in% xval)
Can someone help me understand why this behavior occurs? I am not talking about this, but I do not understand this.
source
share