如何在每行的一系列列中返回第一个非NULL值?而第二个非NULL值?

时间:2017-02-07 06:38:57

标签: r hierarchy hierarchical-data

我有以下组织数据:

EmployeeID <- c(10:15)
Job.Title <- c("Program Manager", "Development Manager", "Developer" , "Developer", "Developer", "Summer Intern")
Level.1 <- c(1,1,1,1,1,1)
Level.2 <- c(2,2,2,2,2,2)
Level.3 <- c("",10,10,10,10,10)
Level.4 <- c("","",11,11,11,11)
Level.5 <- c("","","","","",12)
Level.6 <- c("","","","","","")
Pay.Type <- c("Salary", "Salary", "Salary", "Salary", "Salary", "Hourly")
acme = data.frame(EmployeeID, Job.Title, Level.1, Level.2, Level.3, Level.4, Level.5, Level.6, Pay.Type)

acme

  EmployeeID           Job.Title Level.1 Level.2 Level.3 Level.4 Level.5 Level.6 Pay.Type
1         10     Program Manager       1       2                                   Salary
2         11 Development Manager       1       2      10                           Salary
3         12           Developer       1       2      10      11                   Salary
4         13           Developer       1       2      10      11                   Salary
5         14           Developer       1       2      10      11                   Salary
6         15       Summer Intern       1       2      10      11      12           Hourly

对于每一行,我需要识别Level.1到Level.6的第一个非NULL值,从Level.6开始,然后是Level.5,然后是Level.4,依此类推。我还需要在同一模式中识别第二个非Null值。每行的标识值需要放在新列中,因此最终表格如下所示:

  EmployeeID           Job.Title Level.1 Level.2 Level.3 Level.4 Level.5 Level.6 Pay.Type Supervisor Manager
1         10     Program Manager       1       2                                   Salary          2       1
2         11 Development Manager       1       2      10                           Salary         10       2
3         12           Developer       1       2      10      11                   Salary         11      10
4         13           Developer       1       2      10      11                   Salary         11      10
5         14           Developer       1       2      10      11                   Salary         11      10
6         15       Summer Intern       1       2      10      11      12           Hourly         12      11

4 个答案:

答案 0 :(得分:4)

我们可以逐行使用apply并获取所有非空的索引,并选择第一个和第二个值分别获得两列。

acme[, c("Supervisor", "Manager")] <- t(apply(acme[, 8:3], 1, 
                      function(x) c(x[which(x != "")[1]], x[which(x != "")[2]])))

acme

#  EmployeeID           Job.Title Level.1 Level.2 Level.3 Level.4 Level.5 Level.6 Pay.Type Supervisor Manager
#1         10     Program Manager       1       2                                   Salary          2       1
#2         11 Development Manager       1       2      10                           Salary         10       2
#3         12           Developer       1       2      10      11                   Salary         11      10
#4         13           Developer       1       2      10      11                   Salary         11      10
#5         14           Developer       1       2      10      11                   Salary         11      10
#6         15       Summer Intern       1       2      10      11      12           Hourly         12      11

修改

如果有很多列,我们需要找到开始和结束列的索引。我们可以将grep用于相同的

mincol <- min(grep("Level", colnames(acme)))
maxcol <- max(grep("Level", colnames(acme)))

 acme[, c("Supervisor", "Manager")] <- t(apply(acme[, maxcol:mincol], 1, 
                      function(x) c(x[which(x != "")[1]], x[which(x != "")[2]])))

应该有效。

如果我们只需要Supervisor,我们可以忽略第二部分。

acme[, "Supervisor"] <- t(apply(acme[, maxcol:mincol], 1, 
                            function(x) x[which(x != "")[1]]))

答案 1 :(得分:3)

这是data.table“单行”:

library(data.table)
setDT(acme)[melt(acme, measure.vars = patterns("Level.\\d"))[value != ""][
  order(variable), .(Supervisor = value[.N], Manager = value[.N - 1]), by = EmployeeID], 
  on = "EmployeeID"][]

   EmployeeID           Job.Title Level.1 Level.2 Level.3 Level.4 Level.5 Level.6 Pay.Type Supervisor
#1:         10     Program Manager       1       2                                   Salary          2
#2:         11 Development Manager       1       2      10                           Salary         10
#3:         12           Developer       1       2      10      11                   Salary         11
#4:         13           Developer       1       2      10      11                   Salary         11
#5:         14           Developer       1       2      10      11                   Salary         11
#6:         15       Summer Intern       1       2      10      11      12           Hourly         12
   Manager
#1:       1
#2:       2
#3:      10
#4:      10
#5:      10
#6:      11

工作原理

  1. data.frame被强制为data.table
  2. 并按顺序从长格式转换为
  3. 删除级别为""的所有行。
  4. 现在,数据按级别编号排序(隐含地表示为Level.1Level.2等。)
  5. 为每位员工提取最后一个(主管)和倒数第二个值(经理),创建一个由三列组成的中间结果。
  6. 最后,中间结果将加入acme以附加新列
  7. 并打印
  8. 注意: melt()会发出一条警告消息,指出并非所有级别列都具有相同的数据类型。这是由于在"" data.frame的定义中将整数值与字符(acme)混合而引起的。最好使用NA代替""。顺便说一句:在这种情况下,可以使用na.rm = FALSE melt()

    来简化代码

    注意:步骤4中简单的alaphybetical排序最多可以使用9个级别(Level.1Level.9)。如果有更多级别,则必须提取级别编号并强制转换为整数。

答案 2 :(得分:3)

dplyrtidyr依赖于数据重塑的解决方案。

library(tidyverse)
acme %>%
  gather('level', 'value', starts_with('Level.')) %>%
  group_by(EmployeeID) %>%
  filter(value != '') %>%
  summarise(Supervisor = last(value),
            Manager = nth(value, -2)) %>%
  left_join(acme)

答案 3 :(得分:2)

我们可以使用max.col执行此操作。找到&#39; Level&#39;的索引。列(&#39; i1&#39;),转换&#39; acme&#39;的子集基于&#39; i1&#39;到matrix!=""),应用max.col并获取last TRUE值的列索引,减去1以获得倒数第二个TRUE值(&#39; i3&#39;),使用行/列索引提取元素并创建&#39; Supervisor&#39;和经理&#39;列

i1 <- grep("Level\\.\\d+", names(acme))
i2 <- max.col(acme[i1]!="", "last")
i3 <- i2-1
acme$Supervisor <- acme[i1][cbind(1:nrow(acme), i2)]
acme$Manager <-  acme[i1][cbind(1:nrow(acme), i3)]
acme
#  EmployeeID           Job.Title Level.1 Level.2 Level.3 Level.4 Level.5 Level.6 Pay.Type Supervisor Manager
#1         10     Program Manager       1       2                                   Salary          2       1
#2         11 Development Manager       1       2      10                           Salary         10       2
#3         12           Developer       1       2      10      11                   Salary         11      10
#4         13           Developer       1       2      10      11                   Salary         11      10
#5         14           Developer       1       2      10      11                   Salary         11      10
#6         15       Summer Intern       1       2      10      11      12           Hourly         12      11

注意:此解决方案非常简单有效,无需任何不必要的重塑