问题描述
我有一个扁平的层次结构,深约10层。有一个钥匙,但是很遗憾,这个钥匙没有被遵守,所以不能用于我的目的。
我需要把这张桌子放到一个很宽的层次结构中,我唯一的关系是行ID和父ID。
我的数据看起来像这样(简化);
id name description code sub_level_name parent_id
1 Parent Company My Big Company MBC Company
2 Franchise1 My Franchise1 MF1 Franchise 1
3 Store1 My Store1 MS1 Store 2
4 Store2 My Store2 MS2 Store 2
5 Store1Owner My Store1Owner MSO1 Store Owner 3
6 Store2Owner My Store2Owner MSO2 Store Owner 4
我希望结果看起来像
company_name company_description company_code franchise_name franchise_description franchise_code store_name store_description store_code storeowner_name storeowner_description storeowner_code
Parent Company My Big Company MBC Franchise1 My Franchise1 MF1 Store1 My Store1 MS1 Store1Owner My Store1Owner MSO1
Parent Company My Big Company MBC Franchise1 My Franchise1 MF1 Store2 My Store2 MS2 Store2Owner My Store2Owner MSO2
通常我会使用pivot_wider并使用sub_level_name列,但这是我所说的在较大范围内未遵循的内容(该表有7000行,并且可以深10行)
我觉得我需要做的是将父ID与ID匹配,并根据其sub_level_name将各列连接起来,以创建“ x.name,x.description,x.code”列的类型,然后执行逐行,直到达到层次结构的顶层。
非常有用的帮助-不必是R解决方案,SQL解决方案也很好。
编辑:Dput源数据
structure(list(id = c(1,2,3,4,5,6),name = c("Parent Company","Franchise1","Store1","Store2","Store1Owner","Store2Owner"
),description = c("My Big Company","My Franchise1","My Store1","My Store2","My Store1Owner","My Store2Owner"),code = c("MBC","MF1","MS1","MS2","MSO1","MSO2"),sub_level_name = c("Company","Franchise","Store","Store Owner","Store Owner"),parent_id = c(NA,1,4)),class = c("spec_tbl_df","tbl_df","tbl","data.frame"),row.names = c(NA,-6L),spec = structure(list(
cols = list(id = structure(list(),class = c("collector_double","collector")),name = structure(list(),class = c("collector_character",description = structure(list(),code = structure(list(),sub_level_name = structure(list(),parent_id = structure(list(),"collector"))),default = structure(list(),class = c("collector_guess",skip = 1),class = "col_spec"))
解决方法
您可以尝试为每个id
创建一个sub_level_name
列,删除不需要的列并获取宽格式的数据。
library(dplyr)
df %>%
group_by(sub_level_name) %>%
mutate(id = row_number()) %>%
select(-parent_id) %>%
tidyr::pivot_wider(names_from = sub_level_name,values_from = c(name,description,code))