[R] Two selections from Bag A

David Winsemius dwinsemius at comcast.net
Sun Aug 26 10:24:21 CEST 2012


On Aug 25, 2012, at 5:37 PM, darnold wrote:

> All, I am looking at an example in Aliaga's Interactive Statistics.  
> Bag A has
> the following vouchers.
>
> BagA <- c(-1000,10,10,10,10,10,10,
>          10,20,20,20,20,20,20,30,
>          30,40,40,50,60)
>
> Bag B has the following vouchers.
>
> BagB <- c(10,20,30,30,40,40,50,50,
>          50,50,50,50,60,60,60,60,
>          60,60,60,1000)
>
> Two values are selected (from BagA or BagB) without replacement. In  
> Table
> 1.1 on page 54 of the third edition, she lists all "Possible two  
> values
> selected" in columns one and two,

?unique
?expand.grid or ?combn

Perhaps spliting names from the tabulation below>


> the "Average of the two selected values"

?mean

> in column three and "BAG A Numbers of way of selecting the two  
> values" in
> column four, and "BAG B Number of ways of selecting the two values" in
> column five.
>
> Here are the first few rows:
>
> -1000       -1000     -1000     0     0

Why is that combination even listed?

> -1000             10      -495      7     0
> -1000             20     -490       6     0
> -1000            30      -485       2     0
> -1000            40      -480       2     0
> -1000            50      -475       1     0
> -1000            60      -470       1     0
> -1000       1000             0       0     0

What are the rules for listing a combination?


>      10           10           10     21     0
>      10            20           15    42      1

I can get that value if choosing just from BagA, but if the  
possibilities are for either bag to be selected, then an additional  
value would arise because 10 and 20 are in BagB.

?table

?apply
?paste


> ...
>
> She then condenses the data in Table 1.2 on page 55, the first column
> holding "Average of the two selected values', the second column  
> holding "BAG
> A Number of ways of selecting the two values," and third column  
> holding "BAG
> B Number of ways of selecting the two values."
>
> Here are a few sample rows:
>
> -1000     0     0
>  -495     7     0
>  -490     6     0
> ....
>
> Can anyone help show me an efficient way of creating these two tables?

table( apply( combn(BagA,2), 2, function(x) paste( sort(x), sep=".",  
collapse=".") ) )

table( apply( combn(BagB,2), 2, function(x) paste( sort(x), sep=".",  
collapse=".") ) )


You should be able to take it from this illustration of how to get the  
BagA results:

cbind( do.call( rbind ,
             sapply(names(table( apply( combn(BagA,2), 2, function(x)  
paste( sort(x), sep=".", collapse=".") ) ) ) , strsplit, split= "\ 
\.") ),  # first 2 columns
        table( apply( combn(BagA,2), 2, function(x) paste( sort(x),  
sep=".", collapse=".") ) ) )
          [,1]    [,2] [,3]
-1000.10 "-1000" "10" "7"
-1000.20 "-1000" "20" "6"
-1000.30 "-1000" "30" "2"
-1000.40 "-1000" "40" "2"
-1000.50 "-1000" "50" "1"
-1000.60 "-1000" "60" "1"
10.10    "10"    "10" "21"
10.20    "10"    "20" "42"
10.30    "10"    "30" "14"
10.40    "10"    "40" "14"
10.50    "10"    "50" "7"
10.60    "10"    "60" "7"
20.20    "20"    "20" "15"
20.30    "20"    "30" "12"
20.40    "20"    "40" "12"
20.50    "20"    "50" "6"
20.60    "20"    "60" "6"
30.30    "30"    "30" "1"
30.40    "30"    "40" "4"
30.50    "30"    "50" "2"
30.60    "30"    "60" "2"
40.40    "40"    "40" "1"
40.50    "40"    "50" "2"
40.60    "40"    "60" "2"
50.60    "50"    "60" "1"


-- 
David.


David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list