0

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:

  1. change the names of the df to get MyRes, MyRes1 as names
  2. add a column with all rows containing the above text (MyRes, MyRes1)
  3. 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

gabx
  • 472
  • 2
  • 7
  • 18

2 Answers2

2

It wasn't clear what "the df" refers to but if the objective is to get a list of summaries with a source column appended:

library(dplyr)

myUser.tw %>% 
  mget(.GlobalEnv) %>%
  lapply(MySummarize) %>%
  bind_rows(.id = "source") %>%
  mutate(source = sub("_tw$", "", source)) %>%
  split(.$source)

giving:

$MyRes
# A tibble: 2 x 4
# Groups:   created.week, Retweet.count [2]
  source created.week Retweet.count Custom.count
   <chr>        <dbl>         <dbl>        <dbl>
1  MyRes           30             0            0
2  MyRes           31             0            0

$MyRes1
# A tibble: 2 x 4
# Groups:   created.week, Retweet.count [2]
  source created.week Retweet.count Custom.count
   <chr>        <dbl>         <dbl>        <dbl>
1 MyRes1           33             0            0
2 MyRes1           34             0            0

or if you want a single data frame omit the split.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • TY for your solution. I must admit I don't know which one I prefer, so, let's say "girl power". :-) – gabx Sep 02 '17 at 13:10
1

One possible approach:

# list of tibbles with tw
myUser.tw.list <- mget(myUser.tw) 

# perform lapply over the sequence of positions rather than the list of elements
myUser <- lapply(seq(myUser.tw), 
       function(i){
         myUser.tw.list[i][[1]] %>% group_by(created.week) %>%
           summarise(retweet = sum(retweet), custom = sum(custom)) %>%
           ungroup() %>%
           mutate(Twitter.name = gsub("_tw$", "", names(myUser.tw.list[i])))
         })
names(myUser) <- gsub("_tw$", "", myUser.tw)

Result: list of tibbles with names

> myUser
$MyRes
# A tibble: 2 x 4
  created.week retweet custom Twitter.name
         <dbl>   <dbl>  <dbl>        <chr>
1           30       0      0        MyRes
2           31       0      0        MyRes

$MyRes1
# A tibble: 2 x 4
  created.week retweet custom Twitter.name
         <dbl>   <dbl>  <dbl>        <chr>
1           33       0      0       MyRes1
2           34       0      0       MyRes1
Z.Lin
  • 28,055
  • 6
  • 54
  • 94