Statistically Significant

December 13, 2009

R Trick (Nerd Alert)

Filed under: Math — Hoxie @ 11:38 am

At work this past week, I finally figured out how to do something in R that I’ve been wanting to do for a while now. The problem has to do with creating and assigning variables using a loop. I figured out how to do mass variable creation using a loop this summer: to assign a random value between 0 and 1 to variables z1, z2, …, z10 using a loop, you can use:
for (index in 1:10){
assign(paste(“z”, index, sep=”") , runif(1))
}
(I can’t think of a place I’d choose to do this over just creating a 10-element vector of random numbers using “z <- runif(10)” and accessing the elements of the vector as I needed them, but there are places where it’s helpful to create a few variables using the loop index.)

But what if you have variables z1, z2, …, z10 created already and you want to work with each of them separately? I didn’t know how to create a “current variable” inside of the loop. Trying something similar fails:
for (index in 1:10){
assign(“current_value” , paste(“z”, index, sep=”"))
cat(current_value, “\n”)
# do complicated stuff
}
This will print “z1″, “z2″, … “z10″ instead of the values contained in our ten z variables.

The solution I discovered this past week:
for (index in 1:10){
eval(parse(text=paste(“current_value <- z”, index, sep=”")))
cat(current_value, “\n”)
}
For index==1, for example, this prepares the string “current_value <- z1″ and then submits it to R for evaluation, so now the variable current_value contains the value of z1. Nice.

12/07/09 – 12/13/09

Filed under: Life — Hoxie @ 11:22 am

Another week in the life of Hoxie.

I made some big moves in the world of graduate school applications this week, submitting applications to UNC Chapel Hill and Harvard. Berkeley should be submitted later today, which will make me more than halfway done with getting apps out the door. Hoping to finish all remaining apps later this week before heading home for the holidays!

The big event of the week was the arrival of a good friend of mine from high school, Caitlin. She’s applying to med school right now and had interviews at Tufts and Harvard. Rather than fork over the big bucks for a Boston hotel, she spent a few nights on an air mattress in the dining room. She and I were both pretty busy during the week, but yesterday we headed down to Harvard Square and had some fun Christmas shopping and getting some food. (Sadly, the only picture that we took was a candid shot on her iPhone in my pantry to appease her mom.)

While out Christmas shopping, I picked up a great game for myself that I’ve meaning to acquire for a while now: Set. Julia introduced me to Set last year, and we usually play a few games whenever we get together. Now that I own it, maybe she won’t trounce me quite as badly. Set is the Official Hoxie Holiday Gift Recommendation for Less Than $20 of 2009. Pick it up for the smart person in your life today.

And speaking of Julia, I’m heading down to her new apartment for a Chanukah party later this evening. I don’t think I’ve been to a Chanukah party since about sixth grade, when I got all of my gelt stolen and swore I’d never spin another dreidel, but I think tonight should be a little bit better.

Going back to Fort Lauderdale on Saturday, can’t wait!

Next Page »