我有一个数据框,它是一个边缘列表(无向),用于描述谁与谁绑在一起,然后是与这些参与者绑定的数据框。种族。我想得到一个数据框,其中列出了一列中每个自我的名称,以及另一个列表中给定类型种族的改变的总和(例如乔和他的白人朋友的数量)。这就是我的尝试:
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的改名列表,这不是拆分功能吗?
答案 0 :(得分: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))
,大卫