我有一些许可数据,并试图在我的dataframe中创建一个列,该列根据某人注册的程序告诉我所列出的许可是否可以接受。
为了做到这一点,我创建了一个列表,因为其中一些许可对于多个程序是可以接受的。理想情况下,我想的是,我可以以某种方式使用这个列表作为参考,以查看程序是否在许可名称下列出。我也尝试过case_when,但是不断地出错。我也希望有一个列表,我可以使用作为一种地图,因为许可证名称可能会每年变化。
示例代码
以下是我的数据摘要:
df1 <- data.frame(Program = c("Elementary Education", "Elementary Education", "Secondary Math", "Secondary Math", "Secondary ELA", "Secondary ELA"), Licensure = c("Content Area - Elementary Education (Grades 1-6)", "Content Area - Secondary Math (Grades 7-12)", "Content Area - Secondary Math (Grades 7-12)", "Mathematics (Grades 7-12) 1706", "Content Area - Secondary ELA (Grades 7-12)", "Content Area - Early Childhood (preK-Grade 3)"))下面是我创建的列表,其中包含了所有许可,每个许可下面都有可接受的程序:
license_index <- list(
"Content Area - Early Childhood (preK-Grade 3)" = "Elementary Education",
"Content Area - Elementary Education (Grades 1-6)" = "Elementary Education",
"Content Area - Middle Grades ELA (Grades 4-9)" = c("Elementary Education", "Secondary ELA"),
"Content Area - Middle Grades Math (Grades 4-9)" = c("Elementary Education", "Secondary Math"),
"Content Area - Middle School Mathematics (Grades 4-8)" = "Elementary Education",
"Content Area - Secondary ELA (Grades 7-12)" = "Secondary ELA",
"Content Area - Secondary Math (Grades 7-12)" = "Secondary Math",
"Content Area - Secondary English (Grades 7-12)" = "Secondary ELA",
"English Language Arts and Reading (Grades 4-8) 864" = "Elementary Education",
"Core Subjects (Grades EC-6) 1770" = "Elementary Education",
"English Language Arts and Reading (Grades 7-12) 1709" = "Secondary ELA",
"Mathematics (Grades 4-8) 866" = "Elementary Education",
"Mathematics (Grades 7-12) 1706" = "Secondary Math"
)理想情况下,作为最后一栏,我想要的是许可和程序是否匹配:
ideal.df <- data.frame(Program = c("Elementary Education", "Elementary Education", "Secondary Math", "Secondary Math", "Secondary ELA", "Secondary ELA"), Licensure = c("Content Area - Elementary Education (Grades 1-6)", "Content Area - Secondary Math (Grades 7-12)", "Content Area - Secondary Math (Grades 7-12)", "Mathematics (Grades 7-12) 1706", "Content Area - Secondary ELA (Grades 7-12)", "Content Area - Early Childhood (preK-Grade 3)"), match = c("Match", "No", "Match", "Match", "Match", "No"))我想我需要变异函数,也许我需要使用purrr映射函数,但我对tidyverse不太熟悉,我真的很感激帮助!提前感谢!
发布于 2019-06-06 14:14:38
下面是使用tidyverse的一种方法,我们将命名的list转换为带有enframe的两列data.frame,用原始数据集将right_join转换为right_join,并通过将列“match”与“Program”进行比较来创建match
library(tidyverse)
enframe(license_index, name = "Licensure", value = "match") %>%
unnest %>%
right_join(df1) %>%
mutate(match = match == Program) %>%
select(names(df1), everything())
# A tibble: 6 x 3
# Program Licensure match
# <fct> <chr> <lgl>
#1 Elementary Education Content Area - Elementary Education (Grades 1-6) TRUE
#2 Elementary Education Content Area - Secondary Math (Grades 7-12) FALSE
#3 Secondary Math Content Area - Secondary Math (Grades 7-12) TRUE
#4 Secondary Math Mathematics (Grades 7-12) 1706 TRUE
#5 Secondary ELA Content Area - Secondary ELA (Grades 7-12) TRUE
#6 Secondary ELA Content Area - Early Childhood (preK-Grade 3) FALSE或者我们可以使用说唱包,这对这个场景很有帮助。
library(rap)
df1 %>%
rap(match = ~ license_index[[as.character(Licensure)]] == Program) %>%
unnest
# Program Licensure match
#1 Elementary Education Content Area - Elementary Education (Grades 1-6) TRUE
#2 Elementary Education Content Area - Secondary Math (Grades 7-12) FALSE
#3 Secondary Math Content Area - Secondary Math (Grades 7-12) TRUE
#4 Secondary Math Mathematics (Grades 7-12) 1706 TRUE
#5 Secondary ELA Content Area - Secondary ELA (Grades 7-12) TRUE
#6 Secondary ELA Content Area - Early Childhood (preK-Grade 3) FALSE发布于 2019-06-06 14:15:19
试试这个:
x<-stack(license_index)
x$values[match(df1$Licensure,x$ind)]==df1$Program
#[1] TRUE FALSE TRUE TRUE TRUE FALSE如果需要,可以使用TRUE和No映射上述值的Match和FALSE值。
发布于 2019-06-06 14:18:19
我真的帮不了你,但是这个基-R解决方案应该有效:
df1$match <-sapply(1:nrow(df1), function(i){
ifelse(license_index[[which(names(license_index) == df1$Licensure[i])]] == df1$Program[i],'Match','No')})https://stackoverflow.com/questions/56479272
复制相似问题