R:如何根据频率为我的网络创建权重?
我有一个edgelist(2列),我想创建一个第3列,根据我的数据中每个单词的提及次数为每个节点指定权重 见附件我的数据 例如:“oil”“bad”“gas”多次出现,我希望每次出现同一个值时都添加值“1”(并删除多行)R:如何根据频率为我的网络创建权重?,r,network-analysis,R,Network Analysis,我有一个edgelist(2列),我想创建一个第3列,根据我的数据中每个单词的提及次数为每个节点指定权重 见附件我的数据 例如:“oil”“bad”“gas”多次出现,我希望每次出现同一个值时都添加值“1”(并删除多行) 对于这种情况,一个简单的解决方案就是使用表格 #create some sample data set.seed(1) node1<-rep("oil drilling", 20) node2<-sample(c("gas", "frack", "pollute",
对于这种情况,一个简单的解决方案就是使用
表格
#create some sample data
set.seed(1)
node1<-rep("oil drilling", 20)
node2<-sample(c("gas", "frack", "pollute", "good"),20,replace=T)
edglst<-data.frame(node1, node2)
head(edglist,10)
node1 node2
1 oil drilling frack
2 oil drilling frack
3 oil drilling pollute
4 oil drilling good
5 oil drilling gas
6 oil drilling good
7 oil drilling good
8 oil drilling pollute
9 oil drilling pollute
10 oil drilling gas
#use table to get a dataframe with one row per combination and its frequency
as.data.frame(table(edglst))
node1 node2 Freq
1 oil drilling frack 5
2 oil drilling gas 4
3 oil drilling good 6
4 oil drilling pollute 5
#创建一些示例数据
种子(1)
node1对于这种情况,一个简单的解决方案就是使用table
#create some sample data
set.seed(1)
node1<-rep("oil drilling", 20)
node2<-sample(c("gas", "frack", "pollute", "good"),20,replace=T)
edglst<-data.frame(node1, node2)
head(edglist,10)
node1 node2
1 oil drilling frack
2 oil drilling frack
3 oil drilling pollute
4 oil drilling good
5 oil drilling gas
6 oil drilling good
7 oil drilling good
8 oil drilling pollute
9 oil drilling pollute
10 oil drilling gas
#use table to get a dataframe with one row per combination and its frequency
as.data.frame(table(edglst))
node1 node2 Freq
1 oil drilling frack 5
2 oil drilling gas 4
3 oil drilling good 6
4 oil drilling pollute 5
#创建一些示例数据
种子(1)
node1我不想输入您的数据,因此我将用一些生成的数据进行说明
set.seed(1234)
x = sample(LETTERS[1:6], 20, replace=TRUE)
y = sample(letters[1:6], 20, replace=TRUE)
dat = data.frame(x,y)
您可以从plyr
包中的count
函数中获取所需的计数
library(plyr)
count(dat)
x y freq
1 A b 1
2 A d 1
3 B b 4
4 B e 1
5 B f 2
6 D a 3
7 D b 2
8 D e 2
9 E c 1
10 F b 1
11 F d 1
12 F e 1
我不想输入您的数据,因此我将用一些生成的数据进行说明
set.seed(1234)
x = sample(LETTERS[1:6], 20, replace=TRUE)
y = sample(letters[1:6], 20, replace=TRUE)
dat = data.frame(x,y)
您可以从plyr
包中的count
函数中获取所需的计数
library(plyr)
count(dat)
x y freq
1 A b 1
2 A d 1
3 B b 4
4 B e 1
5 B f 2
6 D a 3
7 D b 2
8 D e 2
9 E c 1
10 F b 1
11 F d 1
12 F e 1
请不要以图像形式提供数据。我们谁也不想全部输入。相反,使用dput
创建数据的文本版本,并将其包含在问题中。如果您的数据太多,无法包含所有数据,您只需提供开头的dput(head(dat,20))
请不要以图像形式提供数据。我们谁也不想全部输入。相反,使用dput
创建数据的文本版本,并将其包含在问题中。如果您有太多的数据无法包含所有数据,您可以只提供开头的dput(head(dat,20))
Nice solution+1,但是如果数据中没有出现node1-node2对,您将得到权重为0的难看行谢谢!count函数与as.data.frame(表(x))一样工作,但不包括具有零counts的组合ice solution+1,但是如果数据中没有出现node1-node2对,则将得到具有0权重的难看行。谢谢!count函数与as.data.frame(表(x))一样工作,但不包括与零counts的组合。此外,count函数提供了我所需要的,但我仍然无法根据每个单词在原始数据集中的第三列中打开“freq”列。这就是我一直在尝试的:require(“plyr”)count2awesome,count函数提供了我所需要的,但我仍然无法根据每个单词在原始数据集中的第三列中打开“freq”列。这就是我一直在尝试的:require(“plyr”)count2