简体   繁体   English

遍历列表以创建不同对象的函数

[英]Function that iterates through list to create different objects

I currently have written a function where I am individually going through a number of twitter handles and names an object depending on that twitter user's last name.我目前已经编写了一个函数,在该函数中我将单独浏览多个 twitter 句柄并根据该 twitter 用户的姓氏命名一个对象。 For example:例如:

f <- function() {
  dougjones <- rtweet::get_timeline('@SenDougJones', n = 50, max_id = NULL, home = FALSE, parse = TRUE, check = FALSE, token = token, include_rts = FALSE)
  bennet <- rtweet::get_timeline('@SenatorBennet', n = 50, max_id = NULL, home = FALSE, parse = TRUE, check = FALSE, token = token, include_rts = FALSE)
  blumenthal <- rtweet::get_timeline('@SenBlumenthal', n = 50, max_id = NULL, home = FALSE, parse = TRUE, check = FALSE, token = token, include_rts = FALSE)
}

I want to simplify this by creating a list of user last names and of their twitter handles to create some code that would look like this (if the code worked correctly)我想通过创建用户姓氏和他们的 twitter 句柄列表来简化这一点,以创建一些看起来像这样的代码(如果代码工作正常)

list <- list(
  handles(handle = 
  tibble(c('@SenDougJones','@SenatorBennet','@SenBlumenthal')),
  names(name = tibble(c('Jones','Bennet','Blumenthal')))

f <- function(list){
  for(i in seq_along(list$handles)){
  for(j in seq_along(list$names)){
   names[[j]] <- rtweet::get_timelines(list$handles[[i]],n=50,max_id=NULL,home=FALSE,parse=TRUE, check = FALSE, token = token,include_rts=FALSE) }}}

I know this code is wrong because I get NULL when I run it.我知道这段代码是错误的,因为我在运行它时得到 NULL。 I was wondering how would I go about doing this.我想知道我将如何去做这件事。 Essentially what I am hoping to do is to feed through the twitter handles iteratively through the get_timeline() function and to also name the objects from each iteration based on the last name of the user.基本上我希望做的是通过 get_timeline() 函数迭代地输入 twitter 句柄,并根据用户的姓氏命名每次迭代中的对象。

I am somewhat new with using loops so I have no clue if this is a syntax issue or what, but I was hoping you all might have suggestions.我对使用循环有点陌生,所以我不知道这是语法问题还是什么,但我希望你们都能提出建议。 What should I do to get this outcome?我该怎么做才能得到这个结果?

I do not know what exactly you are looking for, but the following should do the trick.我不知道您到底在寻找什么,但以下内容应该可以解决问题。

last.names <- c('Jones','Bennet','Blumenthal')
twitter.handles <- c('@SenDougJones','@SenatorBennet','@SenBlumenthal')

my.list <- list(last.names = last.names,
                twitter.handles = twitter.handles,
                indexes = seq_along(last.names))

my.list$twitter.timeline <- lapply(my.list$indexes,
       FUN = function(index) {
         print(paste(my.list$last.names[[index]], my.list$twitter.handles[[index]], sep = ";"))
         
         twitter.timeline <- rtweet::get_timelines(my.list$twitter.handles[[index]],
                                                                   n=50,
                                                                    max_id=NULL,
                                                                    home=FALSE,
                                                                    parse=TRUE,
                                                                    check = FALSE,
                                                                    token = token,
                                                                    include_rts=FALSE)

           return(twitter.timeline)
       })


HTH!哼!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历每一行的函数 - A function that iterates through every row object 的访问名称,“i”表示当它在 for 循环中遍历 R 中的对象列表时 - Access name of the object that “i” represents when it iterates in a for-loop through a list of objects in R 创建遍历 dataframe 列的 function - Create function that iterates across dataframe columns 使用对数据框中的行进行迭代的函数将元素追加到列表中 - Append elements to list using a function that iterates over rows in a data frame 通过 function 传递列表并创建摘要 df - Pass a list through a function and create a summary df 将函数应用于具有不同函数参数的网络对象列表 - Apply function to a list of network objects with different function arguments 如何创建一个新的 function 来迭代我之前在 R 中创建的 function? - How do I create a new function that iterates a function I previously made in R? 在使用 lapply 创建的 plot 对象列表中创建不同类型的图 - Create different types of plots within list of plot objects created with lapply 如何在 R 中创建一个简单的线性回归 function 来迭代整个 dataframe? - How do I create a simple linear regression function in R that iterates over the entire dataframe? 创建一个函数,该函数将通过在R中子集一个数据帧来列出数据帧 - Create a function which will make a list of dataframes through subsetting a dataframe in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM