首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的dataframe中创建一个列来映射到R中的列表?

如何在我的dataframe中创建一个列来映射到R中的列表?
EN

Stack Overflow用户
提问于 2019-06-06 14:04:41
回答 3查看 61关注 0票数 1

我有一些许可数据,并试图在我的dataframe中创建一个列,该列根据某人注册的程序告诉我所列出的许可是否可以接受。

为了做到这一点,我创建了一个列表,因为其中一些许可对于多个程序是可以接受的。理想情况下,我想的是,我可以以某种方式使用这个列表作为参考,以查看程序是否在许可名称下列出。我也尝试过case_when,但是不断地出错。我也希望有一个列表,我可以使用作为一种地图,因为许可证名称可能会每年变化。

示例代码

以下是我的数据摘要:

代码语言:javascript
复制
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)"))

下面是我创建的列表,其中包含了所有许可,每个许可下面都有可接受的程序:

代码语言:javascript
复制
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"
)

理想情况下,作为最后一栏,我想要的是许可和程序是否匹配:

代码语言:javascript
复制
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不太熟悉,我真的很感激帮助!提前感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-06 14:14:38

下面是使用tidyverse的一种方法,我们将命名的list转换为带有enframe的两列data.frame,用原始数据集将right_join转换为right_join,并通过将列“match”与“Program”进行比较来创建match

代码语言:javascript
复制
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

或者我们可以使用说唱包,这对这个场景很有帮助。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2019-06-06 14:15:19

试试这个:

代码语言:javascript
复制
x<-stack(license_index)
x$values[match(df1$Licensure,x$ind)]==df1$Program
#[1]  TRUE FALSE  TRUE  TRUE  TRUE FALSE

如果需要,可以使用TRUENo映射上述值的MatchFALSE值。

票数 4
EN

Stack Overflow用户

发布于 2019-06-06 14:18:19

我真的帮不了你,但是这个基-R解决方案应该有效:

代码语言:javascript
复制
df1$match <-sapply(1:nrow(df1), function(i){
  ifelse(license_index[[which(names(license_index) == df1$Licensure[i])]] == df1$Program[i],'Match','No')})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56479272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档