Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Basic Commands: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
Line 10: Line 10:
<ul>
<ul>
<li>[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial |Overview]]</li>
<li>[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial |Overview]]</li>
<li>[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Crash_Course | Crash Course]]</li>
<li id="current">[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Basic Commands | Basic Commands]]</li>
<li id="current">[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Basic Commands | Basic Commands]]</li>
<li>[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Practical |Practical 1]]</li>
<li>[[Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Practical |Practical 1]]</li>
Line 19: Line 20:




==Useful Commands and Functions==


==Useful online resources==
{| class="wikitable" border="1"
* [http://cran.r-project.org/doc/manuals/R-intro.html R Tutorial] from CRAN, other [http://www.cyclismo.org/tutorial/R/ Basic Tutorial]
|+ Data entry and manipulation: (x can be any of several types;  y and z are vectors)
* [http://www.mayin.org/ajayshah/KB/R/index.html R by example]
|-
* [http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html R Graphics Tutorial], [http://addictedtor.free.fr/graphiques/ R GRaph Gallery].
! Command
! Meaning
|-
| x<-c(1, 2, 3, 4)
| Create a vector of numbers
|-
| x
| Prints contents of x
|-
|  y[2:5]
| Returns 2nd to 5th elements of vector y
|-
| y[-3]  
| Returns a vector of all elements in y except for the 3rd
|-
|  y[y<10]
|  Sub-vector of all entries in y less than 10
|-
|  z[y<10]
| Sub-vector of all entries in z for which the corresponding entries in y are less than 10 (x & y must be same length)
|-
|  x<-list(y,z), x$y , x$z
| Construct of list with two vectors in it , Returns vector y, Returns vector z
|-
| x<-data.frame(y,z), x$y, x$z
| Construct of dataframe* with two vectors in it, Returns vector y, Returns vector z 
|-
| x<-factor(y)
| Converts numeric type y into a factor
|-
| is.factor(y)
|  Returns “TRUE” if y contains factors (numeric or symbolic)
|-
| is.numeric(y)
| Returns “TRUE” if y contains numeric data
|-
| is.na(y)
| Returns “TRUE” for each entry 
|}


==Crash Course in R==
Lists the different attributes of an array or dataframe 
* '''Running R'''
levels(x)=c("a", "b",…) Assign names to each factor value
** > Starting R
x<-read.table(file="inp.txt") Read a dataset from an ascii text file of data. Add
*** Find and Select 'R' within your Programs list
“header=TRUE” if the file contains descriptive headers
** > Running 'R' from command line
load("filename") Loads R data from filename
*** Once started, 'R' will open an interactive console.
save(x, "filename") Saves R object x into filename
*** You can then type 'R' commands. Remember, 'R' commands are case sensitive.
save.image("filename") Saves all current R objects into filename
*** Commands are separated either by a semi-colon (‘;’), or by a newline.
*** The vertical arrow keys on the keyboard can be used to scroll forward and backward through a command history.
** > Running 'R' From a script (strongly suggested)
*** 'R' can runs commands that have been saved in a text file (you can use your prefered text editor)
*** Commands are separated either by a semi-colon (‘;’), or by a newline.
*** For good practice, comments should be added to the script by using '#', like '# this is my comment'
*** to execute the script, type in the 'R' console source("path_to_my_script/my_script.R")
* '''Exit R'''
** type q() in 'R' console
* '''Getting help'''
** Check online tutorials (see above)
** Type help(command_name) or ?command_name like help(length), ?length
* '''Objects in R'''
** > Data Types available
*** integer
*** numeric
*** character
*** complex
*** logical
** > Data Structures available
*** Vectors
*** Matrices
*** Factors
*** Lists
*** Data Frames
** > Listing objects in memory
*** type objects() in 'R' console.
** > Clearing memory
*** type rm(object_name)
*** to clear all memory type rm(objects())
* '''Import / Export Data'''
* '''Graphics in 'R' '''
* '''Useful functions'''
** Generating regular expressions
*** seq()
*** rep()
*** 1:30
** ....
* '''Write your own functions'''

Revision as of 07:58, 5 October 2009

Fall 2009 - Synthetic Biology (MRes class)

Home        Lecture        'R' Tutorial        Resources        Literature

<html> <body> <!-- Start of StatCounter Code --> <script type="text/javascript"> var sc_project=3315864; var sc_invisible=0; var sc_partition=36; var sc_security="8bb2efcd"; </script>

<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div class="statcounter"><a class="statcounter" href="http://www.statcounter.com/"><img class="statcounter" src="http://c37.statcounter.com/3315864/0/8bb2efcd/0/" alt="blog stats" /></a></div></noscript> <!-- End of StatCounter Code -->

</body> </html>

Introduction to 'R'




Useful Commands and Functions

Data entry and manipulation: (x can be any of several types; y and z are vectors)
Command Meaning
x<-c(1, 2, 3, 4) Create a vector of numbers
x Prints contents of x
y[2:5] Returns 2nd to 5th elements of vector y
y[-3] Returns a vector of all elements in y except for the 3rd
y[y<10] Sub-vector of all entries in y less than 10
z[y<10] Sub-vector of all entries in z for which the corresponding entries in y are less than 10 (x & y must be same length)
x<-list(y,z), x$y , x$z Construct of list with two vectors in it , Returns vector y, Returns vector z
x<-data.frame(y,z), x$y, x$z Construct of dataframe* with two vectors in it, Returns vector y, Returns vector z
x<-factor(y) Converts numeric type y into a factor
is.factor(y) Returns “TRUE” if y contains factors (numeric or symbolic)
is.numeric(y) Returns “TRUE” if y contains numeric data
is.na(y) Returns “TRUE” for each entry
Lists the different attributes of an array or dataframe  

levels(x)=c("a", "b",…) Assign names to each factor value x<-read.table(file="inp.txt") Read a dataset from an ascii text file of data. Add “header=TRUE” if the file contains descriptive headers load("filename") Loads R data from filename save(x, "filename") Saves R object x into filename save.image("filename") Saves all current R objects into filename