根据R中的测量条件将长数据帧旋转为宽格式

时间:2018-09-10 16:40:37

标签: r dplyr data.table reshape

我有一个这样的数据框

ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B",
            "CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B")
Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F",
          "Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F")
Measurement <- c("Length","Breadth","Width","Height",NA,NA,NA,NA,
                 "Length","Breadth","Width","Height",NA,NA,NA,NA)
Passfail <- c("Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass",
              "Pass","Pass","Fail","Fail","Pass","Pass","Pass","Pass")
Points <- c(7,5,3,4,0,0,0,0,17,15,13,14,0,0,0,0)
Average <- c(7.5,6.5,7.1,6.6,NA,NA,NA,NA,17.5,16.5,17.1,16.6,NA,NA,NA,NA)
Sigma <- c(2.5,2.5,2.1,2.6,NA,NA,NA,NA,12.5,12.5,12.1,12.6,NA,NA,NA,NA)
Tool <- c("ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2",
          "ABC_1","ABC_2","ABD_1","ABD_2","COB_1","COB_2","COB_1","COB_2")
Dose <- c(NA,NA,NA,NA,17.1,NA,NA,17.3,NA,NA,NA,NA,117.1,NA,NA,117.3)
Machine <- c("CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2",
             "CO2","CO6","CO3","CO6","CO2,CO6","CO2,CO3,CO4","CO2,CO3","CO2")

df1 <- data.frame(ID,ToolID,Step,Measurement,Passfail,Points,Average,Sigma,Tool,Dose,Machine)

我正在尝试使用这些条件将长数据帧转换为宽格式。

1)对于每个ID,如果度量值为 NO NA ,则旋转工具ID,Step,Passfail度量,点,平均值和Sigma

因此,结果列将为CCP_A_Step_A_Length_Points, CCP_A_Step_A_Length_Average, CCP_A_Step_A_Length_Sigma, CCP_A_Step_A_Length_Passfail,依此类推。

2)对于每个ID,如果测量值为 NA ,则旋转工具ID,依次调整“工具,剂量和机器”

因此,结果列将为IOT_B_Step_D__Tool, IOT_B_Step_D_Dose, IOT_B_Step_D_Machine,依此类推。

我希望所有这些都放在一个数据帧中,因此,在这种情况下,是一个具有2行的数据帧。

这是我的所需输出

  ID CCP_A_Step_A_Length_Points CCP_A_Step_A_Length_Average CCP_A_Step_A_Length_Sigma CCP_A_Step_A_Length_Passfail CCP_A_Step_A_Breadth_Points CCP_A_Step_A_Breadth_Average
   A                          7                         7.5                       2.5                         Pass                           5                          6.5
   B                         17                        17.5                      12.5                         Pass                          15                         16.5
  CCP_A_Step_A_Breadth_Sigma CCP_A_Step_A_Breadth_Passfail CCQ_A_Step_B_Width_Points CCQ_A_Step_B_Width_Average CCQ_A_Step_B_Width_Sigma CCQ_A_Step_B_Width_Passfail
                         2.5                          Pass                         3                        7.1                      2.1                        Fail
                        12.5                          Pass                        13                       17.1                     12.1                        Fail
  CCQ_A_Step_C_Height_Points CCQ_A_Step_C_Height_Average CCQ_A_Step_C_Height_Sigma CCQ_A_Step_C_Height_Passfail IOT_B_Step_D__Tool IOT_B_Step_D_Dose IOT_B_Step_D_Machine
                           4                         6.6                       2.6                         Fail              COB_1              17.1              CO2,CO6
                          14                        16.6                       2.6                         Fail              COB_1             117.1              CO2,CO6
  CCP_B_Step_D__Tool CCP_B_Step_D_Dose CCP_B_Step_D_Machine CCQ_B_Step_E__Tool CCQ_B_Step_E_Dose CCQ_B_Step_E_Machine IOT_B_Step_F__Tool CCQ_A_Step_F_Dose CCQ_A_Step_F_Machine
               COB_2                NA          CO2,CO3,CO4              COB_1              17.3              CO2,CO3              COB_2                NA                  CO2
               COB_2                NA          CO2,CO3,CO4              COB_1             117.3              CO2,CO3              COB_2                NA                  CO2

我正在尝试以这种方式进行操作,但并没有正确执行。

library(reshape2)
df3 <- dcast(df1, ID + ToolID + Step + Measurement~ Passfail+Points+Average+Sigma)

有人可以指出我正确的方向吗?我想将其应用于更大的数据集,因此快速的解决方案将对我有很大帮助。

1 个答案:

答案 0 :(得分:1)

我相信这可以为您带来想要的东西:

df_na <- df1 %>%
    filter(is.na(Measurement)) %>%
    tbl_df()
df_nna <- df1 %>%
    filter(!is.na(Measurement)) %>%
    tbl_df()

df_nna_wide = df_nna %>%
    gather(key=key, value=value, -ID, -ToolID, -Step, -Measurement) %>%
    mutate(key = paste(ToolID, Step, Measurement, key, sep='_')) %>%
    select(ID, key, value) %>%
    arrange(ID, key, value) %>%
    spread(key=key, value=value)

df_na_wide = df_na %>%
    select(-Measurement) %>%
    gather(key=key, value=value, -ID, -ToolID, -Step) %>%
    mutate(key = paste(ToolID, Step, key, sep='_')) %>%
    select(ID, key, value) %>%
    arrange(ID, key, value) %>%
    spread(key=key, value=value)

df_wide = df_nna_wide %>%
    left_join(df_na_wide, by='ID')

如果您的数据集非常大,那么data.tables可能会更适合您的需求,但是我对语法不太了解,无法以此为基础创建解决方案。