Installing and using R statistical package in Suse Linux
R is available at http://cran.r-project.org. To compile R, you need the g77 Fortran compiler, which in some versions of Suse is not found on the Suse CD, but is hidden on the DVD. You can also download it from here: http://ftp.opensuse.org/pub/opensuse/distribution/SL-10.0-OSS/inst-source\ /suse/i586/gcc-fortran-4.0.2_20050901-3.i586.rpm . An alternative version is at http://quatramaran.ens.fr/~coudert/gfortran/gfortran-linux.tar.gz. I did not test this version.
Although there are many books on using R, the procedure for installing R is
poorly documented.
The first step is to install g77 Fortran, if you don't already have it:
rpm -i gcc-fortran-4.0.2_20050901-3.i586.rpm |
configure --enable-R-shlib make |
# But we won't know that if the index files are out of date or # nonexistent. if test -n "$texindex" && test -n "$index_files"; then $verbose "Running $texindex $index_files ..." |
texindex=${MAKEINDEX:-makeindex} |
make dvi make pdf su make install make install-dvi make install-pdf |
Installing the graphical user interface
A GUI is available at http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/. As with most R packages, it is installed from within R. To install it, enter the command
install.packages("Rcmdr", dependencies=TRUE) install.packages("tcltk") |
library(Rcmdr) |
However, other GUIs for linux, such ar Rkward, are available. To install Rkward, type
./configure make su make install |
Update: Rkward now requires cmake to compile. I was unable to get the latest version of Rkward to compile in Suse 12.1.
Basic operations
This is an incomplete list of some of the basic operations
in R.
--Packages--
Operation | Command | Comments |
Starting R | R | Begins the long journey to the dark side. |
Exit | q() | Some people just type "killall R" in a different window to avoid the y/n prompt. |
Installing R packages | available.packages() | Prints a giant list that scrolls off the screen and is lost. |
Installing package by name | install.packages("Rpvm") | Installs Rpvm. |
Install all packages. | install.packages(CRAN.packages()[,1]) | Takes a long time. Many packages will not actually install because of various errors. |
Bring all existing packages up to date | update.packages() | |
Load (attach) a library package | library(MASS) | Attaches 'MASS' package. |
Show all installed packages | library() | No arguments = show all |
List packages that are attached | sessionInfo() | |
List all objects in workspace | ls() |
--Data--
Operation | Command | Comments |
Arithmetic | 4+5 | No RPN mode. |
Entering data (vector) | x <- c(1, 2, 3, 4) y <- c(5, 6, 7) z <- c(x,y) |
<- is assignment operator. c() concatenates the elements to create a vector. z now contains 1 to 7. Can also be strings or floats. If one element is a string, they all become strings. |
Plot a graph | plot( ccar~year, pch=16) | ccar = vector for y axis year = vector for x axis pch = Character to use for data points |
Combine two vectors | ff <- data.frame(yy=year, cc=ccar) | Creates a data frame named 'ff' and gives it two columns, yy and cc, containing data from vector 'year' and 'ccar'. |
Print data frame | ff | Typing the variable name prints its contents. |
Help | apropos("sort") help("sort") |
Displays man page for sort function. |
Read vector from a text file | ff <- read.table("ff.txt", header=TRUE, sep=",") | 'sep' is the character that separates consecutive numbers |
Show all data | data() data( package= "datasets") |
|
Create a sequence of numbers | seq(from=1, to=7, by=0.1) | Typically used to create a vector using '<-' |
Create a series of repeated numbers | 2:6 rep(2:6) rep(2:6, 2) rep(2:6, each=2) rep(2,5) rep(2,6) rep(2:6, rep(2,5)) |
2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 2 3 3 4 4 5 5 6 6 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 4 4 5 5 6 6 |
Test for membership | x<-rep(2:6) x %in% c(2,4) x[x %in% c(2,4)] |
x becomes 2 3 4 5 6 T F T F F 2 4 |
Test for membership | match(x, c(2,4)) match(x, c(2,4), nomatch=99) |
1 NA 2 NA NA 1 99 2 99 99 |
For loops | for(i in 1:4) print (i^2) | 1 4 9 16 |
Significant digits | options(digits=16) sqrt(2) |
1.414213562373095 |
Save data to disk file | write(x, file="x.dat") | Cannot save lists (use 'save' to save lists). |
Save lists to disk file | save(r.lm, file="list", ascii=TRUE) | Saves results in an unreadable, binary format unless 'ascii' is set to 'TRUE'. ASCII format is a long list of unlabeled stuff. |
--Statistics--
Operation | Command | Comments |
mean | mean(x) | Prints mean of whatever is in vector x |
median | median(x) | |
sort | sort(x) | |
Reverse | rev(x) | |
Standard deviation | sd(x) | |
Rnorm | x<-rnorm(10) | A normal series |
Pearson's product-moment correlation for linear relationship | cor.test(x,y) | Prints t, p-value, confidence interval |
Linear model (a k a linear regression) | r <- data.frame(x=c(1,2,3,4,5), y=c(1,4,9,16,22)) r.lm <- lm(x~y^2, r) |
First create a data frame.
For linear fitting, specify the
formula, then the dataframe. Prints
slope and intercept. Saves everything
else internally in a 'list' format. Type summary(r.lm) or names(r.lm) to get more information. The last line performs the linear regression. |
Getting actual results from linear model | names(r.lm) residuals(r.lm) |
'Names' prints a list of what is available (e.g., residuals). |
Binomial distribution | dbinom(0:4,4,0.95) | vector of quantiles, no.of trials, probability. Use range e.g. 0:4 to get all of 'em. |
Cumulative binomial distribution | pbinom(0:4,4,0.95) | q, size, prob. Also qbinom, rbinom. |
Poisson distribution | dpois(0:4,3) | x, lambda. Also ppois, qpois, rpois. |
Normal distribution | dnorm(1:10) | Also pnorm, qnorm, rnorm. |
t-test | t.test(x, var=TRUE) t.test(x,y, var=TRUE) |
Single vector vs.0 Compare 2 vectors Use var=TRUE unless variances are known to be different, otherwise it uses Welch. |
Chi square test | chisq.test(x) | x is a vector |
Anova | aa <- anova(r.lm) | r.lm is the result from a
model (e.g., a list made by lm();
see linear model above).
Type model.matrix(r.lm) to
find out what R thinks your
data look like. Type summary(aa) to get more information. |
--Graphics--
Operation | Command | Comments |
demo(graphics) | Draws several graphs and prints the appropriate commands | |
Plot a graph | plot(x,y) plot(y ~ x) plot(x,y,col=5) plot(x,y,col=1:50) |
Points only Same Set fixed point color Range of point colors |
Add stuff to existing graph | lines(x,y) | Graph must be plotted first |
Plot 3x3 array of graphs | par(mfrow=c(3,1)) | Creates empty window. Use 'plot' commands to add the actual graphs. |
Back