简体   繁体   中英

R: add column based on existing column relationship

I have a data frame that I'm using to tabularly represent a directed graph relationship. Right now I am considering three nodes at a time. I have the relation between node1 and node2, and the relation between node2 and node3, each of which have corresponding edge weights. In rare cases there is a relationship between node1 and node3, but I am having trouble isolating it. In essence, I have A -> B, and B -> C, and want to find A -> C. Here is an example of the data frame.

df
      node1    node2   weight1     node2_a     node3   weight2
       5         2        .2          2         7        .3
       10        20       .4         20         30       .6
       10        30       .3         30         8        .3

So, I would essentially like to have another three columns, which would read like this

new_df
      node1    node2   weight1     node2_a     node3   weight2   node1.a     node3.a     weight3
       5         2        .2          2         7        .3         na          na          na
       10        20       .4         20         30       .6         10          30          .3
       10        30       .3         30         8        .3         na          na         na

I understand there may be better ways to do this, but I'm curious how I can make it work like this. Thanks, if this is unclear I am happy to explain further.

This gives you more or less what you need...

merge(df, df[1:3], 
      by.x=c("node1","node3"), by.y=c("node1","node2"), 
      all.x=TRUE)

  node1 node3 node2 weight1.x node2_a weight2 weight1.y
1     5     7     2       0.2       2     0.3        NA
2    10     8    30       0.3      30     0.3        NA
3    10    30    20       0.4      20     0.6       0.3

This reorders the dataframe, but that may not matter. You might like to reorder and rename the columns ( weight1.y is your weight3 ), and to add your node1.a and node3.a columns as equal to node1 and node3 for those rows for which weight1.y is not NA .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM