Introduction to r
Download
1 / 64

Introduction to R - PowerPoint PPT Presentation


  • 58 Views
  • Uploaded on

Introduction to R. Carol Bult The Jackson Laboratory Functional Genomics (BMB550) Spring 2011. R Project for Statistical Computing. http://www.r-project.org/. What is R?. R is an integrated suite of software facilities for data manipulation, calculation, and graphical display. It includes:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to R' - alvaro


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to r

Introduction to R

Carol Bult

The Jackson Laboratory

Functional Genomics (BMB550)

Spring 2011


R Project for Statistical Computing

http://www.r-project.org/


What is r
What is R?

  • R is an integrated suite of software facilities for data manipulation, calculation, and graphical display. It includes:

    • An effective data handling and storage facility,

    • A suite of operators for calculations on arrays (matrices),

    • A large coherent integrated collection of intermediate tools for data analysis,

    • Graphical facilities for data analysis and display either on-screen or on hardcopy, and

    • A well-developed, simple, and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

From: http://www.r-project.org/index.html


R is available for free under the

GNU General Public License

http://www.fsf.org/licensing/licenses/quick-guide-gplv3.html



BioConductor

BioConductor is a project to generate code for analyzing genomic data. Most of the components of BioConductor are written in R.

http://www.bioconductor.org/


Installing r
Installing R

Guidance on downloading and installing R can be found on the R home page under the FAQ link.


Cran mirrors
CRAN Mirrors

CRAN stands for “Comprehensive R Archive Network”

Each CRAN mirror should all have the same versions of the R software and various analysis packages used in R.

Usually you pick a CRAN mirror that is closest to you geographically to use as the source for downloading your R code and packages.


What is an r package
What is an R “package”?

  • An R package is data analysis code that is written so that it can be executed in the R environment

  • Several basic statistics packages are supplied with the basic R distribution

  • Additional packages can be obtained from the CRAN sites

    • You will likely need to download additional packages that deal specifically with microarray analysis for this course


Let s get started
Let’s Get Started…

http://www.cloud.target.maine.edu/


Xming is an X server for Windows

http://sourceforge.net/projects/xming/


Use PuTTY to access the cloud server

1. Enter the IP address and select SSH

2. Under SSH select Tunnels and make sure the Enable X11 forwarding is selected


With the Xming X server running and the X11 forwarding set in PuTTY, you should now be able to launch windows from PuTTY.

To test if you X server is running properly….

At the $ prompt in your PuTTY window, type matlab and see if the matlab window launches.

From within R, type help.start() from the > prompt and see if a web browser window launches.



You can invoke on-line help with the sessionhelp.start() command.

Note: R is a case sensitive language!

What happens if you type Help.Start() at the command prompt?


At the prompt (>) type the command , sessionlibrary(), to see which packages are available in your R installation.

A library is a location where R goes to find packages.

What is listed for you may differ from what is shown here.


You might find this R reference card helpful….Use it as a stub to create your own reference card!

http://www.psych.upenn.edu/~baron/refcard.pdf





In this series of commands, we load the BOD (Biological Oxygen Demand) data set and then print out the data to the screen.


Data frames in r
Data Frames in R Oxygen Demand) data set and then print out the data to the screen.

  • The data sets in R are objects called data frames

    • You can think of a data frame as a table where the columns are variables and rows are observations


Types of Objects in R Oxygen Demand) data set and then print out the data to the screen.

  • Vector

    • One-dimensional array of arbitrary length. All members of a vector must be of the same type (numeric, alpha, etc.)

  • Matrix

    • Two –dimensional array with an arbitrary number of rows and columns. All elements in a matrix must be of the same type.

  • Array

    • Similar to a matrix but with an arbitrary dimension

  • Data frame

    • Organized similar to a matrix except that each column can include its own type of data. Not all columns in a data frame need to contain the same type of data.

  • Function

    • A type of R object that performs a specific operation. R contains many built-in functions.

  • List

    • An arbitrary collection of R objects


Create the bod data frame from scratch
Create the BOD Data Frame from Scratch Oxygen Demand) data set and then print out the data to the screen.

1. Create a vector object for time using the c() function.

2. Create a vector object for demand.

3. Use the data.frame() function to create the data frame object


Reading in data using the scan() command Oxygen Demand) data set and then print out the data to the screen.

If you have a simple text file of values (no headers), you can read those data in using the scan() function.

In this example, there is a text file called scandata.txt in the working directory/folder that I pointed R to when I first started up the program.

To read in structured data files…i.e., ones with multiple columns of data and headers, etc. use the read.table() command instead of scan().


Reading in Data from Files Oxygen Demand) data set and then print out the data to the screen.

Use the read.table() function to read in data from a text file and use it as a data frame in R.

Other input functions for R include:

read.csv()

read.delim()

If you had data in an Excel spreadsheet, how could you import it into R?

<- can be used as an assignment operator, but = should also work.


How would you find out more about the c() function? Oxygen Demand) data set and then print out the data to the screen.

Find the details behind the data in the BOD data frame.


Writing Output to Files Oxygen Demand) data set and then print out the data to the screen.

Want to save the data in your data frame as a text file on your computer?

Use the write.table() function to output the MyBOD data frame to a text file.

This file will be saved to the directory that R is working from.


Writing Output to Files Oxygen Demand) data set and then print out the data to the screen.

Use the write.csv() function to output the MyBOD data frame to a comma separated file (which can be opened easily in Excel).

This file will be saved to the directory that R is working from.


Editing Data Oxygen Demand) data set and then print out the data to the screen.

For smaller data files, you can edit the file using the edit() function. This launches an R Editor window.

Always write the edited file to a new object name!

In this case we will edit the newdata object and store the results as an object called Mynewdata. To store the edited object as a file on your computer, use the write.table() function.


Exploring What is In a Data Frame Oxygen Demand) data set and then print out the data to the screen.

The names() and str() commands let you get an overview of what is in a data frame.

The names() function allows you to access the column names and edit them. In this code snippet, the first [1] and second [2] column names are changed from lower case to sentence case.


Accessing Data in a Data Frame Oxygen Demand) data set and then print out the data to the screen.

Use the name of the data frame (MyBOD2), a dollar sign ($) and the name of the variable (Time or Demand) to see a list of all of the observation values.

To access a specific value, you simply indicate the position in the vector…for example, MyBOD2$Demand [2] will access the second value for that variable which is 10.3.

If you “attach” the data frame using the attach() command you can access the variables and observations without the cumbersome need to specify the name of the data frame or the $.

Using what you know…how could you change the value of Demand[2] from 10.3 to 10.5? (be careful that you don’t make such changes to the original data frames!)


Adding columns to a Data Frame Oxygen Demand) data set and then print out the data to the screen.

You can add and delete columns from a data frame.

Here we add a column for the sex of whatever it is we are measuring oxygen demand for.

Oops!!! We have a data entry error. The value for sex should all be female (F). How would you fix this?


Deleting columns from a Data Frame Oxygen Demand) data set and then print out the data to the screen.

You can delete columns from a data frame.

Here we deleted the column for sex that we just created from the MyBOD2 data frame.



Displaying Data in R practice to “detach” it.

R comes with an incredible array of built in data analysis tools for exploring, analyzing, and visualizing data.

Here is a plot of the Time and Demand variables for the MyBOD2 data frame using the plot() command.

Note that because we “attached” this data frame we can just use the names of the variables to access the observation data.

Use help(plot) to look up the details of this command. Figure out how to change the command to add a title to the plot.


Displaying Data in R practice to “detach” it.

Here is a box plot of the Demand variables for the MyBOD2 data frame using the boxplot() command.


Analyzing Data practice to “detach” it.

The summary() command provides summary statistics for a data frame.


Analyzing Data practice to “detach” it.

Here are a series of commands to generate some basic statistics for the Demand variable in the MyBOD2 data frame.

The data frame has been attached so that the variable names can be used directly.

Remember that the case of the variable names were changed relative to the original BOD data set (Time vs time; Demand vs demand)!


Examples of stats functions in r
Examples of stats functions in R practice to “detach” it.

  • mean()

  • median()

  • table() – there is no function to find the mode of a data set but the table() function will show how many times a value is observed.

  • max()

  • min()

  • There is no built in function for midrange so you have to construct a formula to calculate this based on the values from the max() and min() functions.


Measuring data spread practice to “detach” it.

Here are a series of commands to generate some basic statistics related to the spread of measurements for the Demand variable in the MyBOD2 data frame.

The data frame has been attached so that the variable names can be used directly.

Remember that the case of the variable names were changed relative to the original BOD data set (Time vs time; Demand vs demand)!


More examples of stats functions in r
More examples of stats functions in R practice to “detach” it.

  • var()

  • sd()

  • There is no built in function for calculating the standard error of the mean (sem) so you have to create a formula to calculate this.

  • There is no built in function for calculating the range so you have to construct a formula to calculate this based on the values from the max() and min() functions.


What is meant by mode? practice to “detach” it.

What do the variance, standard deviation and standard error of the mean tell us about a data set?


Your Turn practice to “detach” it.

Create a data frame for age and frequency using the data on this slide.

Calculate the cumulative frequency and add it as a column to the data frame.

Save the data frame as a comma separated text file and then open it in Excel.

Plot age versus cumulative frequency.

What are mean and median age?

What is the variance, standard deviation, and standard error mean for frequency?






What are mean and median age? open it in Excel.

What is the variance, standard deviation, and standard error mean for frequency?


Creating an R script. open it in Excel.

Instead of typing one command per prompt, you can type all commands in a text file and run your data analysis process as an R script

R ignores lines that start with #.

You can use this feature to add comments about your R code.



Write your age/frequency data frame assignment as an R script.

Store the script in the directory where you keep all of your R work.

Figure out how to run the script without cutting and pasting it into the interface!



Installing R Packages script.

  • In R, choose the menu item Packages -> Install Packages

  • Choose a CRAN site

  • You will see a list of Packages

  • Choose the aplpack package

  • Your should see a message about accessing the package and then the message

  • “package ‘aplpack’ successfully installed and MD5 sums checked”

  • To load the package, type library(aplpack)

  • Run the following command:

  • stem.leaf(rnorm(50))


What is this command doing? script.

stem.leaf(rnorm(50))


Dynamic R! script.

Looping, Slicing


Using a loop, write an R script that converts Celsius readings between 25 and 30 into Fahrenheit


Dynamic R:Looping, Slicing readings between 25 and 30 into Fahrenheit

Imagine you have two vectors that you want to join into a single vector. Easy.


Dynamic R:Looping, Slicing readings between 25 and 30 into Fahrenheit

What if you only want the 2nd and 3rd and last members of vector c?


Create a vector of sequential numbers from 1 to 1000. readings between 25 and 30 into Fahrenheit

Loop through this vector and pick out only those numbers that are evenly divisible by 3 and create a new vector of these numbers.

How many elements in this new vector?

What is the sum of all the elements in the new vector?


Tips readings between 25 and 30 into Fahrenheit

  • Create a directory/folder on you computer where you can keep your work

    • Each time you start R, be sure it is reading and writing from this directory

  • Document your R scripts/commands using a text editor

    • PLAIN TEXT only

  • Glance through the user documentation that comes with R..consult that documentation if you get stuck


ad