Install rpy
aptitude install python-rpy aptitude install r-base
Quick Intro to rpy
Plot with rpy
from rpy import * x = range(0, 10) y = [ 2*i for i in x ] r.plot_default(x, y)
or
from rpy import *
import math
x = range(0, 10)
y1 = [ 2*i for i in x ]
y2 = [ math.log(i+1) for i in x ]
r.plot_default(x, y1, col="blue", type="o")
r.points(x, y2, col="red", type="o")
xlabels = [ "#%d" % (i,) for i in x ]
r.axis(1, at = x, label=xlabels, lwd=1, cex_axis=1.15)
r.title("My Plot")
R vs rpy
Description |
R language |
rpy (R Python Bindings) |
Start command line |
R |
python |
|
|
from rpy import * |
Quit Command line |
q() |
CTRL + D |
Make x = (1, 2, ..., 20) |
x <- 1:20 |
x = range(1, 21) |
A weight vector of standard deviations. |
w <- 1 + sqrt(x)/2 |
w= 1+sqrt(x)/2 |
Make a data frame of two columns, x and y, and look at it |
dummy <- data.frame(x=x, y= x + rnorm(x)*w) |
dummy = r.data_frame(x=x, y=x+w*r.rnorm(x)) |
|
dummy |
dummy |
Fit a simple linear regression of y on x and look at the analysis |
fm <- lm(y ~ x, data=dummy) |
fm=r.lm(r("y~x"),data=dummy) |
|
summary(fm) |
r.summary(fm) |
Create a matrix |
B <- matrix(c(1:12),4,3) |
b=r.matrix(range(1,13),nrow=4,ncol=3) |
Import R modules |
require(scatterplot3d) |
r.require('scatterplot3d') |
scatterplot3d(x = c(col(B)),y=c(row(B)),z=(c(B))) |
r.scatterplot3d(x = r.c(r.col(b)), y=r.c(r.row(b)) , z=r.c(b)) |
|
scatterplot3d(x = c(col(B)),y=c(row(B)),z=(c(B)),type = "h", lwd = 5, pch = " ") |
r.scatterplot3d(x = r.c(r.col(b)), y=r.c(r.row(b)) , z=r.c(b),type = "h", lwd = 5, pch = " ") |
output to ps or png
outfile = "somefile.ps" r.postscript(outfile, paper='letter')
or
outfile = "somefile.png" r.bitmap(outfile, res=200)
import rpy
import csv
import time
from rpy import *
set_default_mode(NO_CONVERSION)
all=[]
reader=csv.reader(open("WMI.csv"))
for row in reader:
all.append(row)
df=r.as_data_frame(all)r.print_(df[5])
c..1287081.Ontario.Inc....2L9....101....R.R...1....Burford..ON.N0E.1A0... 1 1287081 Ontario Inc 2 2L9 3 101 4 R.R. #1 5 Burford ON N0E 1A0 6 Canada 7 N0E 1A0 8 Canada 9 10 Trailers 11 06/22/98 00:00:00 <Robj object at 0x2b7944e5d550>