Home
27.01.2011 22:07 Alter: 2 yrs
Kategorie: Computer
Von: Mohammad Esad-Djou

R language: statistical computation and graphics

An Indroduction, by: Mohammad Esad-Djou


curve(sin, -2*pi, 2*pi, col="green")

R is a system for statistical computation and graphics. It provides, among other things, a programming language, high level graphics, interfaces to other languages and debugging facilities.

The R environment

R is an integrated suite of software facilities for data manipulation, calculation and graphical display. It includes

  • an effective data handling and storage facility,
  • a suite of operators for calculations on arrays, in particular matrices,
  • a large, coherent, integrated collection of intermediate tools for data analysis,
  • graphical facilities for data analysis and display either on-screen or on hardcopy, and
  • a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

The term "environment" is intended to characterize it as a fully planned and coherent system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the case with other data analysis software[1].

Documentation

Current Version: 2.12.1 (December 2010)

The following manuals for R were created on Debian Linux and may differ from the manuals for Mac or Windows on platform-specific pages, but most parts will be identical for all platforms. The correct versions of the manuals for each platform are part of the respective R installations. Here they can be downloaded as PDF files or directly browsed as HTML:

  • An Introduction to R is based on the former "Notes on R", gives an introduction to the language and how to use R for doing statistical analysis and graphics. [browse HTML | download PDF ]
  • A draft of The R language definition documents the language per se. That is, the objects that it works on, and the details of the expression evaluation process, which are useful to know when programming R functions. [browse HTML | download PDF ]
  • Writing R Extensions covers how to create your own packages, write R help files, and the foreign language (C, C++, FORTRAN ...) interfaces. [browse HTML | download PDF ]
  • R Data Import/Export describes the import and export facilities available either in R itself or via packages which are available from CRAN. [browse HTML | download PDF ]
  • R Installation and Administration [browse HTML | download PDF ]
  • R Internals: a guide to the internal structures of R and coding standards for the core team working on R itself. [browse HTML | download PDF ]
  • The R Reference Index: contains all help files of the R standard and recommended packages in printable form. [download PDF, 16MB, approx. 3100 pages]

Translations of manuals into other languages than English are available from the contributed documentation section (only a few translations are available).

The latex or texinfo sources of the latest version of these documents are contained in every R source distribution (in the subdirectory <kbd>doc/manual</kbd> of the extracted archive). Older versions of the manual can be found in the respective archives of the R sources. The HTML versions of the manuals are also part of most R installations (accessible using function <code>help.start()</code>).

You can find here a short introduction and some examples.

helloWorld.r

We start with first script with standard helloWorld's program. The following program presents a simple FOR-loop. n is input variable via keyboard and program is going to write “Hello world” for n times. You can use # for comments:

# usage in R: helloWorld(n)
helloWorld <- function(n)

{
 for (i in 1:n){
    cat("Hello world!\n")
   }
}

You can after initialisation of function “helloWorld”.  For example simply type in R-Console

>helloWorld(3)

And the result seems as follows:

Hello world!

Hello world!

Hello world!

In first example, we used keyboard as input, but R can use a file as standard input too. The following command read one csv-file and writes to array “s”:

 

s <- read.csv(filename)

Graphic

You can create your first diagram with using commands par and plot. par can be used to set or query graphical parameters. Parameters can be set by specifying them as arguments to par in tag = value form, or by passing them as a list of tagged values.

par(mfrow=c(1,1))

plot is the generic function for plotting of R objects.

plot(dnorm,-5,5,ann=FALSE)

 

 

You can use function title, to add text and colour to your plot.

title(main ="Normal (0,1)",col.main="red", xlab="x", ylab="f(x)",col.lab="blue",

 cex.main = 1.5,font.main = 3, cex.lab=1.3, font.lab=3)

 

curve is another helpful function for drawing given function or expressions:

curve(sin, -2*pi, 2*pi, col="green")