1

我有一个数据框,它是一个边缘列表(无向),描述了谁与谁联系在一起,然后是一个包含这些演员种族的数据框。我想得到一个数据框,其中在一个列中列出每个自我的名称,在另一列中列出他们对给定类型种族的改变的总和(例如乔和他的白人朋友的数量)。这是我尝试过的:

atts <- data.frame(Actor = letters[1:10], Ethnicity = sample(1:3, 10, replace=T)) # sample ethnicity data
df <- data.frame(actorA = letters[1:10],actorB=c("h","d","f","i","g","b","a","a","e","h")) # sample edgelist

df.split<-split(df$actorB,df$actorA) # obtain list of alters for column 1
head(df.split)

friends <- c()
n<-length(df.split)
for (i in 1:n){
  alters_e <-atts[atts$Actor %in% df.split[[i]]==TRUE,] # get ethnicity for alters
  friends[i] <- sum(alters_e$Ethnicity==3) # compute no. ties for one ethnicity value
} 
friends

这样做的问题是,如果您的某些自我仅出现在 actorB 列中,则使用 split 功能将不起作用。

任何人都可以为我推荐一种更优雅的方式来通过自我 ID 获取变更列表,这不是拆分功能吗?

4

1 回答 1

0

我希望这有帮助:

(atts <- data.frame(Actor = letters[1:10], Ethnicity = sample(1:3, 10, replace=T))) 
(df <- data.frame(alter = letters[1:10],ego=c("h","d","f","i","g","b","a","a","e","h"))) 

(Merged <- merge (df, atts, by.x="alter", by.y="Actor"))
with(Merged, table(ego,Ethnicity)) 

,大卫

于 2015-12-10T08:15:59.020 回答