问题描述
大脑想要。我不知道这是否可以用 igraph 解决。基本上:
a.) 根据我的数据,我想为每个 ID 创建 TPM(已完成,请参阅示例代码)
b.) 我想为每个 TPM 创建一个有向图
c.) 计算特定节点的介数(在我的例子中是 1 和 5)
对于超过 1000 个 TPM 的大型数据集,我该如何执行此操作?
一些类似的topic
所需的输出:
数据结构:
示例代码:
Transition matrix creation:
lapply(seq_len(nrow(stack)),function(i) {
tmp <- trans.matrix(as.matrix(stack[i,2:6]))
write.csv(tmp,file = paste0(i,".csv"),quote = FALSE)
})
每个 id 的结果 TPM,每个 df 代表每个 id 的 TPM
df1<-structure(list(X1 = c(1,2,3,4),`2` = c(1,0),`3` = c(0,1,`4` = c(0,`5` = c(0,1)),class = c("spec_tbl_df","tbl_df","tbl","data.frame"),row.names = c(NA,-4L),spec = structure(list(
cols = list(X1 = structure(list(),class = c("collector_double","collector")),`2` = structure(list(),`3` = structure(list(),`4` = structure(list(),`5` = structure(list(),"collector"))),default = structure(list(),class = c("collector_guess",skip = 1L),class = "col_spec"))
df2<-structure(list(X1 = c(0,7,8,9),`6` = c(0,`7` = c(0,`8` = c(0,1),`9` = c(1,0)),`6` = structure(list(),`7` = structure(list(),`8` = structure(list(),`9` = structure(list(),class = "col_spec"))
df3<-structure(list(X1 = c(10,14,22,23),`14` = c(0,`22` = c(1,`23` = c(0,`25` = c(0,"data.frame"
),spec = structure(list(cols = list(
X1 = structure(list(),"collector"
)),`14` = structure(list(),`22` = structure(list(),`23` = structure(list(),`25` = structure(list(),class = "col_spec"))
df4<-structure(list(X1 = c(1,13),`1` = c(0,0.5,`13` = c(0,-3L),`1` = structure(list(),`13` = structure(list(),class = "col_spec"))
df5<--structure(list(X1 = c(1,2),0.333333333333333),0.333333333333333)),-2L),class = "col_spec"))
Sample data:
stack<-structure(list(X1 = c(1,4,5),a = c(1,10,b = c(2,9,13,c = c(3,23,d = c(4,e = c(5,6,25,5)),-5L),spec = structure(list(
cols = list(X1 = structure(list(),a = structure(list(),b = structure(list(),c = structure(list(),d = structure(list(),e = structure(list(),class = "col_spec"))
样本数据
解决方法
一个可能的 > Got it to work!
import React,{ useContext } from "react";
import { useQuery } from "@apollo/react-hooks";
import PostCard from "../../components/PostCard";
import { AuthContext } from "../../context/auth";
import gql from "graphql-tag";
export default function Dashboard() {
const context = useContext(AuthContext);
const { loading,data } =
useQuery(FETCH_POST_QUERY,{
variables: { userId: context.user.id },});
console.log(context.user);
if (loading) return <div>Loading...</div>;
const { getUserPost: userId } = data;
return (
<div className="PostCard">
<h1>Host Events</h1>
{userId.createdEvents.map((list) => (
<PostCard
// Add img
id={list.id}
key={list.id}
title={list.title}
description={list.description}
price={list.price}
address={list.address}
date={list.date}
/>
))}
</div>
);
}
// Query into a nested object -> User ->
CreatedEvents -> post
const FETCH_POST_QUERY = gql`
query getUserPost($userId: ID!) {
getUserPost(userId: $userId) {
createdEvents {
id
address
category
date
description
price
title
}
}
}
`;
选项
igraph
给予
# interested vertices in all graphs
v <- c("1","5")
data.frame(
t(
list2DF(
lapply(
# get all `df`s in the global environment and save in a list
mget(ls(pattern = "^df\\d+")),function(x) {
# row-column indices for non-zero values
inds <- data.frame(which(as.matrix(x[-1]) != 0,arr.ind = TRUE))
# replace values in `inds` by row or col names
df <- transform(
inds,row = x$X1[row],col = names(x[-1])[col]
)
# create graph object
g <- graph_from_data_frame(df)
# if the interested vertex shows up in the graph,then we calculate its betweenness centrality; otherwise,return NA
sapply(v,function(z) {
if (z %in% names(V(g))) {
betweenness(g,z,normalized = TRUE)
} else {
NA
}
})
}
)
)
),check.names = FALSE
)