Changing data structure in R

I have the following data and code:

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%"

      

As we can see, the 3rd, 4th and 5th columns "aa" (aggregate output) are not 3 real columns, but part of one variable (as seen from the str (aa) command). How can I convert this to 3 different columns? Thanks for your help.

+3


source to share


1 answer


The problem is that it aggregate

returns the matrix from quantile

and adds it as one column. A quick fix after the fact would be

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

      

which uses cbind

to concatenate matrix columns with data.frame columns.



which will give you a 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

      

optional.

+2


source







All Articles