creating summary of subset filtered dataframe

I am trying filtered data with value having 1 but the dataframe is already labelled . so the objective is to create a summary of filtered dataset

df <- data.frame(NY = c(1,2,1,1,2,1,1,1,2,1,1,1,2,1),
                 DE = c(2,2,1,1,1,2,2,1,1,1,2,2,2,1) )


df$NY<- factor(df$NY, levels =c(1,2), labels = c("available","unavailable"))
df$DE<- factor(df$DE, levels =c(1,2), labels = c("recieved","rejected"))


tbl <- function(df,col1,col2){

  tb <- df %>% filter(col1== "available" & col2 == "recieved") 
  tb <- table(df[[col1]],df[[col2]]) %>% as.data.frame()
  tb
  
}

output is the frequency of "available" in both column available/ total frequency in NY and DE for "recieved"

the output should be look like

enter image description here

your example data and your statement of your goal and your example img do not appear to agree ...
but maybe if you see a solution you will work to clarify your requirements.


tbl <- function(df, col1, col2) {
  df %>%
    summarise(
      c1_avail = sum({{ col1 }} == "available"),
      c2_avail = sum({{ col2 }} == "available"),
      c1_recie = sum({{ col1 }} == "recieved"),
      c2_recie = sum({{ col2 }} == "recieved")
    ) %>%
    mutate(ratio = (c1_avail + c2_avail) / (c1_recie + c2_recie))
}

tbl(df, NY, DE)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.