Difference between revisions of "R Notes"

From Bioinformatics Software
Jump to navigationJump to search
Line 1: Line 1:
 
== A few very basic R examples ==
 
== A few very basic R examples ==
 +
 +
__TOC__
  
 
=== Loading a plain CSV file into a vector ===
 
=== Loading a plain CSV file into a vector ===

Revision as of 17:00, 13 June 2008

A few very basic R examples

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 components using myvector[,1], myvector[,2], ... myvector[,n]

Scatterplot two components of a vector

In this example, myvector has 4 components 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 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 }