2017-08-31 69 views
0

我有类似这样的很多tibbles的:R:应用功能dataframes的列表,并保存到工作区

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")) 

这里是我要适用于所有tibbles功能

编辑以下评论,我在函数中添加x作为最后一行

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 
} 

我的存取权限tibbles这样:

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

因为它们都是我环境中唯一以结尾的_tw

现在,这里是我做的如何应用功能:

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

但实际上它不会改变任何东西。通过一次运行以下一个df将按照我的想法更改它们。打印结果为OK。

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

现在我无法找到一种方法将结果分配给工作区中的df。我已经尝试了很多这样的事情:

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

或包括x <<- x我的函数结束,但没有成功。

任何人都可以帮助我将修改后的df保存在我的工作区中?谢谢

+0

mycount的没有返回'x',它返回'X $'定制和 –

+0

你lapply没有分配到任何东西,所以没有变量发生变化 –

+0

@Moody_Mudskipper感谢这个。我知道这是行不通的。你能帮我吗? – gabx

回答

1

你的示例代码中有很多问题。

myUser_tw没有被重复使用,您使用myUserList_tw代替,可能是一个错字。我将使用myUserList,因为使用以'tw'结尾的变量将不一致,因为您正在考虑将它们设为tibbles

Mycount函数不返回X(在你的编辑更改)都没有定义

retwcust,所以我以为他们是字符串,你忘了引号。

你的循环没有真正循环(不使用i),并且lapply的结果没有分配给任何东西。

这应该工作:

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`) 
} 
+0

是的,这是解决方案。对不起,1拼写错误和2 - 是的retw和cust是在第1列照顾字符串。所以,总而言之,诀窍是For(var in ...),而不是索引像我想的那样。TY – gabx

+0

这也可以工作:'for(i in 1:length(myUserList)){assign(myUserList [i],MyCount(get(myUserList [i])))}'。我认为你在拼写错误之外的主要问题是你认为通过引用做出了改变。 –