R:根据列表元素名称创建新的数据框变量 [英] R: Create New Dataframe Variable Based on List Element Name

查看:45
本文介绍了R:根据列表元素名称创建新的数据框变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含11个数据帧的 list ,每个数据帧的名称描述了其来源.本质上,我想添加一个源"列表中每个数据框的一列,其中包含每个单元格中数据框的名称.

I have a list of 11 data frames, the name of each data frame describes its source. 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.

所有这些都是为了使数据可以向下游传递到CRAN程序包中,该程序包不能很好地与列表配合使用.

This is all so the data can be passed downstream to a CRAN package which doesn't play well with lists.

我尝试使用 lapply 并查看了其他一些答案,但似乎都不适合.

I've tried using lapply and looked through some other SO answers but nothing seems to fit.

非常感谢您的帮助,

  • 谢谢
## 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]`

推荐答案

正如@monte在注释中指出的那样,您必须命名列表元素.假设它们都遵循带有"source_name_"的模式.您可以使用玩具数据使用 dplyr purrr 进行此操作

As noted by @monte in the comments you have to name the list elements. Assuming they all follow a pattern with "source_name_" you could do this using dplyr and purrr using your toy data

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

玩具数据

## 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)  


这篇关于R:根据列表元素名称创建新的数据框变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆