ebook img

R Programming PDF

327 Pages·2009·1.18 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview R Programming

R Programming for Bioinformatics C6367_FM.indd 1 6/11/08 3:08:07 PM Chapman & Hall/CRC Computer Science and Data Analysis Series The interface between the computer and statistical sciences is increasing, as each discipline seeks to harness the power and resources of the other. This series aims to foster the integration between the computer sciences and statistical, numerical, and probabilistic methods by publishing a broad range of reference works, textbooks, and handbooks. SERIES EDITORS David Blei, Princeton University David Madigan, Rutgers University Marina Meila, University of Washington Fionn Murtagh, Royal Holloway, University of London Proposals for the series should be sent directly to one of the series editors above, or submitted to: Chapman & Hall/CRC 4th Floor, Albert House 1-4 Singer Street London EC2A 4BQ UK Published Titles Bayesian Artificial Intelligence Design and Modeling for Computer Kevin B. Korb and Ann E. Nicholson Experiments Kai-Tai Fang, Runze Li, and Computational Statistics Handbook with MATLAB®, Second Edition Agus Sudjianto Wendy L. Martinez and Angel R. Martinez Introduction to Machine Learning and Bioinformatics Pattern Recognition Algorithms for Sushmita Mitra, Sujay Datta, Data Mining Theodore Perkins, and George Michailidis Sankar K. Pal and Pabitra Mitra ® R Graphics Exploratory Data Analysis with MATLAB Paul Murrell Wendy L. Martinez and Angel R. Martinez R Programming for Bioinformatics Clustering for Data Mining: A Data Robert Gentleman Recovery Approach Boris Mirkin Semisupervised Learning for Computational Linguistics Correspondence Analysis and Data Steven Abney Coding with Java and R Fionn Murtagh Statistical Computing with R Maria L. Rizzo C6367_FM.indd 2 6/11/08 3:08:07 PM R Programming for Bioinformatics Robert Gentleman Fred Hutchinson Cancer Research Center Seattle, Washington, U.S.A. C6367_FM.indd 3 6/11/08 3:08:07 PM Chapman & Hall/CRC Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2009 by Taylor & Francis Group, LLC Chapman & Hall/CRC is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed in the United States of America on acid-free paper 10 9 8 7 6 5 4 3 2 1 International Standard Book Number-13: 978-1-4200-6367-7 (Hardcover) This book contains information obtained from authentic and highly regarded sources Reason- able efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The Authors and Publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www. copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC) 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging-in-Publication Data Gentleman, Robert, 1959- R programming for bioinformatics / Robert Gentleman. p. cm. -- (Chapman & Hall/CRC computer science and data analysis series) Bibliographical references (p. ) and index. ISBN 978-1-4200-6367-7 1. Bioinformatics. 2. R (Computer program language) I. Title. II. Series. QH324.2.G46 2008 572.80285’5133--dc22 2008011352 Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com C6367_FM.indd 4 6/11/08 3:08:07 PM To Tanja, Sophie and Katja Contents 1 Introducing R 1 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 A note on the text . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . 4 2 R Language Fundamentals 5 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.1.1 A brief introduction to R . . . . . . . . . . . . . . . . 5 2.1.2 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.1.3 A very brief introduction to OOP in R . . . . . . . . . 7 2.1.4 Some special values . . . . . . . . . . . . . . . . . . . 8 2.1.5 Types of objects . . . . . . . . . . . . . . . . . . . . . 9 2.1.6 Sequence generating and vector subsetting . . . . . . . 11 2.1.7 Types of functions . . . . . . . . . . . . . . . . . . . . 12 2.2 Data structures . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.2.1 Atomic vectors . . . . . . . . . . . . . . . . . . . . . . 12 2.2.2 Numerical computing . . . . . . . . . . . . . . . . . . 15 2.2.3 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.2.4 Lists, environments and data frames . . . . . . . . . . 18 2.3 Managing your R session. . . . . . . . . . . . . . . . . . . . . 22 2.3.1 Finding out more about an object . . . . . . . . . . . 24 2.4 Language basics. . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.4.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.5 Subscripting and subsetting . . . . . . . . . . . . . . . . . . . 28 2.5.1 Vector and matrix subsetting . . . . . . . . . . . . . . 29 2.6 Vectorized computations . . . . . . . . . . . . . . . . . . . . . 36 2.6.1 The recycling rule . . . . . . . . . . . . . . . . . . . . 37 2.7 Replacement functions . . . . . . . . . . . . . . . . . . . . . . 38 2.8 Functional programming . . . . . . . . . . . . . . . . . . . . . 39 2.9 Writing functions . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.10 Flow control. . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2.10.1 Conditionals . . . . . . . . . . . . . . . . . . . . . . . 44 2.11 Exception handling . . . . . . . . . . . . . . . . . . . . . . . . 45 2.12 Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2.12.1 Standard evaluation . . . . . . . . . . . . . . . . . . . 51 2.12.2 Non-standard evaluation . . . . . . . . . . . . . . . . . 52 vii viii 2.12.3 Function evaluation . . . . . . . . . . . . . . . . . . . 53 2.12.4 Indirect function invocation . . . . . . . . . . . . . . . 54 2.12.5 Evaluation on exit . . . . . . . . . . . . . . . . . . . . 54 2.12.6 Other topics . . . . . . . . . . . . . . . . . . . . . . . 55 2.12.7 Name spaces . . . . . . . . . . . . . . . . . . . . . . . 57 2.13 Lexical scope . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.13.1 Likelihoods . . . . . . . . . . . . . . . . . . . . . . . . 61 2.13.2 Function optimization . . . . . . . . . . . . . . . . . . 62 2.14 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 3 Object-Oriented Programming in R 67 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.2 The basics of OOP . . . . . . . . . . . . . . . . . . . . . . . . 68 3.2.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . 69 3.2.2 Dispatch. . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.2.3 Abstract data types . . . . . . . . . . . . . . . . . . . 72 3.2.4 Self-describing data . . . . . . . . . . . . . . . . . . . 73 3.3 S3 OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.3.1 Implicit classes . . . . . . . . . . . . . . . . . . . . . . 76 3.3.2 Expression data example . . . . . . . . . . . . . . . . 77 3.3.3 S3 generic functions and methods. . . . . . . . . . . . 78 3.3.4 Details of dispatch . . . . . . . . . . . . . . . . . . . . 81 3.3.5 Group generics . . . . . . . . . . . . . . . . . . . . . . 83 3.3.6 S3 replacement methods . . . . . . . . . . . . . . . . . 83 3.4 S4 OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 3.4.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.4.2 Types of classes. . . . . . . . . . . . . . . . . . . . . . 98 3.4.3 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . 98 3.4.4 Class unions . . . . . . . . . . . . . . . . . . . . . . . 99 3.4.5 Accessor functions . . . . . . . . . . . . . . . . . . . . 100 3.4.6 Using S3 classes with S4 classes . . . . . . . . . . . . . 100 3.4.7 S4 generic functions and methods. . . . . . . . . . . . 101 3.4.8 The syntax of method declaration . . . . . . . . . . . 105 3.4.9 The semantics of method invocation . . . . . . . . . . 106 3.4.10 Replacement methods . . . . . . . . . . . . . . . . . . 107 3.4.11 Finding methods . . . . . . . . . . . . . . . . . . . . . 107 3.4.12 Advanced topics . . . . . . . . . . . . . . . . . . . . . 108 3.5 Using classes and methods in packages . . . . . . . . . . . . . 110 3.6 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . 110 3.6.1 Finding documentation . . . . . . . . . . . . . . . . . 110 3.6.2 Writing documentation . . . . . . . . . . . . . . . . . 111 3.7 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 3.8 Managing S3 and S4 together . . . . . . . . . . . . . . . . . . 112 3.8.1 Getting and setting the class attribute . . . . . . . . 113 3.8.2 Mixing S3 and S4 methods . . . . . . . . . . . . . . . 114 ix 3.9 Navigating the class and method hierarchy . . . . . . . . . . 115 4 Input and Output in R 119 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 4.2 Basic file handling . . . . . . . . . . . . . . . . . . . . . . . . 120 4.2.1 Viewing files . . . . . . . . . . . . . . . . . . . . . . . 124 4.2.2 File manipulation. . . . . . . . . . . . . . . . . . . . . 125 4.2.3 Working with R’s binary format . . . . . . . . . . . . 129 4.3 Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 4.3.1 Text connections . . . . . . . . . . . . . . . . . . . . . 131 4.3.2 Interprocess communications . . . . . . . . . . . . . . 133 4.3.3 Seek . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 4.4 File input and output . . . . . . . . . . . . . . . . . . . . . . 137 4.4.1 Reading rectangular data . . . . . . . . . . . . . . . . 138 4.4.2 Writing data . . . . . . . . . . . . . . . . . . . . . . . 139 4.4.3 Debian Control Format (DCF) . . . . . . . . . . . . . 140 4.4.4 FASTA Format . . . . . . . . . . . . . . . . . . . . . . 141 4.5 Source and sink: capturing R output . . . . . . . . . . . . . . 142 4.6 Tools for accessing files on the Internet . . . . . . . . . . . . . 143 5 Working with Character Data 145 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 5.2 Builtin capabilities . . . . . . . . . . . . . . . . . . . . . . . . 146 5.2.1 Modifying text . . . . . . . . . . . . . . . . . . . . . . 151 5.2.2 Sorting and comparing . . . . . . . . . . . . . . . . . . 152 5.2.3 Matching a set of alternatives . . . . . . . . . . . . . . 153 5.2.4 Formatting text and numbers . . . . . . . . . . . . . . 155 5.2.5 Special characters and escaping . . . . . . . . . . . . . 155 5.2.6 Parsing and deparsing . . . . . . . . . . . . . . . . . . 158 5.2.7 Plotting with text . . . . . . . . . . . . . . . . . . . . 159 5.2.8 Locale and font encoding . . . . . . . . . . . . . . . . 159 5.3 Regular expressions. . . . . . . . . . . . . . . . . . . . . . . . 159 5.3.1 Regular expression basics . . . . . . . . . . . . . . . . 160 5.3.2 Matching . . . . . . . . . . . . . . . . . . . . . . . . . 166 5.3.3 Using regular expressions . . . . . . . . . . . . . . . . 167 5.3.4 Globbing and regular expressions . . . . . . . . . . . . 169 5.4 Prefixes, suffixes and substrings . . . . . . . . . . . . . . . . . 169 5.5 Biological sequences . . . . . . . . . . . . . . . . . . . . . . . 171 5.5.1 Encoding genomes . . . . . . . . . . . . . . . . . . . . 172 5.6 Matching patterns . . . . . . . . . . . . . . . . . . . . . . . . 173 5.6.1 Matching single query sequences . . . . . . . . . . . . 174 5.6.2 Matching many query sequences . . . . . . . . . . . . 175 5.6.3 Palindromes and paired matches . . . . . . . . . . . . 177 5.6.4 Alignments . . . . . . . . . . . . . . . . . . . . . . . . 179

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.