BEGINNER'S GUIDE TO 'r' LANGUAGE


Introduction

This is an introduction to R (“GNU S”), a language and environment for statistical computing and graphics. R is similar to the award-winning S system, which was developed at Bell Laboratories by John Chambers et al. It provides a wide variety of statistical and graphical techniques (linear and nonlinear modelling, statistical tests, time series analysis, classification, clustering, ...).
This manual provides information on data types, programming elements, statistical modelling and graphics.
This manual is for R, version 3.2.2 (2015-08-14).
Copyright © 1990 W. N. Venables
Copyright © 1992 W. N. Venables & D. M. Smith
Copyright © 1997 R. Gentleman & R. Ihaka
Copyright © 1997, 1998 M. Maechler
Copyright © 1999–2015 R Core Team
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the R Core Team.

R Basics 
The R & BioConductor manual provides a general introduction to the usage of the R environment and its basic command syntax.


Code Editers For R
Several excellent code editors are available that provide functionalities like R syntax highlighting, auto code indenting and utilities to send code/functions to the R console.

1.Basic code editors provided by Rguis
2.RStudio: GUI-based IDE for R
3.Vim-R-Tmux: R working environment based on vim and tmux
4.Emacs (ESS add-on package)
5.gedit and Rgedit
6.RKWard
7.Eclipse
8.Tinn-R
            9.Notepad++ (NppToR)

Programming in R using Vim or Emacs 


Programming in R using RStudio



sample()
print(sample(1:3))
print(sample(1:3, size=3, replace=FALSE))  # same as previous line
print(sample(c(2,5,3), size=4, replace=TRUE)
print(sample(1:2, size=10, prob=c(1,3), replace=TRUE))
[1] 3 1 2
[1] 2 1 3
[1] 2 5 2 2
[1] 2 2 2 1 1 2 2 2 1 2

 Overview

By default sample() randomly reorders the elements passed as the first argument. This means that the default size is the size of the passed array.replace=TRUE makes sure that no element occurs twice.
The last line uses a weighed random distribution instead of a uniform one. One out of four numbers are 1, the out of four are 3.

Arguments

size
This is the size of the returned list. If replace is disabled size must be no bigger than the length of the first argument.
replace
If this is true a sample may contain an element several times while another element might not occur at all.
print(sample(c(2,5,3), size=3, replace=FALSE))
print(sample(c(2,5,3), size=3, replace=TRUE))
[1] 2 3 5
[1] 2 3 3
Allowing some elements to occur more than once lets you get a sample longer than the first argument.
prob
barplot(table(sample(1:3, size=1000, replace=TRUE, prob=c(.30,.60,.10
 



The prob=c(.30,.60,.10) cause 30% ones, 60% twos and 10% threes. The numbers don't have to add up to 1 - they don't in the example at the top of the page.
Table sums up the individual items in the 1000-element list.


How To Run The CODE

I find it easiest to use RStudio. You can also paste the code in a normal R console or let R run a source file. However these approaches are a bit less fail-safe.
Using RStudio
Download and install RStudio.
Open RStudio and do this:
1.Click on the menu: File -> New -> R Script
2.Paste the code in the new source code area
3.Click the "Source" button above the code area:




You can also use the console in RStudio. If you click "Run" instead of "Source" user input might not work properly.
You can use the R documentation like this: help(function.name).
Using the R console
Running the r program on the command line or elsewhere will start the console. You can paste your code there.
Problems with this approach
This will generally work if you use source("filename.r") to run your code. If you paste the code some of it might be read as user input.
Running a source file with R
You can run a source file like this: r -f filename.r.
R also provides a lot of other command line arguments.
Problems with this approach
I have not managed to get user input to work here.


Finding Help
Reference list on R programming (selection)


Previous
Next Post »