Jeff Brown
2010-Apr-18 23:50 UTC
[R] xtabs() of proportions, and naming a dimension (not a row)
Hi,
xtabs() creates a table of counts. I want a table of proportions -- that
is, I want to divide every vector (along a particular dimension) by its sum.
The tiny example below does that. The call to xtabs() creates a matrix
"A"
with dimensions ("x1","x2","y"). I transform
"A" using aperm() and aaply()
to get the matrix "B". The problem: "B" has dimensions
(<No name>, "x2",
"x1"). How can I give (back) the name "y" to the dimension
with no name in
the matrix "B"? (Unless I misunderstand something, dimnames()
won't do it
-- that lets me name the rows in a given dimension, but not the dimension
itself.)
> df <- data.frame (
+ x1 = ceiling( runif( 20 ) * 3 ),
+ x2 = ceiling( runif( 20 ) * 2 ),
+ y = round( runif( 20 ) )
+ )>
> attach( df )
> ( A <- xtabs( ~ y + x1 + x2 ) )
, , x2 = 1
x1
y 1 2 3
0 2 3 2
1 0 2 0
, , x2 = 2
x1
y 1 2 3
0 3 2 2
1 0 3 1
> detach(df)
> ( B <- aperm( aaply( A, 2:3, function (row) row / sum(row) ), c(3,1,2) )
)
, , x2 = 1
x1
1 2 3
0 1 0.6 1
1 0 0.4 0
, , x2 = 2
x1
1 2 3
0 1 0.4 0.6666667
1 0 0.6 0.3333333
> dimnames(B)
[[1]] # This is the line that's bothering me.
[1] "0" "1"
$x1
[1] "1" "2" "3"
$x2
[1] "1" "2"
If there's an easier way to get xtabs() to provide proportions instead of
counts, I'd like to know that, too. But whether there is or is not, I'd
like to know how to name a dimension.
Thanks a lot,
Jeff
--
View this message in context:
http://n4.nabble.com/xtabs-of-proportions-and-naming-a-dimension-not-a-row-tp2015261p2015261.html
Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2010-Apr-19 02:27 UTC
[R] xtabs() of proportions, and naming a dimension (not a row)
Hi:
Using the result of xtabs, use prop.table():
Example:
df <- data.frame (
x1 = ceiling( runif( 20 ) * 3 ),
x2 = ceiling( runif( 20 ) * 2 ),
y = round( runif( 20 ) )
)
# Get different numbers since your random seed was not specified...
(w <- with(df, xtabs(~ y + x1 + x2)))
, , x2 = 1
x1
y 1 2 3
0 1 1 1
1 1 1 1
, , x2 = 2
x1
y 1 2 3
0 1 4 2
1 3 2 2
>class(w)
# [1] "xtabs" "table"
dim(w)
# [1] 2 3 2
> prop.table(w, c(2, 3)) # Column proportions
, , x2 = 1
x1
y 1 2 3
0 0.5000000 0.5000000 0.5000000
1 0.5000000 0.5000000 0.5000000
, , x2 = 2
x1
y 1 2 3
0 0.2500000 0.6666667 0.5000000
1 0.7500000 0.3333333 0.5000000
> prop.table(w, c(1, 3)) # Row proportions
, , x2 = 1
x1
y 1 2 3
0 0.3333333 0.3333333 0.3333333
1 0.3333333 0.3333333 0.3333333
, , x2 = 2
x1
y 1 2 3
0 0.1428571 0.5714286 0.2857143
1 0.4285714 0.2857143 0.2857143
HTH,
Dennis
On Sun, Apr 18, 2010 at 4:50 PM, Jeff Brown
<dopethatwantscash@yahoo.com>wrote:
>
> Hi,
>
> xtabs() creates a table of counts. I want a table of proportions -- that
> is, I want to divide every vector (along a particular dimension) by its
> sum.
>
> The tiny example below does that. The call to xtabs() creates a matrix
"A"
> with dimensions ("x1","x2","y"). I transform
"A" using aperm() and aaply()
> to get the matrix "B". The problem: "B" has dimensions
(<No name>, "x2",
> "x1"). How can I give (back) the name "y" to the
dimension with no name in
> the matrix "B"? (Unless I misunderstand something, dimnames()
won't do it
> -- that lets me name the rows in a given dimension, but not the dimension
> itself.)
>
> > df <- data.frame (
> + x1 = ceiling( runif( 20 ) * 3 ),
> + x2 = ceiling( runif( 20 ) * 2 ),
> + y = round( runif( 20 ) )
> + )
> >
> > attach( df )
> > ( A <- xtabs( ~ y + x1 + x2 ) )
> , , x2 = 1
>
> x1
> y 1 2 3
> 0 2 3 2
> 1 0 2 0
>
> , , x2 = 2
>
> x1
> y 1 2 3
> 0 3 2 2
> 1 0 3 1
>
> > detach(df)
> > ( B <- aperm( aaply( A, 2:3, function (row) row / sum(row) ),
c(3,1,2) )
> )
> , , x2 = 1
>
> x1
> 1 2 3
> 0 1 0.6 1
> 1 0 0.4 0
>
> , , x2 = 2
>
> x1
> 1 2 3
> 0 1 0.4 0.6666667
> 1 0 0.6 0.3333333
>
> > dimnames(B)
> [[1]] # This is the line
> that's bothering me.
> [1] "0" "1"
>
> $x1
> [1] "1" "2" "3"
>
> $x2
> [1] "1" "2"
>
> If there's an easier way to get xtabs() to provide proportions instead
of
> counts, I'd like to know that, too. But whether there is or is not,
I'd
> like to know how to name a dimension.
>
> Thanks a lot,
> Jeff
> --
> View this message in context:
>
http://n4.nabble.com/xtabs-of-proportions-and-naming-a-dimension-not-a-row-tp2015261p2015261.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
David Winsemius
2010-Apr-19 02:47 UTC
[R] xtabs() of proportions, and naming a dimension (not a row)
On Apr 18, 2010, at 7:50 PM, Jeff Brown wrote:> > Hi, > > xtabs() creates a table of counts. I want a table of proportions -- > that > is, I want to divide every vector (along a particular dimension) by > its sum.?prop.table> > The tiny example below does that. The call to xtabs() creates a > matrix "A" > with dimensions ("x1","x2","y"). I transform "A" using aperm() and > aaply() > to get the matrix "B". The problem: "B" has dimensions (<No name>, > "x2", > "x1"). How can I give (back) the name "y" to the dimension with no > name in > the matrix "B"? (Unless I misunderstand something, dimnames() won't > do it > -- that lets me name the rows in a given dimension, but not the > dimension > itself.) > >> df <- data.frame ( > + x1 = ceiling( runif( 20 ) * 3 ), > + x2 = ceiling( runif( 20 ) * 2 ), > + y = round( runif( 20 ) ) > + ) >> >> attach( df ) >> ( A <- xtabs( ~ y + x1 + x2 ) ) > , , x2 = 1 > > x1 > y 1 2 3 > 0 2 3 2 > 1 0 2 0 > > , , x2 = 2 > > x1 > y 1 2 3 > 0 3 2 2 > 1 0 3 1 > >> detach(df) >> ( B <- aperm( aaply( A, 2:3, function (row) row / sum(row) ), >> c(3,1,2) ) )Would have been polite to include: require(plyr)> , , x2 = 1 > > x1 > 1 2 3 > 0 1 0.6 1 > 1 0 0.4 0 > > , , x2 = 2 > > x1 > 1 2 3 > 0 1 0.4 0.6666667 > 1 0 0.6 0.3333333 > >> dimnames(B) > [[1]] # This is the line that's bothering me. > [1] "0" "1" > > $x1 > [1] "1" "2" "3" > > $x2 > [1] "1" "2" > > If there's an easier way to get xtabs() to provide proportions > instead of > counts, I'd like to know that, too. But whether there is or is not, > I'd > like to know how to name a dimension.> dimnames(B) <- list(x0 = dimnames(B)[[1]], x4 = dimnames(B)[[2]], x5=dimnames(B)[[3]]) > B , , x5 = 1 x4 x0 1 2 3 0 1 0.5 0.6 1 0 0.5 0.4 , , x5 = 2 x4 x0 1 2 3 0 0.5714286 0 0 1 0.4285714 1 1> > Thanks a lot, > Jeff > -- > View this message in context: http://n4.nabble.com/xtabs-of-proportions-and-naming-a-dimension-not-a-row-tp2015261p2015261.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Jeff Brown
2010-Apr-19 03:13 UTC
[R] xtabs() of proportions, and naming a dimension (not a row)
Thanks a lot, David and Dennis! Also, your suggestions for how I could have better stated my question are duly noted, and appreciated. -- View this message in context: http://n4.nabble.com/xtabs-of-proportions-and-naming-a-dimension-not-a-row-tp2015261p2015380.html Sent from the R help mailing list archive at Nabble.com.