2014-12-07 118 views
3

我具有以下的數據和代碼:變化數據幀結構中的R

mydf = structure(list(vint1 = c(5L, 5L, 5L, 1L, 4L, 9L, 2L, 1L, 7L, 
9L, 6L, 8L, 8L, 7L, 8L, 1L, 7L, 9L, 3L, 9L, 5L, 3L, 7L, 10L, 
8L, 9L, 3L, 2L, 9L, 4L, 8L, 10L, 8L, 2L, 1L, 9L, 3L, 5L, 9L, 
7L, 8L, 8L, 5L, 6L, 6L, 8L, 9L, 3L, 3L, 7L, 6L, 5L, 8L, 7L, 4L, 
5L, 3L, 2L, 5L, 7L, 7L, 9L, 1L, 2L, 3L, 3L, 8L, 8L, 2L, 10L, 
6L, 7L, 7L, 8L, 9L, 9L, 4L, 5L, 1L, 6L, 5L, 1L, 9L, 5L, 6L, 1L, 
5L, 5L, 8L, 9L, 6L, 7L, 3L, 1L, 5L, 4L, 1L, 10L, 6L, 7L), vch1 = structure(c(3L, 
2L, 5L, 2L, 3L, 5L, 3L, 2L, 2L, 2L, 4L, 1L, 5L, 1L, 2L, 3L, 4L, 
3L, 4L, 5L, 2L, 2L, 4L, 3L, 2L, 3L, 3L, 5L, 4L, 5L, 2L, 1L, 4L, 
5L, 1L, 2L, 1L, 5L, 3L, 2L, 3L, 5L, 2L, 4L, 3L, 3L, 2L, 1L, 2L, 
3L, 4L, 4L, 5L, 4L, 5L, 4L, 3L, 5L, 1L, 4L, 4L, 2L, 2L, 3L, 2L, 
1L, 1L, 4L, 3L, 1L, 3L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, 1L, 3L, 1L, 
5L, 5L, 2L, 5L, 2L, 4L, 2L, 2L, 3L, 5L, 2L, 3L, 3L, 5L, 3L, 3L, 
1L, 4L, 4L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), 
    vbin1 = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 
    2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 
    2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 
    1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 
    2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 
    2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L 
    ), .Label = c("a", "b"), class = "factor")), .Names = c("vint1", 
"vch1", "vbin1"), class = "data.frame", row.names = c(NA, -100L 
)) 

aa = aggregate(vint1~vch1+vbin1, mydf, function(x) quantile(x, c(50,66,90)/100)) 
aa 
    vch1 vbin1 vint1.50% vint1.66% vint1.90% 
1  A  a  6.50  8.62  10.00 
2  B  a  7.50  8.26  9.00 
3  C  a  4.00  6.24  8.60 
4  D  a  6.50  7.00  7.00 
5  E  a  5.50  7.88  9.00 
6  A  b  6.00  7.30  9.00 
7  B  b  5.00  7.00  8.00 
8  C  b  6.00  8.20  9.00 
9  D  b  7.00  7.00  8.00 
10 E  b  5.00  5.96  8.00 

str(aa) 
'data.frame': 10 obs. of 3 variables: 
$ vch1 : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 1 2 3 4 5 
$ vbin1: Factor w/ 2 levels "a","b": 1 1 1 1 1 2 2 2 2 2 
$ vint1: num [1:10, 1:3] 6.5 7.5 4 6.5 5.5 6 5 6 7 5 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : NULL 
    .. ..$ : chr "50%" "66%" "90%" 

正如我們所看到的,第三,第四和「AA」(總產量)的第五列不是3實列,但其中的一部分變量(如命令str(aa)所示)。我怎樣才能將它轉換成3個不同的列?謝謝你的幫助。

回答

2

問題是aggregatequantile獲得矩陣,並將其作爲單列添加。快速,後 - 事實上的解決將是

cbind(aa[,-3], vint1=aa[,3]) 

它使用cbind到矩陣的列用data.frame的列組合在一起。

根據需要,這將給你的結構

'data.frame': 10 obs. of 5 variables: 
$ vch1  : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 1 2 3 4 5 
$ vbin1 : Factor w/ 2 levels "a","b": 1 1 1 1 1 2 2 2 2 2 
$ vint1.50%: num 6.5 7.5 4 6.5 5.5 6 5 6 7 5 
$ vint1.66%: num 8.62 8.26 6.24 7 7.88 7.3 7 8.2 7 5.96 
$ vint1.90%: num 10 9 8.6 7 9 9 8 9 8 8 

+0

它簡單而完美。關於它如何工作的解釋將不勝感激。 – rnso 2014-12-08 00:21:03