Abhishek Choudhary
Abhishek Choudhary

Reputation: 8385

appending dataframe in list inside function in R

I have a function where I am seeking data frame from a method with certain range , for example 1-500 but cant ask the function to give all the values in one shot. So I tried to create a function to iterate value from a chunk of 100 each.

relationstatus <- function(n){
  mylist <- list()
  for(x in seq(1,n,100)) {
    y = x+99
      if(y > n){
        y = n
      }
    myFriends_info <- getUsers(myFriends$id[x:y], token=access_token, private_info=TRUE)
    #store the above myFriends_info in the list in each iteration
    append(mylist,myFriends_info)
    head(myFriends_info)
  } 
  return(mylist)
}

Input - 500
Output - list containing  data frames(myFriends_info data frames)

So for 500 input , I will be expecting a list containing 5 data frames

and then finally I am expecting all those data frame in a list and then the function return the list but I am expecting the function to work as in normal technologies like java or something but I am not able to solve it. Any better way to solve it or where am I doing it wrong as my list returns null

Upvotes: 0

Views: 528

Answers (1)

Abhishek Choudhary
Abhishek Choudhary

Reputation: 8385

It was simple , I just needed to rework on my list-

relationstatus <- function(n){
  mylist <- list()
  j = 1
  for(x in seq(1,n,100)) {
    y = x+99
      if(y > n){
        y = n
      }
    myFriends_info <- getUsers(myFriends$id[x:y], token=access_token, private_info=TRUE)
    sEOG <- paste("", j, sep="")
    mylist[[sEOG]] <-myFriends_info
    j = j+ 1
  } 
  return(mylist)
}

Upvotes: 1

Related Questions