gpt4 book ai didi

r - 从类(class)列表中查找当前事件条目

转载 作者:行者123 更新时间:2023-12-05 09:32:18 25 4
gpt4 key购买 nike

我需要从类(class)列表中找到“当前”等效类(class)。该列表采用相当简单的格式,来自 SQL 查询,如下所示:

<表类="s-表"><头>类(class)代码类(class)名称类(class)状态家长类(class)<正文>HLT31802Title1a已被取代HLT31807HLT31807Title1b已被取代HLT31812HLT31812Title1c已被取代HLT35015HLT35015标题1d已被取代HLT35021HLT35021标题1e当前无ABC12345Title2a已被取代ABC67890ABC67890Title2b当前无

我确定解决方案与递归有关,但我无法理解它。我很高兴发布我尝试过的代码,但如果不在 SQL 中创建多个列(child1、child2 等),我并没有走得太远。

要求的输出应该是这样的:

<表类="s-表"><头>类(class)代码当前类(class)<正文>HLT31802HLT35021HLT31807HLT35021HLT31812HLT35021HLT35015HLT35021HLT35021HLT35021ABC12345ABC67890ABC67890ABC67890

如有任何帮助,我们将不胜感激!

最佳答案

如果您不想使用 course_title 列,您也可以这样做。

  • 使用 igraph 为每个 parent-child 创建一个单独的集群。
  • 此后在我们的案例中使用了 cluster-id values,用于实际数据的 group_by。
  • 为了对parent-child 进行网络分析,我还必须从parent 中删除None,并在NoneNone 时将其替换为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 创建于 2021-06-30 (v2.0.0)

关于r - 从类(class)列表中查找当前事件条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68189016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com