简体   繁体   English

R:根据列表元素名称创建新的数据框变量

[英]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,任何帮助是极大的赞赏,

  • Thanks谢谢
## 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_”的模式,你可以使用dplyrpurrr使用你的玩具数据来做到这一点

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.

相关问题 如何根据 R 中的另一个 dataframe 中的 id 列表创建新变量 - how to create a new variable based on a list of ids in another dataframe in R 根据 R 中的数据集名称创建一个新变量 - create a new variable based on the name of the dataset in R 如何创建一个列表,使列表的每个元素都是基于 R 中的矩阵的 dataframe 的行名称 - How to create a list such that each element of the list is the row name of a dataframe based on a matrix in R R循环根据数据框名称创建多个新列 - R loop to create multiple new columns based on dataframe name 如何基于R中列表的元素创建一个包含四列的新数据框 - How to make a new dataframe with four columns based on an element of a list in R 根据列数在 dataframe 列表中创建新变量 - Create new variable in dataframe list based on number of columns R:如何循环从数据框中选择基于名称的变量,并为每个变量创建一个包含第一个列均值的新变量? - R: How to loop over a name-based selection of variables from a dataframe and for each create a new variable containing the column mean of the first? 根据r中的名称列表有效地为新变量分配名称 - Efficiently assigning a name to a new variable based on a list of names in r 如何在R中为数据框列表中的每个元素创建新变量,其数据框的名称及其值等于元素的位置 - How to create in R new variable for each element in a list of data frames with the name of data frame and its value equal to position of the element 根据包含R中数据框的元素列表的名称创建列 - Create a column based on the name of the element list that contain the data frame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM