Generating an ordered run of binary events in R
October 29, 2007
I needed to generate a sequence of possible binary events in an ordered fashion. For example, I needed to generate the following sequence for three binary variables: 000, 100, 010, 001, 110, 101, 011, 111
I tried to accomplish this in R using the following command:
> expand.grid( rep( list( 0:1 ), 3 ) )
Var1 Var2 Var3
1 0 0 0
2 1 0 0
3 0 1 0
4 1 1 0
5 0 0 1
6 1 0 1
7 0 1 1
8 1 1 1
but notice that the results is not ordered for my need. So here is my little function to generate what I want to do:
expand.grid.binary <- function(N){
## generates an ORDERED sequence of N-binary events
## with all the possible combinations
## expand.grid( rep( list( 0:1 ), N ) ) does not order the results desirablyf <- function(n, r){
v <- t( combn( n, r) )
ind <- cbind( row=rep( 1:nrow(v), ncol(v) ), column=as.vector(v) ) out <- matrix( 0, nrow=nrow(v), ncol=n )
out[ind] <- 1
return(out)}
out <- do.call( “rbind”, sapply( 0:N, f, n=N ) )
return(out)
}> expand.grid.binary(3)
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 1 0 0
[3,] 0 1 0
[4,] 0 0 1
[5,] 1 1 0
[6,] 1 0 1
[7,] 0 1 1
[8,] 1 1 1
The next step is how to generalize this to situations when the variables are non-binary. Any ideas?
OK, so you have an Excel file with the date of births (DOB) or any other kind of dates with day, month and year combination for many people and you want to sort them to find out the order of the anniversaries or birthdays. Sounds easy enough. Except when try to sort, it sort by DOB column and it sorts it chronologically – by year, then month and date (e.g. 30th July 1950 comes before 25th July 1990).
One way around this problem is to “standardize” the year, so that Excel effectively ignores the year. Here are more details.
- Make sure your dates are stored as Date type not text etc in Excel. (highlight the column and check format cells).
- Find out the birthdays (i.e. the year is now irrelevant) for all cases. You can do this in many ways. For example, by creating an adjacent column with the formula =DATE(2400,MONTH(A1),DAY(A1)) assuming that you stored your date of births in column A1. It does not matter if this column is formatted as either general, number or date.
- Sort the entire sheet using the birthdays column.
Notice that I have standardized all the years to year 2400 AD. Why? Because 2400 is a leap year and thus allows for 29th February. And why did I choose 2400 and not 2000 or any other leap year? Well, it is a simple precaution in case someone accidentally uses this as the true date of births!
Proof that (some) geeks are dedicated fans of Star Wars!
October 11, 2007
Normally I do not bother with any kind of fan tribute and least of all fan tributes for Star Wars that I am not personally crazy about (no arguments here please!). But I felt that this wonderful website deserves a special mention in recognition of the efforts put in. This site replays the entire original Star Wars film in ASCII (that is plain text folks)! It is absolutely bloody marvellous and IMHO more interesting than the real one.
I vote the next Star Wars film (7th or 0th or any other number George Lucas thinks will make more money) should simply be made in ASCII.
If y0u want to impress your friends even more, then simply telnet towel.blinkenlights.nl