Friday, February 12, 2016

Counting objects in a vector in R


There is an easy way to count the number of objects in a vector in R.... also known as getting your Freq on.

Suppose you create a vector called myVec with some elements in it, and you want to know the frequency (i.e., the count) of each object.

Simply convert it into a table, and then cast it as.data.frame.  The column names are the original vector name (myVec) and Freq.

       
> myVec <- c("cat", "dog", "mouse", "cat", "cat", "mouse")
> myDF <- as.data.frame(table(myVec))
> myDF
  myVec Freq
1   cat    3
2   dog    1
3 mouse    2
       
 

This also works if your vector is a column of a data frame as well.

The mysteries of the R programming language revealed.

Wednesday, November 02, 2011

USCL Power Rankings Week 10 - Final


The final regular season week of the United States Chess League has finished, and with that, I give to you the final week of the power rankings.

The only big change was Miami dropping to 11th. Also notable is that Manhattan is ranked more powerful than Boston, even though Boston is a higher seed in the playoff structure.

Here is how the final week looks.

Rank Team Score Change
1 - Chicago 1.000 0
2 - Philadelphia 0.955 0
3 - Los Angeles 0.859 0
4 - New York 0.838 0
5 - Manhattan 0.831 0
6 - Boston 0.815 +1
7 - San Francisco 0.812 +1
8 - Dallas 0.811 +2
9 - New England 0.800 0
10 - Arizona 0.777 +1
11 - Miami 0.771 -5
12 - Seattle 0.685 +1
13 - Baltimore 0.683 -1
14 - Carolina 0.651 0
15 - New Jersey 0.645 0
16 - St. Louis 0.616 0


Good luck to all the teams in the playoffs!

Wednesday, October 26, 2011

USCL Power Rankings Week 9


The penultimate week of the power rankings are here. The top 5 stay the same, although New York and Los Angeles swapped 3rd and 4th.

Miami moves up three spots at the expense of Dallas, which falls three spots. Indeed, Miami is in 6th place, only a hair behind Manhattan.

Comparing to the actual USCL standings, the power rankings are very similar, except that Manhattan is ranked more powerful than Boston, even though Boston is ahead in the standings.

As a side note, summing the power scores, the West is rated slightly more powerful than the East.

Rank Team Score Change
1 - Chicago 1.000 0
2 - Philadelphia 0.914 0
3 - Los Angeles 0.850 +1
4 - New York 0.837 -1
5 - Manhattan 0.788 0
6 - Miami 0.788 +3
7 - Boston 0.777 +1
8 - San Francisco 0.773 -2
9 - New England 0.763 +1
10 - Dallas 0.754 -3
11 - Arizona 0.741 0
12 - Baltimore 0.681 0
13 - Seattle 0.652 +1
14 - Carolina 0.648 -1
15 - New Jersey 0.644 0
16 - St. Louis 0.607 0


Friday, October 21, 2011

USCL Season Simulation 2011 (end of Week 8)

Week 8 has ended, and another set of simulations has been completed. Unlike the midweek simulations, I only needed 100,000 runs for each division to get some interesting results.

Once again, the chances that the team will make the playoffs, be in the top 2, and win the division.

Here are the results (to three significant digits).

Western Division (based on 100,000 simulations)

TeamPlayoffs Top 2Number 1
Arizona35.0%2.20%0%
Chicago100%100%100%
San Francisco71.0%6.23%0%
St. Louis0%0%0%
Seattle0.339%0%0%
Miami44.2%1.45%0%
Los Angeles99.9%90.0%0%
Dallas55.9%2.78%0%

Eastern Division (based on 100,000 simulations)

TeamPlayoffs Top 2Number 1
New England33.1%0%0%
Boston51.1%0.239%0%
New York100%92.6%41.4%
Baltimore17.8%0%0%
Manhattan90.9%16.0%0.728%
New Jersey0%0%0%
Philadelphia100%92.6%58.9%
Carolina9.95%0.792%0%

Again, if you have suggested scenarios, please comment with your requests.



USCL Power Rankings Week 8


Very little change at the top of the power rankings this week - the top four teams maintain their position. Manhattan vaults over Dallas and San Francisco to end up in fifth.

Boston and Miami also both make big jumps, while Baltimore falls four places to twelfth.

New Jersey drags itself out of the basement, and St. Louis takes New Jersey's place there.


Rank Team Score Change
1 - Chicago 1.000 0
2 - Philadelphia 0.847 0
3 - New York 0.835 0
4 - Los Angeles 0.811 0
5 - Manhattan 0.764 +2
6 - Dallas 0.734 -1
7 - San Francisco 0.715 -1
8 - Boston 0.708 +3
9 - Miami 0.697 +3
10 - New England 0.693 -1
11 - Arizona 0.685 -1
12 - Baltimore 0.671 -4
13 - Carolina 0.627 0
14 - Seattle 0.620 0
15 - New Jersey 0.565 +1
16 - St. Louis 0.564 -1


Tuesday, October 18, 2011

USCL Season Simulation Midweek 8

It is middle of week 8 in the USCL, and half the teams have played their matches this week. Therefore I ran simulations again.

The interesting thing was that when I ran 100,000 simulations for the Eastern Division, both Philadelphia and New York always made it into the playoffs. There's a chance, however unlikely, that one of those teams will not make it into the playoffs. Therefore, I ran it again, but this time with 2,500,000 simulations. In only one of those simulations, Philadelphia did not make it. So, Philly, don't coast now!

Here are the results.

Western Division (based on 100,000 simulations)

TeamPlayoffs Top 2Number 1
Arizona35.4%1.67%0%
Chicago100%99.981%99.0%
San Francisco71.1%5.67%0%
St. Louis1.14%0%0%
Seattle0.584%0%0%
Miami20.6%0.366%0%
Los Angeles99.68%81.4%1.22%
Dallas79.3%14.9%0.122%

Eastern Division (based on 2,500,000 simulations)

TeamPlayoffs Top 2Number 1
New England37.7%0.763%0%
Boston51.0%0.274%0%
New York99.989%96.0%41.6%
Baltimore38.3%0.654%0%
Manhattan66.4%7.06%0.252%
New Jersey0.0568%0%0%
Philadelphia99.99996%96.6%58.9%
Carolina10.5%0.792%0%

A scenario has been requested, and I will mention it in the comments.



Saturday, October 15, 2011

USCL Season Simulation 2011

Last year, I created a simulation of possible outcomes for the remainder of the United States Chess League season, to project the chances each team has of making the playoffs.

As there are three weeks left, I decided to run another set of simulations this year.

Once again, here are the assumptions.
  1. All teams have the same strength. Therefore, they have the same chances in each match against their opponents.
  2. The game point results of the match follow the historical distribution of USCL matches (which is here). That is, 2.5-1.5 is more common than 3-1, which is more common than 3.5-0.5, etc.
  3. If a team was tied for a playoff spot with the same match and game points, I said they both would make the playoffs -- I didn't go beyond the first tiebreaker.
I ran 100,000 simulations for both the Eastern and Western division. Here are the results.

Western Division

TeamPlayoffs Top 2Number 1
Arizona39.322%6.648%0%
Chicago100.000%99.995%99.596%
San Francisco65.377%16.010%0%
St. Louis0.905%0%0%
Seattle11.293%0.889%0%
Miami19.010%1.242%0%
Los Angeles90.735%51.724%0.381%
Dallas80.074%28.023%0.146%

Eastern Division

TeamPlayoffs Top 2Number 1
New England39.508%1.464%0%
Boston27.179%0.501%0%
New York99.982%93.277%43.467%
Baltimore38.095%1.378%0%
Manhattan67.278%9.757%0.357%
New Jersey0.045%0%0%
Philadelphia99.988%94.578%57.171%
Carolina31.810%0.792%0%

Any scenarios you want me to run? Things like: If NJ wins the remainder of their matches 4-0, what's their chance to get in the playoffs... things like that....

Let me know in the comments...


USCL Power Rankings Week 7


The power rankings are finally here this week!

No big changes at the top and the bottom. Chicago and its perfect record outpace the rest of the field, with Philadelphia and New York in second and third.

There is a logjam in the middle of the Eastern Division standings, with four teams having 3.0 match points, with their tiebreaking game points ranking them as follows: New England, Baltimore, Carolina, Boston. However, the power rankings say that the most powerful team in of these four is not New England, but Baltimore instead, and indeed, Boston is more powerful than Carolina.

Until next week!

Rank Team Score Change
1 - Chicago 1.000 0
2 - Philadelphia 0.860 0
3 - New York 0.845 0
4 - Los Angeles 0.794 +1
5 - Dallas 0.745 +2
6 - San Francisco 0.743 0
7 - Manhattan 0.739 -3
8 - Baltimore 0.707 0
9 - New England 0.700 +5
10 - Arizona 0.681 +1
11 - Boston 0.678 -2
12 - Miami 0.671 -2
13 - Carolina 0.664 -1
14 - Seattle 0.653 -1
15 - St. Louis 0.594 0
16 - New Jersey 0.555 0


Thursday, October 06, 2011

USCL Power Rankings Week 6



Because interdivisional week just completed, the power rankings are now combined into one table.

Unsurprisingly, Chicago holds the top spot, with a huge gap between them and the second place team. Indeed, teams two, three, and four are all from the Eastern Division, with Philadelphia barely edging out New York for second. Los Angeles is more powerful than San Francisco, even though the Mechanics are ahead of the Vibe in the official standings. In the battle of the basement, the Knockouts fall below the Arch Bishops.

COMBINED POWER RANKINGS

Rank Team Score Change
1 - Chicago 1.000 0
2 - Philadelphia 0.837 0
3 - New York 0.836 0
4 - Manhattan 0.789 0
5 - Los Angeles 0.774 0
6 - San Francisco 0.760 0
7 - Dallas 0.748 0
8 - Baltimore 0.706 0
9 - Boston 0.697 0
10 - Miami 0.693 0
11 - Arizona 0.678 0
12 - Carolina 0.661 0
13 - Seattle 0.661 0
14 - New England 0.657 0
15 - St. Louis 0.579 0
16 - New Jersey 0.560 0