3 ms·
I disagree. I've used Matlab for 4 years and switched to Numpy/Scipy 2 years ago -- so I understand the array paradigm part. The problem with R is the inconsist
by shared4you 14y ago
I disagree. I've used Matlab for 4 years and switched to Numpy/Scipy 2 years ago -- so I understand the array paradigm part. The problem with R is the inconsistency and verbosity in syntax and semantics of the array language underlying it -- as other comments have pointed out.
Some differences in ease of use:
[1] Construct a matrix
MATLAB: y = [1 1; 2 2; 6 6];
R: y <- matrix(c(1, 2, 6, 1, 2, 6), 3)
[2] Insert a new row r = [3 3];
MATLAB: x = [y; r];
R: x <- rbind(y, c(r));
Which is intuitive and concise? ;)
- mbq 14y agoSo you also think that mean(iris(iris(:,5)==2,2)) is more intuitive than mean(iris[iris$Species=="versicolor","Sepal.Width"])? Or that constructing matrix by mapping f on 1:10 like M=[] for not_i=1:10 M[:,not_i]=f(not_i) is more concise that sapply(1:10,f)? (I know there is arrayfun, but I have never seen it used except in wow-MATLAB-is-functional blog posts)
- pseut 14y agoIronically, your "iris" object can't be a matrix the way it's written. I've had lots of students get confused about the distinction between matrices and data frames. And sapply will sometimes give (IIRC) matrices of lists with a single element if f is constructed incorrectly (requiring an explicit 'unlist' in the return statement of f). So I generally agree with you, but I think you made shared4you's point.
- mbq 14y agoNope; this entity you describe is not a matrix of lists but a single list with dimension, and sapply creates it when mapped function returns list with the same length for all iterations (which is a perfectly consistent behaviour to how sapply treats vector output). The confusion is from the fact that people think that matrices (or data frames) are R's base types -- they are not, only vectors and lists are. Matrix is just something with dim attribute, data frame is a list of equal-length elements with a proper class.
- pseut 14y agoCool, I didn't realize that lists could have dimension too. Thanks for the correction.
- psb217 14y agoOr, you could just write: M = repmat(f(1:10), n, 1); where 'n' gives the number of rows you want in 'M' and 'f' is written in proper "Matlab" style (i.e. behaves reasonably when given an array as input). Or, to be more in the spirit of linear algebra, you could write: M = ones(n,1) * f(1:10); And, if you only want one row-wise copy, you could (succinctly) write: M = f(1:10); Or, as you suggested, you could write something like: M = repmat(arrayfun(@(x)f(x), 1:10), n, 1); Or, getting more silly, and using the handy bsxfun, you could write: M = bsxfun(@times, ones(n,10), f(1:10)); If you don't feel like implementing 'f' so as to permit array inputs, you could modify this to: M = bsxfun(@times, ones(n,10), arrayfun(@(x)f(x), 1:10)); Anyhow, Matlab is very productive if you can effectively wield its powerful built-ins.
- mbq 14y agoYou didn't get my example -- it was about how to make a matrix from the results of a non-vectorised function that gets one number and returns a vector (say performs a complex simulation). The problem you've solved has an equally simple implementations in R; f(1:10) for a single copy, matrix(f(1:10),10,n) for n columns, matrix(f(1:10),n,10,byrow=T) for n rows, etc.
- hadley 14y agoThere's an important difference between matlab and R: in matlab matrices & arrays are the most important data structure, while in R data frames are the most important. There is no "array language" underlying R - working with arrays and matrices in R is usually painful, and your life is much easier if you stick with data frames. (This is something that could be fixed in R by a package, but no one has done so yet)
- pseut 14y ago> This is something that could be fixed in R by a package, but no one has done so yet I'm curious about about what you mean by this. How would a single package fix that? And does "fix" mean to make matrices easier or to make dataframes more broadly effective?
- hadley 14y agoThe problem isn't with the underlying data structures, it's with the methods that have been implemented for them. A package would fix the problem by fleshing out r with a decent set of consistently named and parameterised matrix manipulation functions.
- mbq 14y agoI don't agree; the core of array paradigm is vectorisation, as started in APL and continuing in Fortran, J, K, lush or R. The idea that it has something to do with matrix algebra is wrong -- MATrix LABoratory is simply an orthogonal story.
- hadley 14y agoHuh? I don't understand your point, e.g. I didn't mention matrix algebra.