Archive

Posts Tagged ‘stupid’

A legitimate use for the stupidest variable name ever

2nd February, 2011 4 comments

The help page to make.names describes how to make a valid variable name in R:

A syntactically valid name consists of letters, numbers and the
dot or underline characters and starts with a letter or the dot
not followed by a number. Names such as ‘”.2way”’ are not valid,
and neither are the reserved words.

What it doesn’t tell you is that you can generate invalid variable names and still use them perfectly well, with a bit of trickery. To create and use such “invalid” variables, you enclose their name in backquotes.

`an invalid variable` <- 69
`an invalid variable`

You can get even more pathological, if your mind is suitably fiendish.

    
`FALSE` <- TRUE
`...` <- "ellipsis"

The last example illustrates a danger of using these silly names. Printing `...` throws an error, though you can retrieve the contents of the variable using get("...").

The main use of these silly names is for writing obfuscated code, so the first-person shooter you’re writing in R looks like just another routine data analysis to your boss. However, I did recently find a use for what is arguably the stupidest possible variable name: a single space.

Sometimes when you view a CSV file, it is easier to see things when you have a bit of white space. Adding blank columns can provide such utility. The trouble is, since these columns have no real use from a data analysis perspective, write.csv doesn’t give you the opportunity to create blank columns willy-nilly. Here’s a little hack to do such a thing. In your data frame, include a column with name ` ` and value NA.

dfr <- data.frame(x = 1:5, ` ` = NA, y = runif(5), check.names = FALSE)
write.csv(dfr, "blank column.csv", na = "")

the CSV file contains a blank column

Is this easier than right-clicking the column header in the CSV file and selecting Insert? Probably not, but doing things in code is always more satisfying.

Tags: , ,

Welcome!

20th August, 2010 Leave a comment

Anyone who knows anything about presenting data will tell you that pie charts are rubbish.  Even their idiot cousins will tell you that 3D pie charts are even worse.  That got me thinking, “how bad can it get?”.  Sure you can choose garish colour schemes and glossy reflective coatings and transparency, but that has been done before.  By almost every two-bit data-viz charlatan in existence, judging by a Google Image search.  To break new ground in awfulness, we need to enter (cue atmospheric intro)… a new dimension!  In fact, we’re going to make the pie chart travel through time (woo!) with an animation.

The MathWorks, to their eternal shame, include a 3D pie chart function, pie3.  By combining it with their rotate function, we can enter into new levels of crappiness.

First, we draw the pie chart.  This code is tweaked from the example on the pie3 help page.


colormap(hsv(6))
x = [1 0.25 3 2.5 2 1];
explode = [0 1 0 0 0 1];
h = pie3(x, explode, repmat({''}, size(x)));

3D pie chart

Now we animate it.


n = 180;
mov(n) = getframe;
for i = 1:n
   rotate(h, [20 20], 360 / n);
   mov(i) = getframe;
end

MATLAB ships with the functions movie, to play back your animation, and movie2avi to export it to video.  For more web-friendlyness, we’re going to create an animated gif using movie2gif on the FEX.


movie2gif(mov, 'pie4.gif', 'DelayTime', 0.5 / n);

And there we have it.  (Click to see it in all its animated glory.)

4D Pie Chart joy

Now, having set the bar suitably low, I promise all future mosts will be more useful than this.

Follow

Get every new post delivered to your Inbox.

Join 218 other followers