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 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%"
As we can see, 3rd, 4th and 5th columns of 'aa' (aggregate output) are not 3 real columns but part of one variable (as seen on command str(aa)). How can I convert this into 3 different columns? Thanks for your help.
Upvotes: 3
Views: 262
Reputation: 206546
The problem is that aggregate
is getting a matrix back from quantile
and is adding that as a single column. A quick, after-the-fact fix would be
cbind(aa[,-3], vint1=aa[,3])
which uses cbind
to combine the columns of the matrix with the columns of a data.frame.
which will give you the structure
'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
as desired.
Upvotes: 2