l_ply:如何将列表的名称属性传递到函数中?

发布于 2024-09-15 23:24:50 字数 818 浏览 6 评论 0原文

假设我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2024-09-22 23:24:50

最简单的方法是从名称开始,然后使用它们来提取您感兴趣的部分:

l_ply(names(data.list),function(name,...) {
  df <- data.list[[name]]
)

您还可以使用 m_ply 传入名称和数据:

m_ply(cbind(names(data.list), data.list), function(name, df, ...) {
   ...
}

It's easiest to start with the names, and then use them to extract the bit you're interested in:

l_ply(names(data.list),function(name,...) {
  df <- data.list[[name]]
)

You can also use m_ply to pass in both the name and data:

m_ply(cbind(names(data.list), data.list), function(name, df, ...) {
   ...
}
|煩躁 2024-09-22 23:24:50

如果您一一传递它们,您可以使用 deparse(substitute(arg)) ,例如:

test <- function(x){
       y <- deparse(substitute(x))
       print(y)
       print(x)
 }

 var <- c("one","two","three")
 test(var)
[1] "var"
[1] "one"   "two"   "three"

对于 l_ply,您必须将属性添加到列表本身,例如:

for(i in 1:length(data.list)){
    attr(data.list[[i]],"name") <- names(data.list)[i]
}

然后您可以使用 attr :

cli <- attr(x,"name")

干杯

In case you pass them one by one, you can use deparse(substitute(arg)) , eg :

test <- function(x){
       y <- deparse(substitute(x))
       print(y)
       print(x)
 }

 var <- c("one","two","three")
 test(var)
[1] "var"
[1] "one"   "two"   "three"

for l_ply, you'll have to resort to add the attribute to the list itself eg :

for(i in 1:length(data.list)){
    attr(data.list[[i]],"name") <- names(data.list)[i]
}

Then you can use attr :

cli <- attr(x,"name")

Cheers

可是我不能没有你 2024-09-22 23:24:50

乔里斯 答案是最干净的方法。我会添加一个函数来提取属性:

for(ename in names(data.list)) {
    attr(data.list[[ename]], "ename") <- ename
}
ename <- function(x) attr(x, "ename") # states for element name

所以您将其用作:

l_ply(data.list, function(df, ...) {
    cli.name<- ename(df)
    # make plots, save results using cli.name
})

我通常使用此方法:

l_ply(names(data.list), function(cli.name, df=data.list[[cli.name]], ...) {
    # make plots, save results using cli.name
})

迭代名称并使用它们从原始列表中提取 data.frame


只是为了通知,有一个黑客。我不推荐它,因为它会扰乱框架并且很难控制。
利用 llply 实际上是一个 for 循环这一事实,您可以从函数内部提取实际步骤。可以在正确的环境下使用 get 来完成。

l_ply(data.list, function(df, ...) {
    cli.name<- names(data.list)[get("i",parent.frame())]
    # make plots, save results using cli.name
})

Joris answer is the cleanest way to do this. I would add a function to extract attribute:

for(ename in names(data.list)) {
    attr(data.list[[ename]], "ename") <- ename
}
ename <- function(x) attr(x, "ename") # states for element name

So you use it as:

l_ply(data.list, function(df, ...) {
    cli.name<- ename(df)
    # make plots, save results using cli.name
})

I usually use this method:

l_ply(names(data.list), function(cli.name, df=data.list[[cli.name]], ...) {
    # make plots, save results using cli.name
})

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 a for loop you can extract actual step from the inside of a function. It can be done using get with the correct environment.

l_ply(data.list, function(df, ...) {
    cli.name<- names(data.list)[get("i",parent.frame())]
    # make plots, save results using cli.name
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文