R: Gephi: manipulating dataframe to use with write.gexf

前端 未结 2 1937
小鲜肉
小鲜肉 2020-12-21 12:51

I am trying to manipulate a data frame. As an example: say I have a dataframe containing customers and the shops they visit:

df = data.frame(customers = c(\"         


        
2条回答
  •  情深已故
    2020-12-21 13:14

    df <- data.frame(customers = c("a", "b", "b", "c", "c"),
                     shop_visited = c("X", "X", "Y", "X", "Z"))
    
    #create an identifier df
    dfnames <- data.frame(i = as.numeric(df$shop_visited), 
                          shop_visited = df$shop_visited)
    
    library(tnet)
    tdf       <- as.tnet( cbind(df[,2],df[,1]),type =  "binary two-mode tnet" )
    relations <- projecting_tm(tdf, method = "sum")
    
    # match original names
    relations[["i"]] <- dfnames[ match(relations[['i']], dfnames[['i']] ) , 'shop_visited']
    relations[["j"]] <- dfnames[ match(relations[['j']], dfnames[['i']] ) , 'shop_visited']
    
    # clean up names
    names(relations) <- c("source" , "target", "weight")
    
    
    #> relations
    #  source target weight
    #1      X      Y      1
    #2      X      Z      1
    #3      Y      X      1
    #4      Z      X      1
    

提交回复
热议问题