1 / 64

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?. R is an integrated suite of software facilities for data manipulation, calculation, and graphical display. It includes:

alvaro
Télécharger la présentation

Introduction to R

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to R Carol Bult The Jackson Laboratory Functional Genomics (BMB550) Spring 2011

  2. R Project for Statistical Computing http://www.r-project.org/

  3. 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

  4. R is available for free under the GNU General Public License http://www.fsf.org/licensing/licenses/quick-guide-gplv3.html

  5. R is an official part of the Free Software Foundation http://www.fsf.org/

  6. 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/

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

  8. 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.

  9. 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

  10. Let’s Get Started… http://www.cloud.target.maine.edu/

  11. Xming is an X server for Windows http://sourceforge.net/projects/xming/

  12. 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

  13. 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.

  14. Once logged in, type R at the $ prompt to start your R session

  15. You can invoke on-line help with the help.start() command. Note: R is a case sensitive language! What happens if you type Help.Start() at the command prompt?

  16. At the prompt (>) type the command , library(), 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.

  17. The caret “>” is the command prompt • ‘library()’ is the command • the parantheses are for defining specific operations associated with the function. 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

  18. Use this command to get a list of functions associated with the stats package.

  19. Use this command to launch detailed documentation for a package.

  20. Running this command will load all of the data sets that come with your R installation.

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

  22. Data Frames in R • 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

  23. Types of Objects in R • 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

  24. Create the BOD Data Frame from Scratch 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

  25. Reading in data using the scan() command 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().

  26. Reading in Data from Files 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.

  27. How would you find out more about the c() function? Find the details behind the data in the BOD data frame.

  28. Writing Output to Files 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.

  29. Writing Output to Files 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.

  30. Editing Data 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.

  31. Exploring What is In a Data Frame 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.

  32. Accessing Data in a Data Frame 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!)

  33. Adding columns to a Data Frame 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?

  34. Deleting columns from a Data Frame You can delete columns from a data frame. Here we deleted the column for sex that we just created from the MyBOD2 data frame.

  35. Note: When you are done using a data frame it is a good practice to “detach” it.

  36. Displaying Data in R 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.

  37. Displaying Data in R Here is a box plot of the Demand variables for the MyBOD2 data frame using the boxplot() command.

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

  39. Analyzing Data 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)!

  40. Examples of stats functions in R • 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.

  41. Measuring data spread 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)!

  42. More examples of stats functions in R • 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.

  43. What is meant by mode? What do the variance, standard deviation and standard error of the mean tell us about a data set?

  44. Your Turn 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?

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

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

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

  48. Plot age versus cumulative frequency.

More Related