Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 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 Reply

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...