-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_tree.R
More file actions
28 lines (21 loc) · 791 Bytes
/
csv_tree.R
File metadata and controls
28 lines (21 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
library(dplyr)
library(jsonlite)
# Read the CSV file
df <- read.csv("[PATH_TO_CSV_FILE]/structure_tree_safe_2017.csv")
result <- df %>%
group_by(parent_structure_id) %>%
reframe(child_id = paste(sort((id)), collapse = ", ")
) %>%
unique()
tree_csv <- as.data.frame(result)
# Group by 'parent_structure_id' and concat 'id' values
children_df <- df %>%
group_by(parent_structure_id) %>%
summarise(children = paste(sort(id), collapse = ", "))
# Merge back with the original dataframe to add the children information
result <- df %>%
left_join(children_df, by = c("id" = "parent_structure_id"))
# Select only the relevant columns: 'id', 'parent_structure_id', and 'children'
result <- result %>%
select(id, parent_structure_id, children)
tree_csv <- as.data.frame(result)