Generator functions, or function factories as they're sometimes called, can be quite useful. I was writing a function recently where some data was being processed, but I wanted to give the user the option to process it further. There are standard, straight forward ways of doing this of course, but I had a nifty idea:… Continue reading Self-destructing function that generates self-destructing functions
This one's a bit silly.
It can be really handy to create your own classes and methods in R. Most obviously it's very handy if you're creating a package for a particular procedure. Then you'd want a summary() method to quickly get some descriptive statistics, a plot() method to display results in a graphical manner, and so on.
But also outside of writing packages might a custom class and a few methods come in handy. Most frequently in my experience it's in the form of a print() method.
Creating an S3 class and methods can be really simple, while still make life (or at least coding) that little bit less cumbersome.
I claimed in a previous post that my method of selecting the number of strata when performing stratified partitioning on a uniformly distributed continuous variable, gave the optimal result, as measured by between-fold variance. All well and good, you might think, but there's one big problem. The variable i partitioned wasn't merely uniformly distributed, it was distributed entirely regularly and evenly.
This is a suggestion for an approach to creating optimal strata of a continuous variable for further partitioning, followed by a visual justification for this assessment.
The partitioning itself is done with createFolds() from the caret package, but grouping, plotting and all the rest does not depend on any external library.
I don't use append() very often, but when I do I'm a little annoyed when I rediscover that I can't reference elements by name, but have to do it by index only.
It's especially irksome as to add this feature all you'd have to do is include a couple of short and simple lines of code.
It's such a simple change that I figured I wouldn't even have to do it in an editor, I could do it all programmatically.