This topic is quite a mix of this one and this one. My troubles come from my inability to pass functions/code to all elements of a list of tibbles. I know how to get wanted results line by line, but can't do it in a whole.
For topic, let's take two tibbles very similar in structure to my real case.
MyRes_tw <- structure(list(text = c("follow @SmartRE_Info and get your token in waves t.co/g3q4XelPaK #SmartRE",
"RT @investFeed: Make your FEED work for you - check out this blog on the power of the FEED token: t.co/JOHSCeitGc",
"RT @investFeed: WE HAVE NOW PASSED 8,000 $ETH IN OUR TOKEN SALE PURCHASED! t.co/bx7s1xWyXI #ICO #Tokensale t.co/ZFndFhUfVT"
), Tweet.id = c("889602043249254400", "889589518159945729", "889573909405679616"
), created.date = structure(c(17371, 17371, 17371), class = "Date"),
created.week = c(30, 30, 31), retweet = c(0, 0, 0), custom = c(0,
0, 0)), .Names = c("text", "Tweet.id", "created.date", "created.week",
"retweet", "custom"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
MyRes1_tw <- structure(list(text = c("RT @AmbrosusAMB: We are on the front page of #NASDAQ / #Editorial Choice, Proud #Ethereum #Blockchain #ICO #TGE @Nasdaq @gavofyork @jutta_s…",
"RT @MyBit_DApp: 10 minutes left in #mybit #tokensale over 10,000 #ethereum contributed! Check it out t.co/AgyRCcyyzD",
"RT @MyBit_DApp: only 23 ETH left now", "RT @MyBit_DApp: #MyBit #tokensale ends in ~1 hour. 9k+ $ETH raised so far. Only 125 #ethereum left at 25% discount. t.co/AgyRCcyyzD",
"RT @MyBit_DApp: ~12 hours left in the t.co/AgyRCcyyzD #TokenSale #ICO 25% Bonus activated for #ethereum $ether #bitcoin $BTC $xbt"
), Tweet.id = c("897499492219445252", "897487635442274305", "897487621714305024",
"897487610494558208", "897487593117450244"), created.date = structure(c(17393,
17393, 17393, 17393, 17393), class = "Date"), created.week = c(33,
33, 34, 34, 34), retweet = c(0, 0, 0, 0, 0), custom = c(0, 0,
0, 0, 0)), .Names = c("text", "Tweet.id", "created.date", "created.week",
"retweet", "custom"), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
These two df are data coming from Twitter. I want to do some tidy on them to get at the end these results:
MyRes <- structure(list(created.week = c(33, 34, 35), retweet = c(12,
0, 8), custom = c(0, 0, 2), Twitter.name = c("MyRes", "MyRes",
"MyRes")), .Names = c("created.week", "retweet", "custom", "Twitter.name"
), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
MyRes1 <- structure(list(created.week = c(33, 34, 35), retweet = c(12,
0, 8), custom = c(0, 0, 2), Twitter.name = c("MyRes1", "MyRes1",
"MyRes1")), .Names = c("created.week", "retweet", "custom", "Twitter.name"
), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
Please note that names are important, the name of each result tibble being the name of tibbles from start with _tw being dropped.
As well, please note that in final result, last column $Twitter.name shall reflect the tibble name.
I list my tibbles in my environment this way myUser.tw <- ls(,pattern = "_tw")
as they are the only objects ending with _tw.
I wrote this function to help:
MySummarize <- function(x){
summarise(group_by(x, created.week, Retweet.count = sum(retweet), Custom.count = sum(custom)))
}
Now comes the pain! Below is my working code:
testLst <- mget(myUser.tw) %>%
lapply(function(x) MySummarize(x)) %>%
list2env(testLst, envir = .GlobalEnv)
Then I can't find a way to:
- change the names of the df to get MyRes, MyRes1 as names
- add a column with all rows containing the above text (MyRes, MyRes1)
- save the result in my environment.
believe it or not, I have been on this a very long time. I would appreciate help to finalize my whole code. Thank you