Reputation: 2536
I have a dataframe, and I want to count the number of observations for each name that meet a certain criteria:
library(dplyr)
test <- tibble(name = c("Justin", "Corey", "Sibley", "Justin", "Corey", "Sibley", "Justin", "Corey", "Sibley"),
class = c("Bio", "Bio", "Bio", "Psych", "Pysch", "Psych", "English", "English", "English"),
result = c("Fail", "Pass", "Pass", "Fail", "Pass", "Pass", "Fail", "Fail", "Pass"))
In the above example, the courses the students took are either STEM (e.g., "Bio" & "Psych") or literature ("English"). I want to make two new columns, one that says their STEM score, and one that contains their literature score, where the code will count 1 for each pass.
The answer should look like:
library(dplyr)
answer <- tibble(name = c("Justin", "Corey", "Sibley"),
stem_assessment = c(0, 2, 2),
lit_assessment = c(0, 0, 1))
I've tried experimenting with case_when()
, group_by()
, n()
, summarize()
, and count()
, but I can't seem to crack it.
Upvotes: 0
Views: 888
Reputation: 389135
group_by
and summarise
should do -
library(dplyr)
stem <- c("Bio", "Pysch", "Psych")
lit <- c('English')
test %>%
group_by(name) %>%
summarise(stem_assessment = sum(class %in% stem & result == "Pass"),
lit_assessment = sum(class %in% lit & result == "Pass"))
# name stem_assessment lit_assessment
# <chr> <int> <int>
#1 Corey 2 0
#2 Justin 0 0
#3 Sibley 2 1
Upvotes: 2
Reputation: 1474
You can use a combination of count()
and pivot_longer/pivot_wider
. I also coded the typo (Pysch) in test
.
library(tidyverse)
test %>%
mutate(
stem_assessment = class %in% c("Bio", "Psych", "Pysch") & result == "Pass",
lit_assessment = class == "English" & result == "Pass"
) %>%
select(-c(class, result)) %>%
pivot_longer(-name, names_to = "var", values_to = "value") %>%
count(name, var, wt = value) %>%
pivot_wider(name, names_from = var, values_from = n)
Upvotes: 1