根据单列的值将数据重新整形到不同的列

时间:2017-12-13 14:13:28

标签: r reshape reshape2 tidyverse

您好我有一个数据框如下(示例)..

time <-c( 8/11/2017 10:21,  8/10/2017 22:34,    8/16/2017 2:28, 8/14/2017 6:17, 8/11/2017 6:33, 8/15/2017 23:46,    8/10/2017 20:10,    8/14/2017 3:35, 8/11/2017 4:09, 8/15/2017 21:05,    8/11/2017 2:16, 8/10/2017 18:17,    8/13/2017 10:02,    8/13/2017 9:08, 8/13/2017 8:32, 8/13/2017 8:20, 8/13/2017 7:56)

code  <- c( 1,  3,  2,  2,  1,  2,  2,  1,  1,  2,  2,  2,  2,  2,  1,  1,  2)    

var1 <-  c( 5,  11, 16, 22, 27, 33, 38, 44, 49, 55, 60, 66, 71, 77, 66, 71, 77)    

var2 <- c(  115,    66, 71, 33, 38, 110,    115,    121,    126,    132,    104,    66, 71, 77, 115,    121,    66)    

var3 <- c(  38, 44, 49, 55, 60, 66, 71, 77, 66, 71, 77, 132,    104,    66, 71, 77, 115)    

time    code    var1    var2    var3    
8/11/2017 10:21 1   5   115 38     
8/10/2017 22:34 3   11  66  44    
8/16/2017 2:28  2   16  71  49    
8/14/2017 6:17  2   22  33  55    
8/11/2017 6:33  1   27  38  60    
8/15/2017 23:46 2   33  110 66    
8/10/2017 20:10 2   38  115 71    
8/14/2017 3:35  1   44  121 77    
8/11/2017 4:09  1   49  126 66    
8/15/2017 21:05 2   55  132 71    
8/11/2017 2:16  2   60  104 77    
8/10/2017 18:17 2   66  66  132    
8/13/2017 10:02 2   71  71  104    
8/13/2017 9:08  2   77  77  66    
8/13/2017 8:32  1   66  115 71  
8/13/2017 8:20  1   71  121 77  
8/13/2017 7:56  2   77  66  115  

我想使用列&#34;代码&#34;重新构建此数据帧。我预期的输出应如下所示。

time    code1_var1  code1_var2  code1_var3  code2_var1  code2_var2  code2_var3  code3_var1  code3_var2  code3_var3    
8/11/2017 10:21                                 
8/10/2017 22:34                                 
8/16/2017 2:28                                  
8/14/2017 6:17                                  
8/11/2017 6:33                                  
8/15/2017 23:46                                 
8/10/2017 20:10                                 
8/14/2017 3:35                                  
8/11/2017 4:09                                  
8/15/2017 21:05                                 
8/11/2017 2:16                                  
8/10/2017 18:17                                 
8/13/2017 10:02                                 
8/13/2017 9:08                                  
8/13/2017 8:32                                  
8/13/2017 8:20                                  
8/13/2017 7:56          

但是当我在R中尝试dcast功能时它给了我一个时间变量的错误。

请帮我解决这个重塑目标

注意:由于重新整形和丢失数据,结果应该有很多NA。

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用“data.table”中的dcast或来自R的reshape

假设您的矢量是在名为“d”的data.frame中收集的,请尝试以下操作:

library(data.table)
setDT(d)
x <- dcast(d, time ~ code, value.var = paste0("var", 1:3))
head(x)
#               time var1_1 var1_2 var1_3 var2_1 var2_2 var2_3 var3_1 var3_2 var3_3
# 1: 8/10/2017 18:17     NA     66     NA     NA     66     NA     NA    132     NA
# 2: 8/10/2017 20:10     NA     38     NA     NA    115     NA     NA     71     NA
# 3: 8/10/2017 22:34     NA     NA     11     NA     NA     66     NA     NA     44
# 4: 8/11/2017 10:21      5     NA     NA    115     NA     NA     38     NA     NA
# 5:  8/11/2017 2:16     NA     60     NA     NA    104     NA     NA     77     NA
# 6:  8/11/2017 4:09     49     NA     NA    126     NA     NA     66     NA     NA

OR

reshape(d, direction = "wide", idvar = "time", timevar = "code")

如果你想使用tidyverse,你需要先gather,然后创建一个新的“时间”变量,然后重塑为宽格式:

library(tidyverse)
d %>%
  gather(variable, value, starts_with("var")) %>%
  unite(key, code, variable) %>%
  spread(key, value)