简体   繁体   中英

Reshape Data where measure id measure variables need to be paired and then defined by multiple id variables in new column

I have the following data frame that looks like this:

  NA   X.nm.    X.A. Reaction.Type Trial Actual.Total.Seconds    RA
   1   300  3.3294          0ng      1               14.784 NaDithio
  51   350  0.1779          0ng      1               14.784 NaAsc
  81   380  0.1000          50ng     2               14.784 NaAsc
 101   400  0.0509          0ng      1               14.784 NaAsc
 151   450  0.0125          0ng      2               14.784 NaAsc
 201   500  0.0054          0ng      2               14.784 NaDithio
 251   550  0.0026          0ng      1               14.784 NaDithio
 301   600  0.0010          50ng     1               14.784 NaAsc
 351   650 -0.0001          0ng      1               14.784 NaAsc
 381   680 -0.0005          0ng      1               14.784 NaAsc

So there is a column for "Reaction.Type", "Trial", "Actual.Total.Seconds", "RA", "X.nm", "XA". Please ignore column Na.

I want to reformat my data frame so that there is a new X.nm. and XA (which are paired) column for every combo of ("Reaction.Type", "Trial", "Actual.Total.Seconds", "RA"). I want the title of each column as the combo. Every X.nm. has a corresponding XA (kind of like a coordinate point, for every X.nm, there is a XA that goes with it)

Example: Column 1 title: X.nm from 0ng, 1, 50seconds, NaAsc (this will have another column that matches the X.nm to the XA)

Column 2 title: XA from 0ng, 1, 50seconds, NaAsc

*Then do this for every combo like below, so there'd be more columns for each combo

  X.nm. from 0ng, 1, 14.784seconds, NaDithio    X.A from 0ng, 1, 50seconds, NaDithio
    300                                         3.3294   
    550                                          0.0026         

When I try to use recast() from reshape2 package, it doesn't keep the XA and X.nm pairs together.

Please help! Thank you.

library(tidyverse)
df2 <- df %>% 
   unite(var, c(Reaction.Type, Trial, Actual.Total.Seconds, RA)) %>% 
   gather(var2, val, -var, -NA.) %>% 
   unite(var3, c(var2, var)) %>% 
   spread(var3, val)

df2[,split(names(df2), c(1, rep(2:7, 2))) %>% unlist]

#    NA. X.A._0ng_1_14.784_NaAsc X.nm._0ng_1_14.784_NaAsc
# 1    1                      NA                       NA
# 2   51                  0.1779                      350
# 3   81                      NA                       NA
# 4  101                  0.0509                      400
# 5  151                      NA                       NA
# 6  201                      NA                       NA
# 7  251                      NA                       NA
# 8  301                      NA                       NA
# 9  351                 -0.0001                      650
# 10 381                 -0.0005                      680

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM