使用 R 将 author_id 转换为 author_username

Converting author_id to author_username with R

我的数据集(大约 30000 条推文)中有一个“sourcetweet_author_id”列,其中包括引用和转推用户的推特 ID。我想将推特 ID 转换为推特用户名。

我用 rtweet 包的 lookup_users 功能收集了“sourcetweet_author_id”的用户名。

data.with.usernames <- lookup_users(as_userid(mydata$sourcetweet_author_id))

示例输出:

示例数据:

"user_id" "status_id" "created_at" "screen_name"
"99564663" "1521494990890876929" 2022-05-03 14:20:48 "LeventUzumcu"
"4274638635" "1521110034515701760" 2022-05-02 12:51:07 "SalihaSnmezate1"
"1266093027254325250" "1300887103874707457" 2020-09-01 20:03:49 "arjin3426"
"1494034783" "1521523729599107073" 2022-05-03 16:15:00 "DikenComTr"

但是这个函数只返回唯一用户列表。这很正常,因为我的数据集包含来自同一条推文的许多转推。

现在,我需要一个函数来将每个 sourcetweet_author_id 与其用户名匹配,并使用该函数将“user_id”列中的所有 ID 转换为新列中的用户名。

我的原始数据集的样本数据 table:

"sourcetweet_author_id" "created_at" "retweet_count" "like_count"
"99564663" "2020-07-23T14:00:39.000Z" 8031 0
"99564663" "2020-07-23T14:00:35.000Z" 7153 0
"1266093027254325250" "2020-07-23T14:00:29.000Z" 7153 0
"1266093027254325250" "2020-07-23T14:00:29.000Z" 6596 0

这应该将 screen_name 列添加到 original_dataset:

library(dplyr)
original_dataset %>%
  left_join(
    select(data.with.usernames, sourcetweet_author_id = user_id, screen_name)
  )