# Share your functions & code

#### Dason

If we wanted to save a call to unique we could store that result
Code:
diag(length(u <- unique(x)))[match(x, u),]

#### bryangoodrich

##### Probably A Mammal
While I'm at it, here is my scale01 function for scaling to the unit interval for both vectors and data frames. Preparing a neural network, this is handy (don't need it for the dummy variables above, which is also why I put that function together).

Again, nothing amazing. I'm just consolidating stuff I've found on SO, which usually just wraps an already existing function (in this case, scale).

Code:
scale01 <- function(x) UseMethod("scale01", x)

scale01.data.frame <- function(x)
{
m <- apply(x, 2, min, na.rm = TRUE)
M <- apply(x, 2, max, na.rm = TRUE)
as.data.frame(scale(x, m, (M-m)))
}

scale01.numeric <- function(x)
{
m <- min(x)
M <- max(x)
as.numeric(scale(x, m, (M-m)))
}

#### bryangoodrich

##### Probably A Mammal
If I weren't so lazy, I'd add the column naming part to Jake's solution and benchmark to see how much faster that approach is. I always forget about diag. I also don't think do do assignments within other expressions, which can really shorten code! A+ to Dason for that.

#### Dason

I also don't think do do assignments within other expressions, which can really shorten code! A+ to Dason for that.
That's not necessarily a bad thing. It's not any faster than doing the assignment in a different line of code and ultimately doing it inline makes it a little less readable in my opinion. I just wanted to keep it a one-liner since that was part of the beauty (to me) of Jake's solution.

#### Dason

BG your signature made me try to get jQuery to run in R. I gave it a good 10 minutes of effort and then gave up.