我已经创建了一个示例数据框来帮助说明我的问题。想象一下这个设置,但很多行和列。它现在看起来像这样:
> row1 <- rep(LETTERS[1:3], each=5)
> row2 <- sample(1:100, 15)
> df <- data.frame(row1,row2)
> t(df)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
row1 "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C"
row2 "33" "56" "44" "70" " 2" "15" "34" " 3" "24" "21" "68" "88" "27" "79" "31"
我需要让它看起来像:
> t(df2)
[,1] [,2] [,3] [,4] [,5]
row1 "A" "A" "A" "A" "A"
row2 "33" "56" "44" "70" " 2"
row3 "B" "B" "B" "B" "B"
row4 "15" "34" " 3" "24" "21"
row5 "C" "C" "C" "C" "C"
row6 "68" "88" "27" "79" "31"
本质上,有一组行标题具有唯一的四个代码标识符(示例中的LETTERS)。我尝试使用不同的选择&#39;来自dplyr的呼叫以及&#39;子集&#39;称为R.原生。
任何协助指出我正确的方向将不胜感激。非常感谢提前。
答案 0 :(得分:4)
您可以使用split
按row1
分割数据,然后使用cbind
将其重新绑定在一起。请注意,转置会将您的所有值转换为character
。
t(do.call(cbind, split(df, df$row1)))
## 1 2 3 4 5
## A.row1 "A" "A" "A" "A" "A"
## A.row2 "26" "59" "21" "85" "74"
## B.row1 "B" "B" "B" "B" "B"
## B.row2 "78" "60" "24" "53" " 3"
## C.row1 "C" "C" "C" "C" "C"
## C.row2 "52" "34" "66" "98" "41"