1

I am having trouble building a tree.map from parent/child grouped pairs. Here's my sample data:

SubjectID <- c('101','101','101','102','103','103','103')
parent <- c(1387, 1620, 1743,986,1623,1191,1450)
child <- c(1620,1743,1859 ,1015,1385,1450,1623)
df <- data.frame(SubjectID, parent,child)

I've tried building the tree with tree.map:

df$pathString <- paste("study",df$SubjectID, df$parent, df$child, sep="/")
as.Node(df)

The result is:

1  study           
2   ¦--101         
3   ¦   ¦--1387    
4   ¦   ¦   °--1620
5   ¦   ¦--1620    
6   ¦   ¦   °--1743
7   ¦   °--1743    
8   ¦       °--1859
9   ¦--102         
10  ¦   °--986     
11  ¦       °--1015
12  °--103         
13      ¦--1623    
14      ¦   °--1385
15      ¦--1191    
16      ¦   °--1450
17      °--1450    
18          °--1623

I would like the result to link the parent with child like this:

1  study           
2   ¦--101         
3   ¦   ¦--1387    
4   ¦   ¦   °--1620
5   ¦   ¦      °--1743
6   ¦   ¦         °--1859
9   ¦--102         
7   ¦   °--986     
8   ¦       °--1015
9   °--103         
10      ¦--1623    
11      ¦   °--1385
12      ¦--1191    
13      ¦   °--1450
14      ¦      °--1623
Eric Leung
  • 2,354
  • 12
  • 25
user2909302
  • 103
  • 8

1 Answers1

0

The blow codes work and gives exactly the same output.

library(data.tree)

SubjectID <- c('101','102','103','103')
parent <- c(1387, 986,1623,1191)
child1 <-c(1620,1015,1385,1450)
child2 <- c(1743,'','',1623)
child3 <- c(1859,'','','')
df<-data.frame(SubjectID, parent,child1,child2,child3)

df$pathString<- paste("study",df$SubjectID, df$parent, df$child1,df$child2,df$child3, sep="/") 

as.Node(df)

enter image description here

Update: An update based on your comments to include data transformation. There might be a better way to transform the data. If conditions change, the code can be updated accordingly.

SubjectID <- c('101','101','101','102','103','103','103')
parent <-    c(1387,  1620, 1743, 986,  1623, 1191, 1450)
child<-      c(1620,  1743, 1859 ,1015, 1385, 1450, 1623)

nchild <- 10
nc <- 1
child_new <- rep(list(c()),nchild)
SID <- SubjectID[nc]
parentn <- parent[nc]
child_new[[nc]] <- child[nc]

for (s in 2:length(SubjectID)) {

  if (SubjectID[s]!=SubjectID[s-1]){
    nc <- 1
    SID <- c(SID,SubjectID[s])
    parentn <- c(parentn,parent[s])
    child_new[[1]] <- c(child_new[[1]],child[s])
    for (ic in 2:length(child_new))  child_new[[ic]] <- c(child_new[[ic]],"")
  } else {
    if(parent[s]==child[s-1]) {
      nc <- nc+1
      child_new[[nc]] <- c(child_new[[nc]],child[s])
    }
    if(parent[s]!=child[s-1]) {
      SID <- c(SID,SubjectID[s])
      parentn <- c(parentn,parent[s])
      child_new[[nc]] <- c(child_new[[nc]],child[s])
    }
  }
}

for (i in 1:length(child_new)){
  len_child <- length(child_new[[i]])
  if (len_child<length(SID)) child_new[[i]] <- c(child_new[[i]],rep("",length(SID)-len_child)) 
}

library(data.tree)
df <- data.frame(t(matrix(unlist(child_new), nrow=nchild, byrow=T)))
df <- cbind(SID, parentn, df)
df$pathString<- paste("study",do.call(paste, c(df[colnames(df)], sep="/")),sep="/") 

as.Node(df)
Sam S.
  • 627
  • 1
  • 7
  • 23
  • Thanks @SamS. Any suggestions on an efficient way to reformat my data to match your structure? I have 2k SubjectIDs and 7K parent/children. – user2909302 May 30 '19 at 12:22
  • Yes, it is possible to reformat your data using for loop over parent and child elements and create child1, child 2 so on when the condition meets. I am a bit busy now, but will be back to you soon. – Sam S. May 31 '19 at 03:00
  • How did you find my answer? Was it useful or did you find something better? Any update I can add? Please share your thought. – Sam S. Jul 04 '19 at 01:55