ldply中的列表中使用对象名称

ldply中的列表中使用对象名称

本文介绍了在lapply/ldply中的列表中使用对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试回答 a问题之前,我遇到了一个看起来应该很简单的问题,但我无法弄清楚.

In attempting to answer a question earlier, I ran into a problem that seemed like it should be simple, but I couldn't figure out.

如果我有数据帧列表:

df1 <- data.frame(a=1:3, x=rnorm(3))
df2 <- data.frame(a=1:3, x=rnorm(3))
df3 <- data.frame(a=1:3, x=rnorm(3))

df.list <- list(df1, df2, df3)

我想一起rbind,我可以执行以下操作:

That I want to rbind together, I can do the following:

df.all <- ldply(df.list, rbind)

但是,我想要另一列来标识每行来自哪个data.frame.我希望能够使用deparse(substitute(x))方法(以及其他位置)以获取相关data.frame的名称并添加一列.这是我的处理方式:

However, I want another column that identifies which data.frame each row came from. I expected to be able to use the deparse(substitute(x)) method (here and elsewhere) to get the name of the relevant data.frame and add a column. This is how I approached it:

fun <- function(x) {
  name <- deparse(substitute(x))
  x$id <- name
  return(x)
}
df.all <- ldply(df.list, fun)

返回哪个

  a          x      id
1 1  1.1138062 X[[1L]]
2 2 -0.5742069 X[[1L]]
3 3  0.7546323 X[[1L]]
4 1  1.8358605 X[[2L]]
5 2  0.9107199 X[[2L]]
6 3  0.8313439 X[[2L]]
7 1  0.5827148 X[[3L]]
8 2 -0.9896495 X[[3L]]
9 3 -0.9451503 X[[3L]]

因此,显然列表中的每个元素都不包含我认为包含的名称.谁能建议一种实现我所期望的方式(如下所示)?

So obviously each element of the list does not contain the name I think it does. Can anyone suggest a way to get what I expected (shown below)?

  a          x  id
1 1  1.1138062 df1
2 2 -0.5742069 df1
3 3  0.7546323 df1
4 1  1.8358605 df2
5 2  0.9107199 df2
6 3  0.8313439 df2
7 1  0.5827148 df3
8 2 -0.9896495 df3
9 3 -0.9451503 df3

推荐答案

用名称定义列表,它应该为您提供一个.id列,其名称为data.frame

Define your list with names and it should give you an .id column with the data.frame name

df.list <- list(df1=df1, df2=df2, df3=df3)
df.all <- ldply(df.list, rbind)

输出:

  .id a           x
1 df1 1  1.84658809
2 df1 2 -0.01177462
3 df1 3  0.58579469
4 df2 1 -0.64748756
5 df2 2  0.24384614
6 df2 3  0.59012676
7 df3 1 -0.63037679
8 df3 2 -1.17416295
9 df3 3  1.09349618

然后您可以从列df.all$.id

根据@Gary Weissman的评论,如果您想自动生成名称,可以这样做

As per @Gary Weissman's comment if you want to generate the names automatically you can do

names(df.list) <- paste0('df',seq_along(df.list)

这篇关于在lapply/ldply中的列表中使用对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:27