使用R SOFTWARE生成推文

dhxwm5r4  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(89)

我正在尝试编写一个r代码,使用以下代码在特定位置生成tweet

token <- create_token(consumer_key = "1234", consumer_secret = "1234", access_token = "1234", access_secret = "1234")

states_a <- c("ondo", "ekiti", "ogun", "lagos", "osun", "oyo")
unemployment_tweets_a <- search_tweets(q = "#unemployment",location = states_a, n = 2000,
                                  lang = "en",
                                  include_rts = FALSE)

但是代码不断地显示下面的代码
vapply(元素、编码、字符(1))中出错:值的长度必须为1,但FUN(X5)结果的长度为6
我有问题但没有进展

2g32fytz

2g32fytz1#

错误消息表明search_tweets()函数中的location参数需要单个位置,但传递给函数的states_a变量是多个位置的向量。
你可以使用lapply来实现你想要的:

library(rtweet)

auth <- rtweet_app()

# Create a function called FUN
FUN <- function(i) {
  search_tweets(q = "#unemployment",location = i, n = 2000,
                                       lang = "en",
                                       include_rts = FALSE, token = auth)
}

states_a <- c("ondo", "ekiti", "ogun", "lagos", "osun", "oyo")

# Use lapply to searh for each location
unemployment_tweets_a <- lapply(states_a, FUN)

# Then use rbind to convert the list to a dataframe
unemployment_tweets_a  <- do.call(rbind, unemployment_tweets_a)

通过这种方式,您将获得来自您指定的所有6个位置的推文。

相关问题