Difference between revisions of "R Notes"
From Bioinformatics Software
Jump to navigationJump to searchm |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
=== Loading a plain CSV file into a vector === | === Loading a plain CSV file into a vector === | ||
myvector = read.csv(file="path_to_csv_file", header=FALSE) | myvector = read.csv(file="path_to_csv_file", header=FALSE) | ||
− | * You may then access each of the individual | + | * You may then access each of the individual records using myvector[row, column] |
+ | ** Access entire columns using myvector[,1], myvector[,2], ... myvector[,n] | ||
+ | ** Access entire rows using myvector[1,], myvector[2,], ... myvector[n,] | ||
− | === Scatterplot two | + | === Scatterplot two columns of a vector === |
− | In this example, myvector has 4 | + | In this example, myvector has 4 columns and we want to plot the 3rd and 4th |
plot(myvector[,3], myvector[,4], main="This is the title of the plot") | plot(myvector[,3], myvector[,4], main="This is the title of the plot") | ||
Line 33: | Line 35: | ||
* Launch R, and then paste this into the shell: | * Launch R, and then paste this into the shell: | ||
# Scott's quick R example | # Scott's quick R example | ||
− | par (mfrow=c(2,2), oma = c(0, 0, 2, 0), mar = c(5.1, 4.1, 2.1, 2.1) | + | par (mfrow=c(2,2), oma = c(0, 0, 2, 0), mar = c(5.1, 4.1, 2.1, 2.1)); <br /> |
a = rnorm(1000) | a = rnorm(1000) | ||
b = rnorm(1000) | b = rnorm(1000) | ||
− | c = | + | c = rlnorm(20000) |
− | d = rnorm( | + | d = rnorm(20000, mean=5, sd=20) <br /> |
plot(a, b, main = "A vs B", xlab="x axis", ylab="y axis", pch=20, col="blue") | plot(a, b, main = "A vs B", xlab="x axis", ylab="y axis", pch=20, col="blue") | ||
− | plot(c, d, main = "C vs D", xlab="x axis", ylab="y axis", pch=".", col=palette(rainbow( | + | plot(c, d, main = "C vs D", xlab="x axis", ylab="y axis", pch=".", col=palette(rainbow(50))) |
hist(a, main = "Histogram of A", col="darkgreen") | hist(a, main = "Histogram of A", col="darkgreen") | ||
plot(density(b), main = "Density plot of B", col="red") <br /> | plot(density(b), main = "Density plot of B", col="red") <br /> | ||
mtext("This is an example that combines multiple plots on a single page", line=0.5, outer=TRUE) | mtext("This is an example that combines multiple plots on a single page", line=0.5, outer=TRUE) |
Latest revision as of 17:38, 13 June 2008
A few very basic R examples
Contents
Loading a plain CSV file into a vector
myvector = read.csv(file="path_to_csv_file", header=FALSE)
- You may then access each of the individual records using myvector[row, column]
- Access entire columns using myvector[,1], myvector[,2], ... myvector[,n]
- Access entire rows using myvector[1,], myvector[2,], ... myvector[n,]
Scatterplot two columns of a vector
In this example, myvector has 4 columns and we want to plot the 3rd and 4th
plot(myvector[,3], myvector[,4], main="This is the title of the plot")
Plot a simple histogram
hist(myvector[,3])
- Also try plot(density(myvector[,3])) for a density-plot
Linear regression
mymodel = lm(myvector[,3] ~ myvector[,4]) summary(mymodel)
Squeeze multiple plots onto a page ( 4x4 in this example )
par(cfrow = c(4,4)) # or par(cfcol = c(4,4)) depending on whether you want row or column ordering plot(myvector[,3], myvector[,4]) plot(myvector[,3], myvector[,4]) plot(myvector[,3], myvector[,4]) plot(myvector[,3], myvector[,4])
For loops
for (i in 1:100) { do_something_useful }
A complete example
- Launch R, and then paste this into the shell:
# Scott's quick R example par (mfrow=c(2,2), oma = c(0, 0, 2, 0), mar = c(5.1, 4.1, 2.1, 2.1));
a = rnorm(1000) b = rnorm(1000) c = rlnorm(20000) d = rnorm(20000, mean=5, sd=20)
plot(a, b, main = "A vs B", xlab="x axis", ylab="y axis", pch=20, col="blue") plot(c, d, main = "C vs D", xlab="x axis", ylab="y axis", pch=".", col=palette(rainbow(50))) hist(a, main = "Histogram of A", col="darkgreen") plot(density(b), main = "Density plot of B", col="red")
mtext("This is an example that combines multiple plots on a single page", line=0.5, outer=TRUE)