[Solved] Group data in one column based on a string value in another column using dplyr


You can filter the data based on the column, then do the count for task :

df <- data.frame(
  student = c(
    rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4)
  ), 
  task = rep(
    c("Home", "Class", "Assign", "Poster"), 4
  ), 
  res = sample(
    c("Completed", "Pending", "Not performed"), 
    16, TRUE
  )
) 

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df %>% 
  filter(res == "Completed") %>%
  count(task)
#> # A tibble: 4 x 2
#>   task       n
#>   <fct>  <int>
#> 1 Assign     1
#> 2 Class      1
#> 3 Home       1
#> 4 Poster     3

Created on 2019-09-29 by the reprex package (v0.3.0)

solved Group data in one column based on a string value in another column using dplyr