如何根据每个组件的名称在列表的所有组件中创建一个新列?

发布于 2025-01-24 01:06:22 字数 1285 浏览 0 评论 0原文

这是一个示例列表:

list1 <- list(a = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1), 
    d = c(2, 2, 2, 2)), class = "data.frame", row.names = c(NA, 
-4L)), b = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2)), class = "data.frame", row.names = c(NA, 
-4L)), d = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2)), class = "data.frame", row.names = c(NA, 
-4L)))

我想在列表的每个组件(即A,B,d)中添加一个列,以便第四列指示组件的名称。这是我要寻找的例子。

list2 <- list(a = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1), 
    d = c(2, 2, 2, 2), name = structure(c(1L, 1L, 1L, 1L), class = "factor", .Label = "a")), class = "data.frame", row.names = c(NA, 
-4L)), b = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2), name = structure(c(1L, 1L, 1L, 1L), class = "factor", .Label = "b")), class = "data.frame", row.names = c(NA, 
-4L)), d = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2), name = structure(c(1L, 1L, 1L, 1L), class = "factor", .Label = "c")), class = "data.frame", row.names = c(NA, 
-4L)))

我尝试过

list2 <- lapply(list1, function(x) cbind(list1, name = names(x[1])))

但无济于事。我了解为什么以上是不起作用的,但无法找到不同的解决方案。感谢您的帮助!

Here is an example list:

list1 <- list(a = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1), 
    d = c(2, 2, 2, 2)), class = "data.frame", row.names = c(NA, 
-4L)), b = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2)), class = "data.frame", row.names = c(NA, 
-4L)), d = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2)), class = "data.frame", row.names = c(NA, 
-4L)))

I would like to add a column inside each component of the list (i.e. a, b, d) such that the fourth column indicates the name of component. Here's an example of what I'm looking for.

list2 <- list(a = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1), 
    d = c(2, 2, 2, 2), name = structure(c(1L, 1L, 1L, 1L), class = "factor", .Label = "a")), class = "data.frame", row.names = c(NA, 
-4L)), b = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2), name = structure(c(1L, 1L, 1L, 1L), class = "factor", .Label = "b")), class = "data.frame", row.names = c(NA, 
-4L)), d = structure(list(a = c(1, 2, 4, 5), b = c(1, 1, 1, 1
), d = c(2, 2, 2, 2), name = structure(c(1L, 1L, 1L, 1L), class = "factor", .Label = "c")), class = "data.frame", row.names = c(NA, 
-4L)))

I have tried variants of

list2 <- lapply(list1, function(x) cbind(list1, name = names(x[1])))

but to no avail. I understand why the above wouldn't work, but couldn't figure out a different solution. Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赠我空喜 2025-01-31 01:06:22

我们可以使用imap

library(purrr)
library(dplyr)
imap(list1, ~ .x %>%
           mutate(name = .y))

基本rmap

Map(cbind, list1, name = names(list1))

$a
  a b d name
1 1 1 2    a
2 2 1 2    a
3 4 1 2    a
4 5 1 2    a

$b
  a b d name
1 1 1 2    b
2 2 1 2    b
3 4 1 2    b
4 5 1 2    b

$d
  a b d name
1 1 1 2    d
2 2 1 2    d
3 4 1 2    d
4 5 1 2    d

lapply 序列或名称

lapply(names(list1), function(x) cbind(list1[[x]], name = x))

We can use imap

library(purrr)
library(dplyr)
imap(list1, ~ .x %>%
           mutate(name = .y))

Or in base R with Map

Map(cbind, list1, name = names(list1))

-output

$a
  a b d name
1 1 1 2    a
2 2 1 2    a
3 4 1 2    a
4 5 1 2    a

$b
  a b d name
1 1 1 2    b
2 2 1 2    b
3 4 1 2    b
4 5 1 2    b

$d
  a b d name
1 1 1 2    d
2 2 1 2    d
3 4 1 2    d
4 5 1 2    d

With lapply, it can be done by looping over the sequence or names

lapply(names(list1), function(x) cbind(list1[[x]], name = x))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文