【发布时间】:2017-04-06 21:28:09
【问题描述】:
我有一个使用特定主题标签的 Twitter ID 列表,现在我正在尝试制作一个网络图来查看他们关注的人。使用全新的 rtweet 包,我的想法是对于每个 user_id,我使用 get_friends 函数并最终得到两列表 - userids | 以下。
问题在于,我最终得到的不是两列,而是一列。以下是我基于类似问题所做的事情:
#this is where the ids list comes from
head(ids)
user_id freq
2953382183 291
2832407758 178
522476436 149
773707421579677696 117
1296286704 113
773555423970529280 113
#for each user_id, get_friends() show me who the user is following
userids <- ids[1,1]
following <- get_friends(userids)
head(following)
ids
540219772
757699150507020288
2392165598
628569910
576547113
181996651
#NOW I'LL TRY TO FILL A NEW DATA FRAME FOR EACH "user_id" WITH ALL FOLLOWING "ids"
#initializing an empty data frame
final <- data.frame(userids = character(), following =character())
totalusers <- nrow(ids) #ids is a data frame where I got all `user_id`
userids <- NULL
following <- NULL
df <- NULL
for (i in 1:totalusers)
{
userids[i] <- ids[i,1]
following <- get_friends(userids[i]) #get_friends returns a data frame, by package default
df[i] <- data.frame(userids[i], following)
final <- rbind(final, df[i])
}
有谁知道我如何将 以下 变量附加到此数据框?非常感谢。
【问题讨论】:
-
您可能应该阅读关于生长对象的 RInferno。您要做的是索引正确的行和列,而不是在每次迭代中创建数据框,或者类似地,在每次迭代中使用
rbind。 -
非常感谢@shayaa。现在我将使用数据框的解决方案编辑帖子,同时我正在研究一种更有效的方法来做到这一点。
-
没问题。提供用于测试代码以及预期结果的最小数据集是公认的标准。如果你不这样做,你经常会被否决。此外,您可以针对自己的问题发布解决方案,无需将其保留为编辑。