Maybe all the different ways of tackling a common data wrangling task, or producing a certain kind of visualisation-please let me know if you have any ideas. The method for data.tables will return a character matrix if there are only atomic columns and any non-(numeric/logical/complex) column, applying as.vector to factors and format to other non-character columns. It dispatches to as. if its x argument is a data.table.
With that challenge solved, what other R code golf challenges would you like to see? as.matrix is a generic function in base R. This glorious code will repeatedly sample entries of m from the binomial distribution until the result is an identity matrix. To me as a statistician, all of these methods seem are bit too… deterministic. The function T: R 2 R 3 is a not a linear transformation. If this annoys you, hide it with suppressWarnings(). For the following linear transformations T : RnRn, nd a matrix A such that T(x) Ax for. In matrix(c(1, rep(0, n)), nrow = n, ncol = n) :ĭata length is not a sub-multiple or multiple of the number of rows matrix(c(1, rep(0, n)), nrow = n, ncol = n)īecause $n^2$ isn’t an exact multiple of $n+1$, R will throw a warning, which you can ignore. That is, R will repeat a vector to fit the space allocated if it isn’t already long enough. Guillaume’s own version doesn’t fit on one line but is a bit more sensible: x <- rep(0, n * n)Ĭolin Fay makes a more readable solution, taking advantage of matrix broadcasting. We can write down such a sequence and then reshape it. a one, then $n$ zeroes, another one, another $n$ zeroes and so on, finishing with the $n$th one. The three-dimensional identity matrix, for example, is
It is a diagonal matrix of ones, with all off-diagonal entries equal to zero. The identity matrixĪs a quick reminder, the identity matrix is the linear algebraic equivalent of the number 1. Taking the scenic route and learning along the way.Ĭrazy code golf, if you like. This R challenge is less about minimising your golf handicap, however, and more about showing off some features and functions in R that others might not know about.
In code golf, programmers try to write an algorithm using the shortest programme possible, often exploiting lesser-known eccentricies of each programming language. HlhIM6mSrx- Guillaume Nicoulaud February 3, 2018 This was an interesting little challenge set by Guillaume Nicoulaud on Twitter.įind as many way you can to create the identity matrix with #rstats. How many different ways are there to create an identity matrix in R? Tea & Stats Tea & Stats Data science with David Selby.