rtips
 

 Using ess (Emacs R-project interface)

To install in a machine with no root privileges:

Download the tgz file.

tar xzf ess*.tgz

cd ess*/lisp

If your R binary is not in the /usr/bin, edit this line in ess*/lisp/ess-site.el:
(setq-default inferior-R-program-name "/software/bin/R")        ; unix systems
 
then in the lisp directory, do a "make".
 
In your .emacs file, add these two lines:
(load "/path/to/ess/ess-5.3.8/lisp/ess-site")
(ess-toggle-underscore nil)

the second line is for the annoying use of "<-" instead of underscore in emacs

====

compile R and libRmath

cd /home/avilella/9_opl/R

rm -rf /home/avilella/9_opl/R/R-2.5.1/

wget http://cran.r-project.org/src/base/R-2/R-2.5.1.tar.gz

tar xzf /home/avilella/9_opl/R/R-2.5.1.tar.gz

cd /home/avilella/9_opl/R/R-2.5.1

./configure --prefix=/home/avilella/9_opl/R --disable-linux-stackend-check --with-readline=no

--with-x=no

make

cd /home/avilella/9_opl/R/R-2.5.1/src/nmath/standalone

make shared

======

rebuilding a factor after a subset call

> str(input)
'data.frame': 44926 obs. of 8 variables:
$ ancestor_taxon_name : Factor w/ 15 levels "Amniota","Bilateria",..: 11 11 11 7 11 7 10 8 10 7 ...

mammals = subset(input,
ancestor_taxon_name %in%
c("Homo sapiens","Homo/Pan/Gorilla group","Catarrhini","Primates","Euarchontoglires","Eutheria","Theria","Mammalia"))

but the 15 levels are still there:

> str(mammals)
'data.frame': 18132 obs. of 8 variables:
$ ancestor_taxon_name : Factor w/ 15 levels "Amniota","Bilateria",..: 11 11 11 11 10 8 10 11 11 11 ...

so now we rebuild the factor:

mammals$ancestor_taxon_name = as.factor(as.character(mammals$ancestor_taxon_name))

and we have only our 8 levels:

> str(mammals)
'data.frame': 18132 obs. of 8 variables:
$ ancestor_taxon_name : Factor w/ 8 levels "Catarrhini","Euarchontoglires",..: 5 5 5 5 4 3 4 5 5 5 ...

then, for example, the boxplot will look good.

----

To monitor a mysql database, you can do something like:

library(RMySQL)

library(geneplotter)

con1 = dbConnect(MySQL(), user="anyonymous",
dbname="avilella_db", host="myhost", port=3304)

while(1) {
  input = dbGetQuery(con1,"select * from tbl_name")
  str(input)
  smoothScatter(input$value1,input$value2,nrpoints=0)
  system("sleep 60")
}

----

> In perl, I can capture parts of a regex like /(\w)\/(\d)/ to get back $1 and
> $2 containing the parts of the regex in parentheses.  Is there a parallel in
> R?
>
> Thanks,
> Sean

gsub and sub support backreferences

sub("(.):(.)", "\\2:\\1", c("1:2", "5:6")) # c("2:1", "6:5")

# and you can capture them separately using strapply in the gsubfn package
# or perform function-based substitutions using gsubfn.

# Returns: list(c("1", "2"), c("5", "6"))

library(gsubfn)
strapply(c("1:2", "5:6"), "(.):(.)", function(z,x,y) c(x,y))

# same
strapply(c("1:2", "5:6"), "(.):(.)", c, backref = -2)

########################################
 
 How can I get the values of mean and median (not only points but values too) on the boxplot. I am using boxplot function from graphics package. Following is my data set
   
  > df
  [1]  5  1  1  0  0 10 38 47  2  5  0 28  5  8 81 21 12  9  1 12  2  4 22  3
   
  > mean.val <- sapply(df,mean)
> boxplot(df,las = 1,col = "light blue")
> points(seq(df), mean.val, pch = 19)
   
I could get mean as dot symbol but i need values too? Also how to print the x-axis labels vertically instead of horizontally? Is there any other function to achieve these?
par(las = 3)
bxp <- boxplot(df,las = 1,col = "light blue")

to control label orientation and access the data displayed in the
boxplot (see `?par', `?boxplot')

use

text(x, y, 'some_text_or_number')

to add text to the boxplot. for x, use the 'number of the box' (i.e. `1'
in the example above). for y use the mean or median or whatever you wanted).

A few ideas to try:

df <- c(5, 1, 1, 0, 0, 10, 38, 47, 2, 5, 0, 28, 5, 8, 81, 21, 12, 9, 1,
12, 2, 4, 22, 3)
boxplot(df, las=1, col="light blue")
points(1, mean(df), pch=19)
text(0.75, mean(df), paste("Mean =", round(mean(df), 2)), adj=1)
text(1.25, median(df), paste("Median =", round(median(df), 2)), adj=0)

boxplot(df, las=1, col="light blue")
points(1, mean(df), pch = 19)
text(0.75, mean(df), round(mean(df), 2), adj=1)
text(1.25, median(df), round(median(df), 2), adj=0)

mydf <- data.frame(A=runif(100), B=runif(100), C=runif(100), D=runif(100))
par(mar=c(8,5,5,5))
boxplot(mydf, las=1, col="light blue")
points(seq(mydf), colMeans(mydf), pch=19)
mtext(side=1, line=2, at=seq(mydf), paste("Mean =",
round(colMeans(mydf),2)))
mtext(side=1, line=3, at=seq(mydf), paste("Median =", round(apply(mydf,
2, median),2)))

Also, see bp.example() in the following post:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/3158.html

########################################

##begin example
require(lattice)

cars.lo <- loess(dist ~ speed, cars)

print(xyplot(cars.lo$residuals+cars.lo$fitted~cars.lo$x,
             strip=FALSE,
             outer=TRUE,
             layout=c(1,2),
             ylab="",
             scales=list(y=list(relation="free",rot=0)),
             panel=function(x,y,panel.number,...){
                   if(panel.number==1){
                      panel.xyplot(x,y)
                      panel.abline(h=0)
                   }else{
                      panel.xyplot(x,y=cars.lo$y)
                      panel.xyplot(x,y,type="l")
                   }
             }))

require(grid)
trellis.focus("panel", 1, 1, clip.off=TRUE, highlight=FALSE)
grid.text("residuals", x=unit(0, "npc") + unit(-2, "lines"),rot=90)
trellis.focus("panel", 1, 2, clip.off=TRUE, highlight=FALSE)
grid.text("fitted", x=unit(0, "npc") + unit(-2, "lines"),rot=90)
## end example

########################################

flipping plot vertically

plot(density(rnorm(1000)), xaxt="n", ylim=c(1,0))
axis(side = 3)

Use xaxt = 'n' in your plot call (?par for details) to suppress plotting of
the axis and then add the axis via a call to axis().

If you do a lot of plotting, you may wish to purchase a copy of Murrell's R
GRAPHICS or V&R's MASS. At the very least, do read the relevant sections of
an Introduction to R and the Reference Manual, as I believe this sort of
thing is covered there.

########################################

concatenating data frame


result <- list()
for (i in 1:100){
    result[[i]] <- data.frame(id=sample(letters,1), value=i)
}
newDataFrame <- do.call('rbind', result)
11

########################################

merging multiple data files

# sudo apt-get install r-cran-zoo
library(zoo)
my.files <- c("file1.csv", "file2.csv", ..., "filen.csv")

#use read.zoo in the zoo package and merge.zoo (which can do a multiway merge):

#do.call("merge", lapply(my.files, read.zoo, ...any.other.read.zoo.args...))
do.call("merge", lapply(my.files, read.zoo))

#After loading zoo see:
vignette("zoo")
?read.zoo
?merge.zoo

########################################

boxplot and range of x-axis

liste1 <- c(3,4,5,3,3,4,4)
liste2 <- c(1,2,3,4,6)
png(file = "liste1.png")
boxplot(liste1)
png(file = "liste2.png")
boxplot(liste2)

boxplot(liste1,ylim=c(1,6))
boxplot(liste2,ylim=c(1,6))
boxplot(liste1,liste2)

########################################

boxplot group labels

play <- data.frame(code = letters[round(runif(100)*10+1)], income =
runif(100))
with(play, boxplot(income~code, horizontal = TRUE, boxwex = 0.1))

## The boxplot group order is based upon the factor levels for the RHS of
## the formula. By default, the levels are done with an alpha sort.
##
## So, to change the order, you can use the reorder() function, which has a
## factor method:

## Change the order of the levels in the factor:

boxplot(income~factor(code, levels = rev(levels(code))), play,
     horizontal = TRUE, boxwex = 0.1)


########################################

units of x and y in symbols

symbols(rep(0,6), rep(0,6), circles = c(5,15, 25, 35, 45, 55), inches =
FALSE, xlim = c(-60,60), ylim = c(-60, 60))

symbols(rep(0,6), rep(0,6), circles = c(5,15, 25, 35, 45, 55), inches =
FALSE, xlim = c(-60,60), ylim = c(-60, 60), asp=1)

########################################

trellis background color

trellis.par.set(col.whitebg())

########################################

trellis.par.set(col.whitebg())
mydat = data.frame(x=rnorm(100), g=c(rep(1, 50), rep(2, 50)))
bwplot(g ~ x, data=mydat, par.settings=list("box.umbrella"=list(lty=1)))

########################################

microarray-like color graph

df1 = sample(1:3,100,replace=T)
df2 = sample(0:1,100,replace=T)
df3 = sample(0:1,100,replace=T)
df4 = sample(1:4,100,replace=T)
df5 = rnorm(100,0,500)
df6 = rnorm(100,2,2)
hu.df = data.frame(df1,df2,df3,df4,df5,df6)
color = rgb(hu.df$df1,hu.df$df2,hu.df$df3,maxColorValue=3)
plot(hu.df$df6,hu.df$df4,col=color,pch=15,cex=3)

########################################

vectorize a matrix

a <- matrix(1:25,5,5)
vec = c(a[, c(1,3,5,2,4)])
df = stack(as.data.frame(a[, c(1,3,5,2,4)]))