如何解决从课程列表中查找当前活动条目
我需要从课程列表中找到“当前”等效课程。该列表采用相当简单的格式,来自 SQL 查询,如下所示:
课程代码 | Course_Title | 课程状态 | Parent_Course |
---|---|---|---|
HLT31802 | 标题 1a | 被取代 | HLT31807 |
HLT31807 | 标题 1b | 被取代 | HLT31812 |
HLT31812 | Title1c | 被取代 | HLT35015 |
HLT35015 | 标题1d | 被取代 | HLT35021 |
HLT35021 | Title1e | 当前 | 无 |
ABC12345 | 标题 2a | 被取代 | ABC67890 |
ABC67890 | Title2b | 当前 | 无 |
我确定该解决方案与递归有关,但我无法理解。我很高兴发布我尝试过的代码,但我没有在 SQL 中创建多个列(child1、child2 等)。
所需的输出是这样的:
课程代码 | Current_Course |
---|---|
HLT31802 | HLT35021 |
HLT31807 | HLT35021 |
HLT31812 | HLT35021 |
HLT35015 | HLT35021 |
HLT35021 | HLT35021 |
ABC12345 | ABC67890 |
ABC67890 | ABC67890 |
任何帮助将不胜感激!
解决方法
如果您不想使用 course_title
列,您也可以这样做。
- 使用
igraph
为每个parent-child
创建单独的集群。 - 此后在我们的例子中使用了集群 ID
values
,用于 group_by 的实际数据。 - 为了对
parent-child
进行网络分析,我还必须从 parent 中删除None
,并在找到None
时将其替换为 child 本身。
希望其他语法很清楚。其他要求解释
df <- read.table(header = T,text = 'Course_Code Course_Title Course_Status Parent_Course
HLT31802 Title1a Superseded HLT31807
HLT31807 Title1b Superseded HLT31812
HLT31812 Title1c Superseded HLT35015
HLT35015 Title1d Superseded HLT35021
HLT35021 Title1e Current None
ABC12345 Title2a Superseded ABC67890
ABC67890 Title2b Current None')
library(tidyverse)
library(igraph)
df %>%
mutate(Parent_Course = ifelse(Parent_Course == 'None',Course_Code,Parent_Course)) %>%
select(1,4) %>%
graph.data.frame() %>%
components() %>%
pluck(membership) %>%
stack() %>%
right_join(df,by = c('ind' = 'Course_Code')) %>%
group_by(values) %>%
mutate(Parent_Course = ind[Course_Status == 'Current'],.keep = 'used') %>%
rename(Course_Code = ind)
#> # A tibble: 7 x 4
#> # Groups: values [2]
#> values Course_Code Course_Status Parent_Course
#> <dbl> <chr> <chr> <chr>
#> 1 1 HLT31802 Superseded HLT35021
#> 2 1 HLT31807 Superseded HLT35021
#> 3 1 HLT31812 Superseded HLT35021
#> 4 1 HLT35015 Superseded HLT35021
#> 5 1 HLT35021 Current HLT35021
#> 6 2 ABC12345 Superseded ABC67890
#> 7 2 ABC67890 Current ABC67890
由 reprex package (v2.0.0) 于 2021 年 6 月 30 日创建
,另一个igraph
选项
df %>%
mutate(Parent_Course = ifelse(Parent_Course == "None",Parent_Course)) %>%
select(Course_Code,Parent_Course,everything()) %>%
group_by(Grp = membership(components(graph_from_data_frame(.)))[Course_Code]) %>%
mutate(Parent_Course = Parent_Course[Course_Status == "Current"]) %>%
ungroup()
给予
Course_Code Parent_Course Course_Title Course_Status Grp
<chr> <chr> <chr> <chr> <dbl>
1 HLT31802 HLT35021 Title1a Superseded 1
2 HLT31807 HLT35021 Title1b Superseded 1
3 HLT31812 HLT35021 Title1c Superseded 1
4 HLT35015 HLT35021 Title1d Superseded 1
5 HLT35021 HLT35021 Title1e Current 1
6 ABC12345 ABC67890 Title2a Superseded 2
7 ABC67890 ABC67890 Title2b Current 2
,
通过使用包 stringr
和 dplyr
:
df <- data %>%
filter(Course_Status == "Current") %>%
mutate(Ety = str_sub(Course_Code,1,3)) %>%
select(-Course_Status) %>%
rename(Current_Course = Course_Code)
df2 <- data %>%
select(Course_Code) %>%
mutate(Ety = str_sub(Course_Code,3))
res <- left_join(df2,df,by = "Ety")
res <- res %>%
select(-Ety)
res
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。