Group data in one column based on a string value i

2019-10-17 16:59发布

问题:

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

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.

回答1:

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)



回答2:

Using @Colin's dataset along with map_df and spread we can provide more efficient solution.

library(dplyr)
df %>% 
   split(.$task) %>% 
   purrr::map_df(.%>%count(res), .id='task') %>% 
   tidyr::spread(res, n, fill = 0)

# A tibble: 4 x 4
  task   Completed `Not performed` Pending
  <chr>      <dbl>           <dbl>   <dbl>
1 Assign         3               1       0
2 Class          3               0       1
3 Home           1               1       2
4 Poster         3               0       1

Short and sweet answer from @Jaap

df %>% count(task, res) %>% spread(res, n, fill = 0)

PS: Dataset in copy-paste format "not as an image" will make it much easy for others to help you.



标签: r dplyr