l_ply:如何将列表的名称属性传递到函数中?
假设我有一个像这样的 R 列表:
> summary(data.list)
Length Class Mode
aug9104AP 18 data.frame list
Aug17-10_acon_7pt_dil_series_01 18 data.frame list
Aug17-10_Picro_7pt_dil_series_01 18 data.frame list
Aug17-10_PTZ_7pt_dil_series_01 18 data.frame list
Aug17-10_Verat_7pt_dil_series_01 18 data.frame list
我想使用 l_ply
处理列表中的每个 data.frame,但我还需要将名称(例如 aug9104AP)与数据框。例如:
l_ply(data.list,function(df,...) {
cli.name<- arg_to_access_current_list_item_name
#make plots with df, use cli.name in plot titles
#save results in a file called cli.name
}, arg_to_access_current_list_item_name
)
arg_to_access_current_list_item_name
应该是什么?
Say I have an R list like this:
> summary(data.list)
Length Class Mode
aug9104AP 18 data.frame list
Aug17-10_acon_7pt_dil_series_01 18 data.frame list
Aug17-10_Picro_7pt_dil_series_01 18 data.frame list
Aug17-10_PTZ_7pt_dil_series_01 18 data.frame list
Aug17-10_Verat_7pt_dil_series_01 18 data.frame list
I want to process each data.frame in the list using l_ply
, but I also need the name (e.g. aug9104AP) to be passed into the processing function along with the data.frame. Something like:
l_ply(data.list,function(df,...) {
cli.name<- arg_to_access_current_list_item_name
#make plots with df, use cli.name in plot titles
#save results in a file called cli.name
}, arg_to_access_current_list_item_name
)
What should arg_to_access_current_list_item_name
be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是从名称开始,然后使用它们来提取您感兴趣的部分:
您还可以使用
m_ply
传入名称和数据:It's easiest to start with the names, and then use them to extract the bit you're interested in:
You can also use
m_ply
to pass in both the name and data:如果您一一传递它们,您可以使用 deparse(substitute(arg)) ,例如:
对于 l_ply,您必须将属性添加到列表本身,例如:
然后您可以使用 attr :
干杯
In case you pass them one by one, you can use deparse(substitute(arg)) , eg :
for l_ply, you'll have to resort to add the attribute to the list itself eg :
Then you can use attr :
Cheers
乔里斯 答案是最干净的方法。我会添加一个函数来提取属性:
所以您将其用作:
我通常使用此方法:
迭代名称并使用它们从原始列表中提取
data.frame
。只是为了通知,有一个黑客。我不推荐它,因为它会扰乱框架并且很难控制。
利用
llply
实际上是一个for
循环这一事实,您可以从函数内部提取实际步骤。可以在正确的环境下使用get
来完成。Joris answer is the cleanest way to do this. I would add a function to extract attribute:
So you use it as:
I usually use this method:
Iterate over names and extract
data.frame
from original list using them.Just for the notice there is a hack. I don't recommend it cause it mess with frames and it's hard to control.
Using fact that
llply
is actually afor
loop you can extract actual step from the inside of a function. It can be done usingget
with the correct environment.