R:Re-arrange 列以扩展方式来自数据框

R: Re-arrange columns from a dataframe in an extended manner

我有以下 R 数据框(header):

A   B   C   x       y
a1  b1  c1  0.68    0.43
a1  b1  c2  -0.52   0
a1  b2  c1  -0.58   -0.32
a1  b2  c2  -1.36   -0.73
a2  b1  c1  0.68    0.43
a2  b1  c2  -0.52   0
a2  b2  c1  -0.58   -0.32
a2  b2  c2  -1.36   -0.73

我想获得以下信息:

C   x_a1_b1 y_a1_b1 x_a1_b2 y_a1_b2 x_a2_b1 y_a2_b1 x_a2_b2 y_a2_b2
c1  0.68    0.43    -0.58   -0.32   0.68    0.43    -0.58   -0.32
c2  -0.52   0       -1.36   -0.73   -0.52   0       -1.36   -0.73

我试图用 tidyr::spread() 以某种方式做到这一点,但我不知道如何才能以所需的方式传播原始 table。

有什么办法可以做到这一点?

谢谢!

spread 已替换为 pivot_wider,请使用可以处理此问题的方法。

tidyr::pivot_wider(df, names_from = c(A,B), values_from = c(x, y))

#  C     x_a1_b1 x_a1_b2 x_a2_b1 x_a2_b2 y_a1_b1 y_a1_b2 y_a2_b1 y_a2_b2
#  <chr>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#1 c1       0.68  -0.580    0.68  -0.580    0.43   -0.32    0.43   -0.32
#2 c2      -0.52  -1.36    -0.52  -1.36     0      -0.73    0      -0.73

data.table 中:

library(data.table)
dcast(setDT(df), C~A+B, value.var = c('x', 'y'))

数据

df <- structure(list(A = c("a1", "a1", "a1", "a1", "a2", "a2", "a2", 
"a2"), B = c("b1", "b1", "b2", "b2", "b1", "b1", "b2", "b2"), 
C = c("c1", "c2", "c1", "c2", "c1", "c2", "c1", "c2"), x = c(0.68, 
-0.52, -0.58, -1.36, 0.68, -0.52, -0.58, -1.36), y = c(0.43, 
0, -0.32, -0.73, 0.43, 0, -0.32, -0.73)), 
class = "data.frame", row.names = c(NA, -8L))

您还可以重塑数据两次:

res1 <- reshape(df, direction = "wide", timevar = "A", idvar = c('B','C'), sep = "_")
res2 <- reshape(res1, direction = "wide", timevar = "B", idvar = "C", sep = "_")
res2
   C x_a1_b1 y_a1_b1 x_a2_b1 y_a2_b1 x_a1_b2 y_a1_b2 x_a2_b2 y_a2_b2
1 c1    0.68    0.43    0.68    0.43   -0.58   -0.32   -0.58   -0.32
2 c2   -0.52    0.00   -0.52    0.00   -1.36   -0.73   -1.36   -0.73