0
我有其被構造這樣一個數據幀:長到寬與dplyr
dd <- data.frame(round = c("round1", "round2", "round1", "round2"),
var1 = c(22, 11, 22, 11),
var2 = c(33, 44, 33, 44),
nam = c("foo", "foo", "bar", "bar"),
val = runif(4))
round var1 var2 nam val
1 round1 22 33 foo 0.32995729
2 round2 11 44 foo 0.89215038
3 round1 22 33 bar 0.09213526
4 round2 11 44 bar 0.82644723
從這個我想獲得的數據幀與兩條線,一條用於nam
每個值和變量 var1_round1
,var1_round2
,var2_round1
,var2_round2
,val_round1
, val_round2
。我會真的喜歡找到這個dplyr解決方案。
nam var1_round1 var1_round2 var2_round1 var2_round2 val_round1 val_round2
1 foo 22 11 33 44 0.32995729 0.8921504
2 bar 22 11 33 44 0.09213526 0.8264472
我能想到的最接近的將是一些有創意的方式來使用spread()
,但我似乎無法弄清楚。