【问题标题】:If "NAs are not allowed in subscripted assignments", how do I compare and match values of a table to a longer list of names in R?如果“下标分配中不允许使用 NA”,如何将表的值与 R 中较长的名称列表进行比较和匹配?
【发布时间】:2020-06-17 15:46:04
【问题描述】:

感谢您在这里帮助我! 我有一个包含 71,483 个用户名(“ALL”)的列表和一个包含 Twitter 数据的数据框(“ActDred”)。

我想计算用户的频率 a) 发布推文和 b) 回复帖子, 然后将此号码添加到用户列表中。有些用户不发推文或回复,我会为他们添加 NA。

这就是我所做的,它适用于 a) 但不适用于 b)。对于 b),我收到错误消息:“NAs are not allowed in subscripted assignments”。

#a)
#Select only Tweet edges
Tw<-ActDred[ActDred$Relationship=="Tweet",]
#Number of tweets of each user
NTw<-table(Tw$`Vertex 1`)

#Match number of tweets to list of all nodes
SA_NumTw <- rep(NA, length(ALL))
SA_NumTw[match(names(NTw), ALL)]<-NTw

#b)
#Select only Reply edges
Re <- ActDred[ActDred$Relationship=="Replies to",]
#Number of replies of each user
NRe<-table(Re$`Vertex 1`)

#Match number of replies to list of all nodes
SA_NumRe <- rep(NA, length(ALL))
SA_NumRe[match(names(NRe), ALL)] <- NRe

对于 b),我得到 ​​p>

“SA_NumRe[which] 中的错误

有谁知道我在这里缺少什么以及为什么它适用于 a) 但不适用于 b)?

非常感谢!

【问题讨论】:

  • 只是一条路过的评论...如果用户不发推文或回复,也许分配 0 而不是 NA?
  • 谢谢!我试过了,也有 99 但没有任何效果,我仍然得到同样的错误。奇怪的是,它以前曾经工作过。几周前,我在几乎相同的数据上使用了完全相同的代码。

标签: r match na


【解决方案1】:

我发现了问题! 有一些用户回复了但不在 ALL 列表中。我先删除了它们,然后继续,它起作用了:)

这是我所做的:

Re <- ActDred[ActDred$Relationship=="Replies to",]
RbutA <- setdiff(Re$`Vertex 1`, ALL) 
# there are 19 repliers that are not in the ALL list, hence I get the error and need to eliminate the 19 users first
Re2 <- Re[Re$`Vertex 1` %in% ALL,1:8]
#Number of replies of each user
NRe<-table(Re2$`Vertex 1`)

#Match number of replies to list of all nodes
SA_NumRe <- rep(NA, length(ALL))
SA_NumRe[match(names(NRe), ALL)] <- NRe

现在,我不再收到错误消息了 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 2020-07-03
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多