Search code examples
rdataframeapplyworkspace

R: apply a function to a list of dataframes and save to workspace


I have a lot of tibbles similar to this one:

dftest_tw <- structure(list(text = c("RT @BitMEXdotcom: A new high: US$500M turnover in the last 24 hours, over 80% of it on $XBTUSD. Congrats to the team and thank you to our u…", 
"RT @Crowd_indicator: Thank you for this nice video, @Nicholas_Merten", 
"RT @Crowd_indicator: Review of #Cindicator by DataDash: t.co/D0da3u5y3V"
), Tweet.id = c("896858423521837057", "896858275689398272", "896858135314538497"
), created.date = structure(c(17391, 17391, 17391), class = "Date"), 
    created.week = c(33, 33, 33)), .Names = c("text", "Tweet.id", 
"created.date", "created.week"), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))

Here is the function I want to apply to all tibbles

EDIT following comment, I add x in my function as last line

MyCount <- function(x){
  x$retweet <- NA
  x$custom <- NA
  x$retweet <- grepl(retw, x$text) * 1
  x$custom <- (grepl(cust, x$text) & !grepl(retw, x$text)) * 1
  x
}

I acces the tibbles this way:

myUser_tw <- ls(,pattern = "_tw")

as they all are the only ones in my env to end with _tw.

Now here is how I do to apply function:

for (i in 1:length(myUserList_tw)){
  lapply(mget(myUserList_tw), MyCount)
}

but in fact it will not change anything. Running the following one df by one will change them the way I want. The printed result is OK.

lapply(mget(myUser_tw[x]), MyCount) 

Now I can't find a way to assign the result to the df in my workspace. I have tried many things like this:

myUser_tw[x] <- lapply(mget(myUser_tw[x]), MyCount) 

or include x <<- x at the end of my function, but no success.

Cany anyone help me to save the modified df in my workspace? Thank you


Solution

  • There are a lot of issues in your sample code.

    myUser_tw is not reused, you use myUserList_tw instead, probably a typo. I will use myUserList beause using a variable ending with 'tw' wouldn't be consistent, as you're considering those to be tibbles.

    Your Mycount function doesn't return x (changed in your edit)

    retw and cust are not defined, so I will assume they are strings and you forgot the quotes.

    Your loop is not really looping on anything (the i is not used), and the result of lapply is not assigned to anything.

    This should work:

    dftest_tw <- structure(list(text = c("RT @BitMEXdotcom: A new high: US$500M turnover in the last 24 hours, over 80% of it on $XBTUSD. Congrats to the team and thank you to our u…", 
                                         "RT @Crowd_indicator: Thank you for this nice video, @Nicholas_Merten", 
                                         "RT @Crowd_indicator: Review of #Cindicator by DataDash: t.co/D0da3u5y3V"
    ), Tweet.id = c("896858423521837057", "896858275689398272", "896858135314538497"
    ), created.date = structure(c(17391, 17391, 17391), class = "Date"), 
    created.week = c(33, 33, 33)), .Names = c("text", "Tweet.id", 
                                              "created.date", "created.week"), row.names = c(NA, -3L), class = c("tbl_df", 
                                                                                                                 "tbl", "data.frame"))
    
    dftest2_tw <- dftest_tw # so we have 2
    
    MyCount <- function(x){
      x$retweet <- NA
      x$custom <- NA
      x$retweet <- grepl("retw", x$text) * 1
      x$custom <- (grepl("cust", x$text) & !grepl("retw", x$text)) * 1
      x
    }
    
    myUserList <- ls(,pattern = "_tw")
    for(var in myUserList){
      assign(var,MyCount(get(var))) # assign to the variable described by string `var` the result of the function MyCount applied on the value of `var` (itself obtained by `get`) 
    }