R:将函数应用于数据框列表并保存到工作区 [英] 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"))
这是我想应用到所有 tibbles 的函数
Here is the function I want to apply to all tibbles
EDIT 以下评论,我在函数中添加 x 作为最后一行
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")
因为它们都是我 env 中唯一以 _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)
}
但实际上它不会改变任何东西.一个接一个地运行以下 df 将按照我想要的方式改变它们.打印结果正常.
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)
现在我找不到将结果分配给工作区中的 df 的方法.我尝试过很多这样的事情:
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)
或在我的函数末尾包含 x <<- x
,但没有成功.
or include x <<- x
at the end of my function, but no success.
谁能帮我将修改后的 df 保存在我的工作区中?谢谢
Cany anyone help me to save the modified df in my workspace? Thank you
推荐答案
您的示例代码中有很多问题.
There are a lot of issues in your sample code.
myUser_tw
未重用,您使用 myUserList_tw
代替,可能是打字错误.我将使用 myUserList
因为使用以 'tw'
结尾的变量会不一致,因为您将它们视为 tibbles
.
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
.
您的 Mycount
函数不返回 x(在您的编辑中更改)
Your Mycount
function doesn't return x (changed in your edit)
retw
和 cust
没有定义,所以我假设它们是字符串并且你忘记了引号.
retw
and cust
are not defined, so I will assume they are strings and you forgot the quotes.
你的循环并没有真正循环任何东西(没有使用 i
),并且 lapply
的结果没有分配给任何东西.
Your loop is not really looping on anything (the i
is not used), and the result of lapply
is not assigned to anything.
这应该有效:
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`)
}
这篇关于R:将函数应用于数据框列表并保存到工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!