Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

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

I have the below data in a spreadsheet where the tasks assigned for the students are listed.

df <- data.frame(
Student=c("A","A","A","A","B","B","B","C","D","D","D","D"),
Task=c("Homework","Classwork","Assignment","Poster","Poster","Homework","Assignment","Homework","Classwork","Homework","Assignment","Poster"),
Status=c("Completed","Pending","Not performed","Not performed","Completed","Not performed","Not performed","Completed","Completed","Pending","Pending","Pending"), 
stringsAsFactors = FALSE)

I would like to group the data at task level and find the count for each task based on 'Status' being 'Completed'. Below is my expected output

Output

I used the below snippet but it does not seem to work. Any help is appreciated.

df %>% group_by(Task)  %>% 
         summarize(
             Count = nrow(df[df$Status == 'Completed',])
         ) 

Edit: Updated the question to add the actual dataset instead of a screenshot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...