gpt4 book ai didi

R:Gephi:操作数据帧以与 write.gexf 一起使用

转载 作者:行者123 更新时间:2023-12-03 03:38:31 27 4
gpt4 key购买 nike

我正在尝试操作数据框。举个例子:假设我有一个包含客户及其访问的商店的数据框:

df = data.frame(customers = c("a", "b", "b", "c", "c"),
shop_visited = c("X", "X", "Y", "X", "Z"))
customers shop_visited
a X
b X
b Y
c X
c Z

总结此数据框:

  • 一名顾客 (b) 在 XY 购物;
  • 一名顾客 (b) 在 YX 购物;
  • 一名顾客 (c) 在 XZ 购物;
  • 一名顾客 (c) 在 ZX 购物

或者,更简洁地说:

relations = data.frame(source = c("X","Y", "X", "Z"), 
target = c("Y","X","Z","X"))
source target
X Y
Y X
X Z
Z X

我正在寻找一种能够进行df -> 关系转换的方法。其背后的动机是我可以使用 relations 作为 write.gexf 中的 edges 参数。为任何帮助干杯。

最佳答案

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

关于R:Gephi:操作数据帧以与 write.gexf 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16173990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com