如何在 R 中同时使用计数函数和 case_when?
How to use a counting function and case_when simultaneously in R?
我有一个数据框,我想计算每个名称满足特定条件的观察次数:
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"))
在上面的示例中,学生选修的课程是 STEM(例如“生物”和“心理”)或文学(“英语”)。我想创建两个新列,一个显示他们的 STEM 分数,一个包含他们的文学分数,其中代码每次通过都会计为 1。
答案应该是这样的:
library(dplyr)
answer <- tibble(name = c("Justin", "Corey", "Sibley"),
stem_assessment = c(0, 2, 2),
lit_assessment = c(0, 0, 1))
我尝试过 case_when()
、group_by()
、n()
、summarize()
和 count()
,但我似乎无法破解它。
您可以组合使用 count()
和 pivot_longer/pivot_wider
。我还在 test
.
中编码了错别字 (Pysch)
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)
group_by
和 summarise
应该做 -
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
我有一个数据框,我想计算每个名称满足特定条件的观察次数:
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"))
在上面的示例中,学生选修的课程是 STEM(例如“生物”和“心理”)或文学(“英语”)。我想创建两个新列,一个显示他们的 STEM 分数,一个包含他们的文学分数,其中代码每次通过都会计为 1。
答案应该是这样的:
library(dplyr)
answer <- tibble(name = c("Justin", "Corey", "Sibley"),
stem_assessment = c(0, 2, 2),
lit_assessment = c(0, 0, 1))
我尝试过 case_when()
、group_by()
、n()
、summarize()
和 count()
,但我似乎无法破解它。
您可以组合使用 count()
和 pivot_longer/pivot_wider
。我还在 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)
group_by
和 summarise
应该做 -
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