循环输出存储为列表 [英] Loop Output Stored as List

查看:49
本文介绍了循环输出存储为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有广泛的监督数据,其中单个观察值由1级员工及其部门一直到8级组成.我与其他命令一起使用循环,以长格式生成所有员工及其下属部门的列表.这样我就可以看到各个级别的员工负责什么部门.可能有比循环更好的方法,但是效果很好.样本数据(简洁起见,达到3级):

I have wide supervisory data where a single observation consists of a level 1 employee and their department all the way down to level 8. I use a loop with other commands to produce a list all employees and the departments beneath them in long format so that I can see what departments employees are responsible for at all levels. There may be a more elegant way to do this than a loop, but it works fine. Sample data (through level 3 for succinctness):

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()绑定这些小标题的行,然后应用 distinct()命令,并我有我需要的东西.

The loop below first produces six tibbles named level1_1, level1_2, level1_3, level2_2, level2_3, level3_3. Each tibble contains an employee name, number, and the department at the same department level or below. The code then lists and binds the rows of these tibbles with ls() and bind_rows(), then applies the distinct() command, and I've got what I need.

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状".

What I'd like to do is, rather than produce an initial output of six tibbles, have the loop itself output the list. This will save me from having a huge list of tibbles in the output which, I'm told, is not very "R-like".

推荐答案

此处是修订版,将结果存储在循环中的列表中.这将包括索引 idx ,每次通过循环都会增加.之后,您可以在此列表上使用 bind_rows 以获得完整的结果.

Here is a revised version that stores the results in a list within your loop. This will include an index idx incremented each time through the loop. Afterwards, you can use bind_rows on this list to get a complete result.

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 

这篇关于循环输出存储为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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