Publications by ellbur
Rearranging definitions in R
I came up with a handy little trick for programming in R. I like to define a lot of variables all at once without worrying about what order they’re in. The goal would be something like this: K = -R2/R1 Wc = 1/(R2*C) R1 = 4.7e+3 R2 = 4.7e+3 C = 0.05e-9 But of course that doesn’t work because K can’t refer to R2 until R2 has been defined...
2157 sym R (1173 sym/6 pcs) 16 img
Packing everything into a data.frame
OK, I know I talk about R too much, but I like R, so I’m going to talk about it some more. Common situation: repeat a procedure many times; each time generates some large wadge of awful-structured data, and in the end you’d like to go back and look at it all. OK, sounds reasonably simple, just lapply(1:Num.Trials, function(N) { ... li...
2575 sym R (606 sym/5 pcs) 16 img
Matlab-style multiple assignment in R
R again! You know how in Matlab you can do? S, I = sort(M) I like that. R generic functions makes this possible. First, let’s genericize assignment. I feel like regular “=” and “ '%=%' = function(l, r, ...) UseMethod('%=%') Now the next step is a bit tricky. We need to group several variables on the left of %=% ... a, b, ... %=% foo() The...
1384 sym R (795 sym/11 pcs) 16 img
Subset views in R
I don’t know how to do this in R. So let me just say why I can’t. I wanted something akin to Boost‘s sub-matrix views, where you can have indexes map back to the original matrix, so you don’t create a new object. Sounds straightforward, just overload ‘[[‘ to subtract the offset and check the length. Alas, no dice. R zealously copies o...
1308 sym R (1766 sym/7 pcs) 16 img
R SQL-ish aggregation
I came to R from SQL. I’ve heard that packages such as reshape and plyr are quite beautiful, but to me they are somewhat non-intuitive. I’m always looking for SELECT mean(sodium) as mean, variance(sodium) as var FROM texas GROUP BY weight; I struggled with this a bit. First, it’s tempting to use tapply(), except that’s more of a one-c...
2538 sym R (2767 sym/9 pcs) 16 img
Complex-valued linear models
Someone has probably already written code to do this. But I couldn’t find it in CRAN, so here goes. Oh no lm() won’t take complex numbers! (or rather, it’ll take them, but it’ll discard the imaginary part.) Easy enough fix. Split into real. y = a*x y1 + y2*i = (a1 + a2*i) * (x1 + x2*i) y1 = a1*x1 - a2*x2 y2 = a1*x2 + a2*x1 So, if we’re ...
1243 sym R (2554 sym/7 pcs) 16 img
“Inside” Functors
So, WordPress doesn’t like RweaveHTML, so this is posted at http://strugglingthroughproblems.blogspot.com/2011/04/inside-functors.html Related To leave a comment for the author, please follow the link and comment on their blog: Struggling Through Problems » R. R-bloggers.com offers daily e-mail updates about R news and tutorials about lear...
557 sym 16 img
“Inside” Functors — Multiple Arguments
Again for HTML reasons this has been taken to http://strugglingthroughproblems.blogspot.com/2011/04/inside-functors-multiple-arguments.html Related To leave a comment for the author, please follow the link and comment on their blog: Struggling Through Problems » R. R-bloggers.com offers daily e-mail updates about R news and tutorials about l...
562 sym 16 img
"Inside" Functors — Evaluating things more than once
(The examples here work with the version of insidefunctor tagged as “v1”) I ran into an interesting problem using “inside” functors. Something is wrong in the following code (well, depending on what you thought it should do). > library(insidefunctor) > `%+.%` = fmap(`+`) > x = seq(0, 10, len = 50) > plot(x, collect(each(x) %+.% runif(1)))...
6158 sym 10 img
Corresponding
(The examples here work with the version of insidefunctor tagged as “v2”) Unfortunately I couldn’t do this cleanly outside the library. So the changes are made in insidefunctor. Levels are no longer used to “line up” eaches. So, for example, > library(insidefunctor) > `%+.%` = fmap(`+`) > `%/.%` = fmap(`/`) > x = c(1, 2, 3) > y = c(4, 5...
4235 sym