My research in social computing with an intention to mine user personalities found me in entangled in a quagmire of what are the ways to deal best with statistical data. I did hear of R on numerous occasions, but unfortunately my curiosity did not decide to get the better of me. I let R be awesome in theory and concept and never explored it practically. Until recently when I decided to test run the glitters of alleged happiness the language tainted its programmers with. Yes, so far, (and I am not far enough into it) . It is bliss.
I decided to jot down some quick noted for my beginner self and for others who might share my initial skeptical outlook to a programming language as handy as this one.
Ways in which R statistical Programming Language is different from any coding language I have worked with so far!
(Assumption: The reader has a broad idea and has worked with languages like Python and C)
It delegates variable values like a weight for graph resource ! x <- y
There are commands like help(func_name) and examples(func_name) to walk the beginners through.
Running files from the interpreter does not need an import but a source(file_name)
Vectors in R → Simply a list of values.
Creating a vector is done by c(x, y, z) wherein c → combine and x, y, z → list of values of the same type.
If however you do get adventurous and try to use different types of values for x y and z (1, TRUE, “three” , respectively) , c converts all the values to a single mode (characters) so that the vector can hold them all.
The range function in python is as simple as calling 5:9 here or maybe a seq(5,9)
The good part about the seq is not this, it is that it lets you hop onto values, for example
[sourcecode language="R"] > seq(5,9,0.5)  5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 [/sourcecode]
One thing which might come across as confusing to the everyday programmer is : Many languages start array indices at 0, but R’s vector indices start at 1…(Err Not good)..
Good thing is that the vector has a special feature of growing when values greater than indices already defined are added to them.
The arrays values/ vector values can have labels/names without having to go through the trouble of declaring a struct or multidimensional arrays 😛 Example..
[sourcecode language="R"]> ranks <- 1:3 > names(ranks) <- c("first", "second", "third") > ranks first second third 1 2 3 (yieey!)
You can now access the vector not only by the indices but also by the names of the value indices!
Magic (No more dieing online for chart plotting! :D)
The barplot function draws a bar chart with a vector’s values. We’ll make a new vector for you, and store it in the vesselsSunk variable.
> store <- c(4, 5, 1) > names(store) <- c("England", "France", "Norway") > barplot(store)
(Suggestion : Try barplot(1:1000))
Not just for a single vector, R can formulate x vs y plots too.
Stay tuned for some pretty ones after a little familiarity with the vector math..
Operations on the vectors in R are like map operations on arrays in python!
> a <- c(1, 2, 3) > a + 1  2 3 4
Plots with R ..
> x <- seq(1, 20, 0.1) > y <- sin(x) > plot(x,y)
One might like to explore the NA options for vectors and vector math operations in R.
> matrix(value,rows,cols) > matrix(a,3,4) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12
To Convert a vector to a matrix in place , use dim(vector_name) <- c(rows, cols).
The good part about accessing data in R is it resonates to that of python accessing of indices (except the difference in the starting index.)
Plotting maps (one of the best parts so far!)
I am going to be writing some quick examples and their screens, which codeschool tutorial walks us through, these are fun enough to grasp the bigger picture and getting one started.
Elevation map of a sandy beach:
> elevation <- matrix(1, 10, 10) > elevation[4, 6] <- 0 > contour(elevation) > persp(elevation, expand=0.2)
(Tip: Play around with different values of expand here)
R also includes some sample data sets to play around with. One of these is volcano, a 3D map of a dormant New Zealand volcano
> persp(volcano, expand=0.2) > image(volcano)
More to come .. 🙂
“A language that doesn’t affect the way you think about programming is not worth knowing.” ― Alan Perlis