what is R Language

R is a language for statistical computing. In a world of big data and scientific approaches to startup ideas, you can have the advantage of a tool in your box for statistical analysis and mathematical computations that is more powerful than a general purpose language.

Yet another language?

R is oriented to mathematics instead of general purpose computation, and has many similarities with Matlab and Octave; for example, it is accessible to people not having a computer science background. Moreover, it is open source.

The support for mathematical computations is reflected in more libraries included out of the box, for managing distributions, estimations, and inference tests. R has also a simplified syntax for mathematical expressions (e. g. the ~ operator to specify regression).

R is still strong on other core concepts, unlike Matlab: there are possibilities for classes and objects (a la Clojure), anonymous functions and closures, and named parameters. It includes a whole environment, more than a language interpreter: some functions implement graphing capabilities (farewell gnuplot) and a shell with completion and history.

The practical side of R is taken care of by bunbled utilities for reading data from files, databases (MySQL, ORacle, JDBC, ODBC), and for saving the results (e.g. the whole current workspace or single variables).

Like Matlab and Octave, R can be used for quick prototyping; after an algorithm is implemented and validated, it can be translated in C or other languages. The reasons for the translation include better performance, and the portability of the code on different machines, operating systems, or programmers.


The installation process depends on your system, but four Linux distribution is supported via repositories (with RPM and Deb packages).

People from other domains (like statistics) install R everyday, so it’s not like compiling the kernel or hunting for missing libraries. Eveything is already compiled and automated, which is a plus with respect to other niche languages which need to download different groups of JARs just because it is assumed a programmer can handle it.

R basic syntax and data structures

Before beginning, you must know that R’s assignment operator is <- and not =. = will work in many cases, but <- is more general as it can be used anywhere; it is the real equivalent of the assignment operator you have used in C-like languages.

1.> if (a = 4) 1

2.Error: unexpected ‘=’ in “if (a =”

3.> if (a <- 4) 1

4.[1] 1

Moreover, as you can already see from the code above, there is no need for semicolons.

Numbers in R are numeric (which means double actually) or integers.

01.> answer <- 42

02.> class(42)

03.[1] “numeric”

04.> answers <- c(42, 43)

05.> class(answers)

06.[1] “numeric”

07.> answers <- 42:43

08.> class(answers)

09.[1] “integer”

10.> class(as.integer(42))

11.[1] “integer”

Booleans are represented with the instances TRUE and FALSE:

1.> if (TRUE) 42

2.[1] 42

Strings are also a first-class type, with easier handling than with C libraries:

1.> message <- “hello”

2.> message

3.[1] “hello”

Strings are also a first-class type, with easier handling than with C libraries:

1.> message <- “hello”

2.> message

3.[1] “hello”

Basically, every R variable is a vector, again similarly to the case of Matlab/Octave; even scalars are just vectors of length 1. Vectors are created with the concatenation function c():

1.> my_vector <- c(1, 2, 3)

2.> my_vector

3.[1] 1 2 3


Lists, however can store variables of any type, while vectors must be homegeneous:

01.> list(42, “a”)


03.[1] 42



06.[1] “a”


08.> c(42, “a”)

09.[1] “42” “a”

Moreover, both lists and vectors can act as maps, since their keys can be strings.

Matrices and data frames are more complex structures. Matrices are the evolution of vectors in 2 dimensions, while data frames are similar structures that can contain values of different types. The difference between them is the same as for vectors and lists.

There are many more types, and useful functions bundled with the interpreter. If you come across some unknown calls, type ?entity at the prompt to load the corresponding man page; entity can be a function or a type name.

A quick example: linear regression

R can seamlessly perform linear regression, a staple problem in statistics and machine learning. The operation consists in finding the parameters of a linear combination that fits several samples of input and output variables. In our case, we want to find the parameters q and m in the model correlated_data = q + m * data.
> data <- c(1, 2, 3, 4)

01.> data

02.[1] 1 2 3 4

03.> mean(data)

04.[1] 2.5

05.> var(data)

06.[1] 1.666667

07.> sd(data)

08.[1] 1.290994

09.> correlated_data <- c(2, 4, 7, 7.5)

10.> fm<-lm(correlated_data ~ data)

11.> fm



14.lm(formula = correlated_data ~ data)



17.(Intercept) data

18. 0.25 1.95



21. [1] “coefficients” “residuals” “effects” “rank”

22. [5] “fitted.values” “assign” “qr” “df.residual”

23. [9] “xlevels” “call” “terms” “model”



26.[1] “lm”

27.> fm$coefficients

28.(Intercept) data

29. 0.25 1.95

30.> fm$coefficients[‘data’]



More information can be seen: http://www.datakeyword.blogspot.com/

website: www.raqsoft.com


About datathinker

a technical consultant on Database performance optimization, Database storage expansion, Off-database computation. personal blog at: datakeywrod, website: raqsoft
This entry was posted in FAQ, Program Language and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s