09 Jan

Coming to R from other programming languages

This page has the following sections:

Description of R
C/C++
Matlab
Python

Description of R

R was designed to do data analysis.  So it is heavy on graphics, statistical functionality, data structures, object manipulation, and numerical computation.

R is an interpreted language that is vector-oriented, and has lexical scoping.  Influences include functional programming and object-orientation.

Vector-oriented

The command:

exp(x)

returns the exponential of each value in x whether x contains one number or a billion.

Thinking holistically on vectors rather than looping over individual values is a very useful mindset to acquire in R.  Circle 3 of The R Inferno has more details on this.

There are not really scalars, but there are vectors of length 1.

Indexing from 1

Indexing starts at 1 and not 0.  There are at least two reasons for this.

R is meant to be human-efficient rather than machine-efficient.  Zero-based indexing is not at all intuitive to a lot of people.

R uses negative indexes.  The command:

x[-1]

returns all the values in x except for the first.

Assignment

An object is created via assignment.  The commands:

x <- 42

and

x = 42

do the same thing.  There are some subtle differences (see Circle 8.2.26 of The R Infernobut mostly you can use either at your whim.

Some people have opinions on which is the right one.  I don’t.  When coming to a new language, it is best to get into the spirit of the language rather than trying to do things in the ways you are used to.  I don’t see the assignment operator being a fundamental part of the spirit of R.

Lexical scoping

Essentially lexical scoping uses search rules for objects that are determined when the code is written rather than when the code is used.  This is almost always what is naturally desired.  Tricks exist to approximate dynamic scoping if that is needed.

Functional programming

Objects in R are mutable.  However, the standard behavior is for arguments of functions to not be changed by the function call.  That is, functions are call-by-value (though efforts are made to avoid unnecessary copying).  If you need call-by-reference, then you probably want to use an environment.

Assignment functions are an exception to calls not changing their arguments (and the syntax makes this clear).  The command:

names(x)

returns NULL if x has no names and it returns a vector of character strings if it does have names. x is not changed.

However, the assignment form of the function does change x.  The command:

names(x) <- NULL

removes the names from x (if it has any).

Functions and function calls are both objects in R.  Thus functions can take functions as arguments, functions can return functions, and computing on the language is supported.

Object-orientation

Circle 7 of The R Inferno has a brief overview of object-orientation in R.  If you are familiar with object-orientation, R’s object-orientation is probably different from what you are used to.

Typing

Typing is pretty much non-existent except when the coder explicitly checks the type and in S4 objects.  It is feasible for an object to change type within a function.

Summary

R is eclectic in terms of programming style, allowing a great deal of freedom (or chaos, depending on your point of view).  It is not dissimilar to Scala in that regard.

The Wikipedia description of R.

If you are a programmer, then among the books on R that are appropriate for you is R in a Nutshell.

C/C++

C and S (of which R is a dialect) were both born at Bell Labs.  The syntax of C was a big influence.

Resources

The Rcpp package provides a very streamlined way of combining R and C++.

Matlab

Matlab and R have quite a similar feel.

Misconceptions

Not everything in R is a matrix.  In particular a (plain) vector is not a matrix.  (Note that “vector” has conflicting meanings in R — see Circle 5.1 of The R Inferno.)

Resources

Python

Resources

 

Back to top level of Impatient R

© Copyright - Burns Statistics