在多个数据框上嵌套功能

时间:2018-07-01 09:45:24

标签: r nested nested-lists tidyr

我有几个具有公司与顾问关系的数据框,每个感兴趣的年份都有一个。

例如,2015年数据框如下所示。让我们将其称为Advisors2015(然后我也有Advisors2014,advisors2013,advisants2012等):

> advisors2015
       [,1]   [,2]                    [,3]           [,4]                        
colnam "Mark" "Company.name"          "Company.ID"   "Advisor.Name"              
row1   "1"    "VOLKSWAGEN AG"         "DE2070000543" "PRICEWATERHOUSECOOPERS"    
row2   " "    "VOLKSWAGEN AG"         "DE2070000543" "PWC DEUTSCHE REVISION"     
row3   " "    "VOLKSWAGEN AG"         "DE2070000543" "C&L TREUARBEIT REVISION"   
row4   "2"    "ROYAL DUTCH SHELL PLC" "GB04366849"   "LLOYDS TSB REGISTRARS"     
row4   "2"    "ROYAL DUTCH SHELL PLC" "GB04366849"   "LLOYDS TSB REGISTRARS"     
row5   " "    "ROYAL DUTCH SHELL PLC" "GB04366849"   "PRICEWATERHOUSECOOPERS"
row6   " "    "ROYAL DUTCH SHELL PLC" "GB04366849"   "KPMG ACCOUNTANTS NV"       
row7   " "    "ROYAL DUTCH SHELL PLC" "GB04366849"   "ERNST & YOUNG"             
row8   "3"    "BP PLC"                "GB00102498"   "CAPITA ASSET SERVICES"     
  

这是2014年:

> advisors2014
         [,1]   [,2]                    [,3]           [,4]                        
colnam "Mark" "Company.name"          "Company.ID"   "Advisor.Name"              
row1   "1"    "VOLKSWAGEN AG"         "DE2070000543" "PRICEWATERHOUSECOOPERS"    
row2   " "    "VOLKSWAGEN AG"         "DE2070000543" "PWC DEUTSCHE REVISION"     
row3   " "    "VOLKSWAGEN AG"         "DE2070000543" "C&L TREUARBEIT REVISION"   
row4   "2"    "ROYAL DUTCH SHELL PLC" "GB04366849"   "LLOYDS TSB REGISTRARS"     
row5   " "    "ROYAL DUTCH SHELL PLC" "GB04366849"   "PRICEWATERHOUSECOOPERS"
row6   "3"    "BP PLC"                "GB00102498"   "CAPITA ASSET SERVICES"     
row7   "4"    "COCACOLA"              "GB111222333"  " "                         

如您所见,每个公司可能有一个或多个顾问。当然,它们也可能随时间而变化:今年(这意味着在此数据帧中),大众汽车有3名顾问,但明年可能只有一名顾问,或者用其他一些顾问代替。

为了跟踪所有这些变化,我希望有一个数据框,用于保存每个公司/年度观察的顾问列表。

我知道我们可以使用nest函数来完成此操作,但是据我了解,它是根据相同数据框中的列创建列表的,而多个数据帧,例如10个。

有人可以帮我解决这个问题吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

如果您要查找单个数据框,其中的列为yearCompany.name,并且该列包含一个列表,则每个列表的元素是一个数据框,其中包含该年份和Company的行.name然后:

library(dplyr)
library(purrr)
library(tidyr)

ls(pattern = "^advisors\\d{4}$", envir = .GlobalEnv) %>%
  mget(envir = .GlobalEnv) %>%
  map_dfr(as.data.frame.matrix, .id = "year") %>%
  mutate(year = sub("advisors", "", year) %>% as.numeric) %>%
  nest(-c(year, Company.name))

给予:

# A tibble: 6 x 3
   year Company.name          data                
  <dbl> <fct>                 <list>              
1 2015. VOLKSWAGEN AG         <data.frame [3 x 3]>
2 2015. ROYAL DUTCH SHELL PLC <data.frame [4 x 3]>
3 2015. BP PLC                <data.frame [1 x 3]>
4 2016. VOLKSWAGEN AG         <data.frame [3 x 3]>
5 2016. ROYAL DUTCH SHELL PLC <data.frame [4 x 3]>
6 2016. BP PLC                <data.frame [1 x 3]>

或者如果您只想要一个长格式的数据框,则省略nest行。

注意

我们假定输入为:

advisors2015 <- 
structure(list(Mark = c(1L, NA, NA, 2L, NA, NA, NA, 3L), 
Company.name = structure(c(3L, 
3L, 3L, 2L, 2L, 2L, 2L, 1L), .Label = c("BP PLC", "ROYAL DUTCH SHELL PLC", 
"VOLKSWAGEN AG"), class = "factor"), Company.ID = structure(c(1L, 
1L, 1L, 3L, 3L, 3L, 3L, 2L), .Label = c("DE2070000543", "GB00102498", 
"GB04366849"), class = "factor"), Advisor.Name = structure(c(6L, 
8L, 1L, 5L, 7L, 4L, 3L, 2L), .Label = c("C&L TREUARBEIT REVISION", 
"CAPITA ASSET SERVICES", "ERNST & YOUNG", "KPMG ACCOUNTANTS NV", 
"LLOYDS TSB REGISTRARS", "PRICEWATERHOUSECOOPERS", "PRICEWATERHOUSECOOPERS LLP", 
"PWC DEUTSCHE REVISION"), class = "factor")), 
class = "data.frame", row.names = c(NA, -8L))

advisors2015 <- advisors2016 <- as.table(as.matrix(advisors2015))