News for hackers. Stuff that matters.
Re:The R learning curve by mbq on Friday February 08, 2013 @05:17AM (#5187129)
My personal observation (after 6 years of using and 3 years of teaching R) is that people misunderstand R by skipping the array paradigm part. If you don't get it, others' code looks like magic and either your code is slow and ugly or you seek rescue in "more-magic" wrappers like sqldf, plyr or data.table.

It's a bit like JavaScript; if you treat JS like "Java script" or "C without types", you'll suffer. Exploit its true nature and it will flourish.

Re:The R learning curve by shared4you on Friday February 08, 2013 @06:24AM (#5187290)
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? ;)

Re:The R learning curve by mbq on Friday February 08, 2013 @07:48AM (#5187561)
So 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)
Re:The R learning curve by psb217 on Friday February 08, 2013 @02:51PM (#5189751)
Or, 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.
Re:The R learning curve by mbq on Saturday February 09, 2013 @02:29PM (#5193658)
You 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.

Re:The R learning curve by pseut on Friday February 08, 2013 @11:23AM (#5188574)
Ironically, 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.
Re:The R learning curve by mbq on Saturday February 09, 2013 @02:10PM (#5193586)
Nope; 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.

Re:The R learning curve by pseut on Sunday February 10, 2013 @08:21PM (#5198771)
Cool, I didn't realize that lists could have dimension too. Thanks for the correction.
Re:The R learning curve by hadley on Friday February 08, 2013 @11:44AM (#5188706)
There'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)
Re:The R learning curve by pseut on Friday February 08, 2013 @04:55PM (#5190350)
> 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?

Re:The R learning curve by hadley on Saturday February 09, 2013 @08:50AM (#5192511)
The 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.
Re:The R learning curve by mbq on Saturday February 09, 2013 @02:50PM (#5193750)
I 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.
Re:The R learning curve by hadley on Sunday February 10, 2013 @10:31AM (#5196627)
Huh? I don't understand your point, e.g. I didn't mention matrix algebra.