r语言 - 循环输出存储为列表



我有广泛的监督数据,其中单个观察包括1级员工及其部门一直到8级。我使用循环和其他命令以长格式生成所有员工及其下属部门的列表,这样我就可以看到各级员工负责的部门。可能有一种比循环更优雅的方法来实现这一点,但它工作得很好。样本数据(为了简洁,一直到第3级):

data <- tibble(LV1_Employee_Name = "Chuck", LV1_Employee_Nbr = "1", LV1_Department = "Tha Boss", LV1_Department_Nbr = "90",
LV2_Employee_Name = c("Alex", "Alex", "Paul", "Paul", "Jennifer", "Jennifer"), LV2_Employee_Nbr = c("2", "2", "3", "3", "4", "4"), LV2_Department = c("Leadership", "Leadership", "Finance", "Finance", "Philanthropy", "Philanthropy"), LV2_Department_Nbr = c("91", "91", "92", "92",  "93", "93"),
LV3_Employee_Name = c("Dan", "Wendy", "Sarah", "Monique", "Miguel", "Brandon"), LV3_Employee_Nbr = c("2", "2", "3", "3", "4", "4"), LV3_Department = c("Analytics", "Pop Health", "Acounting", "Investments", "Yacht Aquisitions", "Golf Junkets"), LV3_Department_Nbr = c("94", "95", "96", "97",  "98", "99"))

下面的循环首先产生六个标题,分别为level1_1, level1_2, level1_3, level2_2, level2_3, level3_3。每个标签都包含一个员工姓名、编号以及同一部门级别或更低级别的部门。然后代码将这些标题的行与ls()bind_rows()列出并绑定,然后应用distinct()命令,然后我得到了我需要的。

first_department <- 1
data_colnames <- c("Employee", "Employee_Id", "Department", "Department_Number")
for(i in 1:3){
for(k in first_department:3){

assign(paste0("level", i, "_", k), setNames(distinct(as_tibble(c(data[ ,paste0("LV", i, "_", "Employee_Name")], data[ ,paste0("LV", i, "_", "Employee_Nbr")],
data[ ,paste0("LV", k, "_", "Department")],  data[ ,paste0("LV", k, "_", "Department_Nbr")]))), 
data_colnames))

}
first_department = first_department + 1
}
employees_departments <- distinct(bind_rows(mget(ls(pattern = "^level")))) %>%
filter(is.na(Department) == FALSE)
rm(list = ls(pattern = "^level"))

我想做的是,而不是产生六个标题的初始输出,让循环本身输出列表。这将使我避免在输出中有一个巨大的标题列表,我被告知,这不是很"r"。

下面是修改后的版本,它将结果存储在循环中的列表中。这将包括索引idx,每次在循环中递增。之后,您可以在此列表中使用bind_rows来获得完整的结果。

library(tidyverse)
idx <- 1
first_department <- 1
data_colnames <- c("Employee", "Employee_Id", "Department", "Department_Number")
data_lst <- list()
for(i in 1:3){
for(k in first_department:3){
data_lst[[idx]] <- setNames(
distinct(as_tibble(
c(data[ ,paste0("LV", i, "_", "Employee_Name")], 
data[ ,paste0("LV", i, "_", "Employee_Nbr")],
data[ ,paste0("LV", k, "_", "Department")],  
data[ ,paste0("LV", k, "_", "Department_Nbr")]))),
data_colnames)
idx <- idx + 1
}
first_department = first_department + 1
}
distinct(bind_rows(data_lst)) %>%
filter(!is.na(Department))

Employee Employee_Id Department        Department_Number
<chr>    <chr>       <chr>             <chr>            
1 Chuck    1           Tha Boss          90               
2 Chuck    1           Leadership        91               
3 Chuck    1           Finance           92               
4 Chuck    1           Philanthropy      93               
5 Chuck    1           Analytics         94               
6 Chuck    1           Pop Health        95               
7 Chuck    1           Acounting         96               
8 Chuck    1           Investments       97               
9 Chuck    1           Yacht Aquisitions 98               
10 Chuck    1           Golf Junkets      99               
11 Alex     2           Leadership        91               
12 Paul     3           Finance           92               
13 Jennifer 4           Philanthropy      93               
14 Alex     2           Analytics         94               
15 Alex     2           Pop Health        95               
16 Paul     3           Acounting         96               
17 Paul     3           Investments       97               
18 Jennifer 4           Yacht Aquisitions 98               
19 Jennifer 4           Golf Junkets      99               
20 Dan      2           Analytics         94               
21 Wendy    2           Pop Health        95               
22 Sarah    3           Acounting         96               
23 Monique  3           Investments       97               
24 Miguel   4           Yacht Aquisitions 98               
25 Brandon  4           Golf Junkets      99 

相关内容

  • 没有找到相关文章