[英]R: Create New Dataframe Variable Based on List Element Name
I have a list
of 11 data frames, the name of each data frame describes its source.我有一个包含 11 个数据框的
list
,每个数据框的名称描述了它的来源。 Essentially I want to add a "source" column to each data frame in the list which contains the name of the data frame in each cell.本质上,我想向列表中的每个数据框添加一个“源”列,其中包含每个单元格中数据框的名称。
This is all so the data can be passed downstream to a CRAN package which doesn't play well with lists.这就是数据可以向下游传递到与列表不兼容的 CRAN 包的全部内容。
I've tried using lapply
and looked through some other SO answers but nothing seems to fit.我试过使用
lapply
并查看了其他一些 SO 答案,但似乎没有合适的。
Any help is greatly appreciated,任何帮助是极大的赞赏,
## Some toy data
p1 <- c("A", "B", "C", "D", "E")
p2 <- c(rnorm(5, 1.25, 1))
p3 <- c(rnorm(5, 1.25, 1))
source_name_1 <- data.frame(p1, p2, p3)
p2 <- c(rnorm(5, 1.25, 1))
p3 <- c(rnorm(5, 1.25, 1))
source_name_2 <- data.frame(p1, p2, p3)
p2 <- c(rnorm(5, 1.25, 1))
p3 <- c(rnorm(5, 1.25, 1))
source_name_3 <- data.frame(p1, p2, p3)
df_list <- list(source_name_1,
source_name_2,
source_name_3)
names(df_list) = paste0("source_name_", 1:length(df_list))
## Previous attempt based on other SO answers
df_list_2 <- lapply(names(df_list),
function(x) cbind(df_list),
source = names(df_list),
SIMPLIFY = TRUE)
#essentially I'm aiming for a 'p4' column in each df comprised of `^source_name[1-9]`
As noted by @monte in the comments you have to name the list elements.正如@monte 在评论中所指出的,您必须为列表元素命名。 Assuming they all follow a pattern with "source_name_" you could do this using
dplyr
and purrr
using your toy data假设他们都遵循“source_name_”的模式,你可以使用
dplyr
和purrr
使用你的玩具数据来做到这一点
df_list <- list(source_name_1,
source_name_2,
source_name_3)
names(df_list) = paste0("source_name_", 1:length(df_list))
library(dplyr)
library(purrr)
purrr::map2(df_list, names(df_list), ~ mutate(.x, p4 = .y))
#> $source_name_1
#> p1 p2 p3 p4
#> 1 A 0.1531752 1.5198717 source_name_1
#> 2 B 0.8299500 1.4534902 source_name_1
#> 3 C 2.1038329 0.3968661 source_name_1
#> 4 D 2.3939380 1.0487960 source_name_1
#> 5 E 1.5773872 1.8611408 source_name_1
#>
#> $source_name_2
#> p1 p2 p3 p4
#> 1 A 0.8662918 -1.014854 source_name_2
#> 2 B -1.8042179 1.339152 source_name_2
#> 3 C 1.4786439 -1.940525 source_name_2
#> 4 D 1.8360023 1.439776 source_name_2
#> 5 E 0.9648816 2.051714 source_name_2
#>
#> $source_name_3
#> p1 p2 p3 p4
#> 1 A 1.268633 1.7334884 source_name_3
#> 2 B 1.615704 1.0503553 source_name_3
#> 3 C 2.056368 1.4954794 source_name_3
#> 4 D 2.335987 1.6293595 source_name_3
#> 5 E 1.236283 0.4498371 source_name_3
Toy data玩具数据
## Some toy data
p1 <- c("A", "B", "C", "D", "E")
p2 <- c(rnorm(5, 1.25, 1))
p3 <- c(rnorm(5, 1.25, 1))
source_name_1 <- data.frame(p1, p2, p3)
p2 <- c(rnorm(5, 1.25, 1))
p3 <- c(rnorm(5, 1.25, 1))
source_name_2 <- data.frame(p1, p2, p3)
p2 <- c(rnorm(5, 1.25, 1))
p3 <- c(rnorm(5, 1.25, 1))
source_name_3 <- data.frame(p1, p2, p3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.