How can I pass elegantly from table1 (with vertical relationships) to table2 (with horizontal relationships)?

```
library(data.table)
# vertical relationship
table1 <- data.table(id=1:4,father=c(NA,"Vladimir","Boris","John"),individual=c("Vladimir","Boris","John","Will"))
table1

```

[![enter image description here](https://i.stack.imgur.com/u8klt.jpg)](https://i.stack.imgur.com/u8klt.jpg)

```
# horizontal relationship
table2 <- data.table(id=1:4,greatgrandfather= c(NA,NA,NA,"Vladimir"), grandfather=c(NA,NA,"Vladimir","Boris"),father=c(NA,"Vladimir","Boris","John"),individual=c("Vladimir","Boris","John","Will"))
table2

```

[![enter image description here](https://i.stack.imgur.com/6qIv8.jpg)](https://i.stack.imgur.com/6qIv8.jpg)

Here goes the ugly solution I came up with: 

```
# ugly solution
temporary.table <- table1[,.(father,individual)]

names(temporary.table)<- c("grandfather","father")
new.table <- merge(table1,temporary.table,by="father",all.x=T)

names(temporary.table)<- c("greatgrandfather","grandfather")
new.table <- merge(new.table,temporary.table,by="grandfather",all.x=T)

ugly.solution <- new.table[order(id)][,.(id,greatgrandfather,grandfather,father,individual)]
ugly.solution

```

[![enter image description here](https://i.stack.imgur.com/Adzct.jpg)](https://i.stack.imgur.com/Adzct.jpg)