I'm so excited about sharing something technical with my spouse for the first time, that I'm downloading R onto my laptop myself to learn along with her, and I've bought "R in a Nutshell" from the O'Reilly book series to help me along the way. In this post, I'll be cataloguing some of my first impressions as I play with this new (to me) language.
1) R is interpreted, has a REPL, and so reminds me of when I first started learning Ruby or LISP.
2) R uses vectors a lot, which is expected for a language designed for statistical analysis. Although initially I was thinking of them like an Array, in truth they're different and in some cases a little more powerful than arrays in other programming languages. Vectors provide advanced indexing (integers, ranges, mapped selecting functions) and vector arithmetic both in a concise format.
3) functions in R are just objects. This provides for an interesting source reading technique:
> double_func = function(x,y,z){ c(x*2,y*2,z*2)}
> double_func(2,4,6)
[1] 4 8 12
> double_func
function(x,y,z){ c(x*2,y*2,z*2)}
4) R also has a class for arrays (which is defined as multidimensional vectors), and although I've dealt with multidimensional arrays before, they feel totally different in this syntax:
> a = array(data=c(1:100),dim=c(2,10,3,2))
> a
, , 1, 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 3 5 7 9 11 13 15 17 19
[2,] 2 4 6 8 10 12 14 16 18 20
, , 2, 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 21 23 25 27 29 31 33 35 37 39
[2,] 22 24 26 28 30 32 34 36 38 40
, , 3, 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 41 43 45 47 49 51 53 55 57 59
[2,] 42 44 46 48 50 52 54 56 58 60
, , 1, 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 61 63 65 67 69 71 73 75 77 79
[2,] 62 64 66 68 70 72 74 76 78 80
, , 2, 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 81 83 85 87 89 91 93 95 97 99
[2,] 82 84 86 88 90 92 94 96 98 100
, , 3, 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 3 5 7 9 11 13 15 17 19
[2,] 2 4 6 8 10 12 14 16 18 20
5) "lists" in R are a little different in that they can have named components and can hold different object types. Building a list in R almost feels like building a struct in C++ or Ruby:
> car = list(make="Honda",model="CRV",mileage=1258) > car $make [1] "Honda" $model [1] "CRV" $mileage [1] 1258 > car$make [1] "Honda" > car$model [1] "CRV"
6) Everything in R is an object, and a "class" is basically a certain type of function definition. In this way, the language feels reminiscent of javascript, where there isn't really an alternate sytax for defining global functions and object classes.
That's all for one day, don't want to cram in too much at one time. But as an overall impression, I'm getting a feeling for why the language is so valued for statistics. Data is very easy to build and manipulate en masse, and I'm sure that although I haven't gotten there yet, there are probably many built in functions for analysis to help along the way. Subscribe to my RSS feed if you want to get further updates as I dig deeper into the R language in weeks to come.

No comments:
Post a Comment