我对R很新,需要社区帮助解决我的问题。
我有这组数据,我计划堆积条形图。
date id column1 column2
2015-09-01 206 96301 91807
2015-09-02 206 87543 88082
2015-09-03 206 87636 89064
2015-09-04 206 95445 93241
2015-09-05 206 93510 93640
2015-09-06 206 153797 106226
我理解它的方式,我需要将此数据帧转换为宽格式。 我试图重新塑造它,但似乎并没有像我需要的那样重塑它。
是否可以将其重塑为此格式:
date id 2015-09-01 2015-09-02 2015-09-03 2015-09-04 2015-09-05 2015-09-06
column1 96301 87543 87636 95445 2156-01-07 153797
column2 91807 88082 89064 93241 93640 106226
答案 0 :(得分:1)
您可以使用t()
功能转换数据。
> A <- matrix(c("2015-09-01","2015-09-02","2015-09-03","2015-09-04","2015-09-05","2015-09-06"), ncol = 6)
> B <- matrix(c(96301,87543,87636,95445,93510,153797), ncol = 6)
> C <- matrix(c(91807,88082,89064,93241,93640,106226), ncol = 6)
> AB <- rbind.fill.matrix(A,B, C)
> AB <- t(AB)
> colnames(AB) <- c("Col1", "Col2", "Farm")
>
> AB
Col1 Col2 Farm
1 "2015-09-01" "96301" "91807"
2 "2015-09-02" "87543" "88082"
3 "2015-09-03" "87636" "89064"
4 "2015-09-04" "95445" "93241"
5 "2015-09-05" "93510" "93640"
6 "2015-09-06" "153797" "106226"
现在你可以使用t()函数。
> t(AB)
1 2 3 4 5 6
Col1 "2015-09-01" "2015-09-02" "2015-09-03" "2015-09-04" "2015-09-05" "2015-09-06"
Col2 "96301" "87543" "87636" "95445" "93510" "153797"
Farm "91807" "88082" "89064" "93241" "93640" "106226"
>
要创建堆积条形图,我建议您查看this page。它详细说明了如何创建多个不同的(条形)图表
希望这有助于你!