Home > Net >  Count the number of filtered rows in data frame based on a condition
Count the number of filtered rows in data frame based on a condition

Time:03-31

I need to filter a dataframe based on each column of another dataframe (NON-inclusion of column check$tl6 in each column cc_comb$h5, cc_comb$h6, etc). Then I need to count the number of filtered rows for each one.

It should look like the below:

check$tl6 <- c(1011,1012,1013,1014,1014,1014)
cc_comb$h4 <- c(1011,1012)
cc_comb$h5 <- c(1011,1012,1014)
cc_comb$h6 <- c(1011,1013,1014)

for(i in 1:ncol(cc_comb)){  
lines <- 
   check %>% 
   filter(!check$tl6 %in% cc_comb[,i]) %>%
   summarise(n())
}   
   
#And I want the result to look like

hs <- c("hs4","hs5","hs6")
lines <- c(2,1,1)

hs_lines <- data.frame(hs,lines)

CodePudding user response:

In base R :

nrow(check[!check$tl6 %in% h5$HS2012, ])

CodePudding user response:

I would not advice using nrow in dplyr, the package has their own function namely n. Also, you will want to use the function summarize to summarize how many rows your data.frame is left with.

Try this:

check %>% 
  filter(!tl6 %in% h5$HS2012) %>%
  summarise(n())

Edit:

Alternative to use multiple filters on the same column (without using dplyr)

lapply(cc_comb, function(x) (sum(!check$tl6 %in% x)))
  • Related