0% found this document useful (0 votes)
6 views170 pages

The R Primer 2nd Edition Claus Thorn Ekstrøm Download Full Chapters

Uploaded by

viennaogr4809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views170 pages

The R Primer 2nd Edition Claus Thorn Ekstrøm Download Full Chapters

Uploaded by

viennaogr4809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 170

The R Primer 2nd Edition Claus Thorn Ekstrøm

download full chapters

https://ebookname.com/product/the-r-primer-2nd-edition-claus-thorn-
ekstrom/

★★★★★
4.7 out of 5.0 (36 reviews )

Instant PDF Access

ebookname.com
The R Primer 2nd Edition Claus Thorn Ekstrøm

EBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 ACADEMIC EDITION – LIMITED RELEASE

Available Instantly Access Library


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Particle detectors 2nd ed Edition Claus Grupen

https://ebookname.com/product/particle-detectors-2nd-ed-edition-claus-
grupen/

ebookname.com

Evolutionary Witchcraft T. Thorn Coyle

https://ebookname.com/product/evolutionary-witchcraft-t-thorn-coyle/

ebookname.com

The Swan System of the C2 Molecule John G. Phillips

https://ebookname.com/product/the-swan-system-of-the-c2-molecule-john-
g-phillips/

ebookname.com

Encyclopedia of Garden Ferns Sue Olsen

https://ebookname.com/product/encyclopedia-of-garden-ferns-sue-olsen/

ebookname.com
Dinosaur DK Eyewitness Books David Lambert

https://ebookname.com/product/dinosaur-dk-eyewitness-books-david-
lambert/

ebookname.com

The Journal of Socho 1st Edition H. Mack Horton

https://ebookname.com/product/the-journal-of-socho-1st-edition-h-mack-
horton/

ebookname.com

Global divas Filipino gay men in the diaspora 3. print


Edition Manalansan

https://ebookname.com/product/global-divas-filipino-gay-men-in-the-
diaspora-3-print-edition-manalansan/

ebookname.com

Improve your punctuation and grammar 3rd ed. [new rev.]


Edition Field

https://ebookname.com/product/improve-your-punctuation-and-
grammar-3rd-ed-new-rev-edition-field/

ebookname.com

Kings of the forest the cultural resilience of Himalayan


hunter gatherers 1 New Edition Jana Fortier

https://ebookname.com/product/kings-of-the-forest-the-cultural-
resilience-of-himalayan-hunter-gatherers-1-new-edition-jana-fortier/

ebookname.com
Pocket World in Figures 2007 Edition The Economist

https://ebookname.com/product/pocket-world-in-figures-2007-edition-
the-economist/

ebookname.com
https://creativecommons.org/licenses/by-sa/4.0/

CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742

© 2016 by The R Foundation


CRC Press is an imprint of Taylor & Francis Group, an Informa business

Version Date: 20170110

International Standard Book Number-13: 978-1-138-63197-7 (Paperback)


International Standard Book Number-13: 978-1-4987-7255-6 (Hardback)

Visit the Taylor & Francis Web site at


http://www.taylorandfrancis.com

and the CRC Press Web site at


http://www.crcpress.com
Contents

Preface xv

Chapter 1  Importing and exporting data 1

1.1 IMPORT AN R DATASET FROM A PACKAGE 1


1.2 LOAD AND SAVE R DATA FILES 2
1.3 READ AND WRITE TEXT FILES 3
1.4 READ AND WRITE CSV FILES 6
1.5 READ DATA FROM THE CLIPBOARD 8
SPREADSHEETS 9
1.6 READ AND WRITE DATA FROM A SPREADSHEET 9
1.7 READ AND WRITE EXCEL FILES 9
STATISTICAL SOFTWARE PROGRAMS 11
1.8 IMPORT AND EXPORT SAS DATASETS 11
1.9 IMPORT AND EXPORT SPSS DATASETS 13
1.10 IMPORT OR EXPORT A STATA DATASET 14
1.11 IMPORT A SYSTAT DATASET 14
DATA EXCHANGE FORMATS AND DATABASES 15
1.12 IMPORT A JSON DATASET 15
1.13 READ DATA FROM A SIMPLE XML FILE 18
1.14 READ DATA FROM AN XML FILE 20
1.15 EXPORT A DATA FRAME TO XML 24
1.16 IMPORT DATA FROM AN HTML TABLE 25
1.17 SCRAPE DATA FROM AN HTML WEB PAGE 26
1.18 IMPORT FROM A MYSQL/POSTGRESQL DATABASE 29
1.19 READ DATA FROM AN SQL DATABASE USING ODBC 30

Chapter 2  Manipulating data 33

2.1 USE MATHEMATICAL FUNCTIONS 35


2.2 USE COMMON VECTOR OPERATIONS 37
2.3 WORK WITH CHARACTER VECTORS 41
2.4 READ NON-ASCII CHARACTER VECTORS 42
2.5 SORT AND ORDER DATA 43
2.6 TRANSFORM A VARIABLE 45
2.7 FIND THE VALUE OF X CORRESPONDING TO THE
MAXIMUM OR MINIMUM OF Y 46
2.8 CHECK IF ELEMENTS IN ONE OBJECT ARE PRESENT
IN ANOTHER OBJECT 47
2.9 APPLY A FUNCTION TO SUBSETS OF A VECTOR 48
2.10 FILL IN MISSING VALUES WITH PREVIOUS VALUES 48
2.11 CONVERT COMMA AS DECIMAL MARK TO PERIOD 50
2.12 LAG OR SHIFT A VECTOR 50
2.13 CALCULATE THE AREA UNDER A CURVE 52
MATRICES, ARRAYS, AND TABLES 54
2.14 APPLY FUNCTION TO MARGINS OF A MATRIX/AR-
RAY 54
2.15 COMPUTE A MATRIX/ARRAY OF PROPORTIONS 55
2.16 TRANSPOSE A MATRIX (OR DATA FRAME) 56
2.17 CREATE A TABLE OF COUNTS 57
2.18 CONVERT A TABLE OF COUNTS TO A DATA FRAME 59
DATES AND TIMES 61
2.19 PARSING DATES AND TIMES 61
2.20 EXTRACT AND FORMAT DATE/TIME INFORMATION 63
FACTORS 65
2.21 CONVERT A FACTOR TO NUMERIC 65
2.22 CONVERT A FACTOR TO CHARACTER STRINGS 66
2.23 ADD A NEW LEVEL TO AN EXISTING FACTOR 66
2.24 COMBINE THE LEVELS OF A FACTOR 67
2.25 REMOVE UNUSED LEVELS OF A FACTOR 68
2.26 CHANGE THE REFERENCE LEVEL 69
2.27 CUT A NUMERIC VECTOR INTO A FACTOR 70
DATA FRAMES AND LISTS 70
2.28 SELECT A SUBSET OF A DATA FRAME 70
2.29 SELECT THE COMPLETE CASES OF A DATA FRAME 72
2.30 DELETE A VARIABLE FROM A DATA FRAME 73
2.31 APPLY FUNCTION TO EACH VARIABLE IN A DATA
FRAME 74
2.32 SPLIT DATA FRAME INTO SUBSETS AND APPLY
FUNCTION TO EACH PART 76
2.33 APPLY FUNCTION TO EACH ROW OF A DATA FRAME 77
2.34 COMBINE TWO DATASETS 80
2.35 MERGE DATASETS 81
2.36 ADD NEW OBSERVATIONS TO A DATA FRAME 84
2.37 STACK THE COLUMNS OF A DATA FRAME TO-
GETHER 85
2.38 RESHAPE A DATA FRAME FROM WIDE TO LONG
FORMAT OR VICE VERSA 88
2.39 CONVERT A DATA FRAME TO A VECTOR 94

Chapter 3  Statistical analyses 97

DESCRIPTIVE STATISTICS 100


3.1 COMPUTE SUMMARY STATISTICS 100
3.2 CREATE DESCRIPTIVE TABLE 102
LINEAR MODELS 103
3.3 FIT A LINEAR REGRESSION MODEL 103
3.4 FIT A MULTIPLE LINEAR REGRESSION MODEL 105
3.5 FIT A POLYNOMIAL REGRESSION MODEL 107
3.6 FIT A ONE-WAY ANALYSIS OF VARIANCE 108
3.7 MAKE POST-HOC PAIRWISE COMPARISONS 111
3.8 FIT A TWO-WAY ANALYSIS OF VARIANCE 114
3.9 FIT A LINEAR NORMAL MODEL 117
3.10 FIT A PENALIZED REGRESSION MODEL 121
GENERALIZED LINEAR MODELS 125
3.11 FIT A LOGISTIC REGRESSION MODEL 125
3.12 FIT A CONDITIONAL LOGISTIC REGRESSION MODEL 130
3.13 FIT AN ORDINAL LOGISTIC REGRESSION MODEL 132
3.14 FIT A MULTINOMIAL LOGISTIC REGRESSION MODEL 137
3.15 FIT A POISSON REGRESSION MODEL 140
METHODS FOR ANALYSIS OF REPEATED MEASURE-
MENTS 145
3.16 FIT A LINEAR MIXED-EFFECTS MODEL 145
3.17 FIT A LINEAR MIXED-EFFECTS MODEL WITH SERIAL
CORRELATION 149
3.18 FIT A GENERALIZED LINEAR MIXED MODEL 156
3.19 FIT A GENERALIZED ESTIMATING EQUATION MODEL 160
3.20 ANALYZE TIME SERIES USING AN ARIMA MODEL 165
3.21 DECOMPOSE A TIME SERIES INTO TREND, SEA-
SONAL, AND RESIDUAL COMPONENTS 171
SPECIFIC METHODS 174
3.22 COMPARE POPULATIONS USING T TEST 174
3.23 FIT A NONLINEAR MODEL 176
3.24 FIT A CENSORED REGRESSION MODEL 180
3.25 FIT A ZERO-INFLATED REGRESSION MODEL 182
3.26 FIT A SMOOTH CURVE 185
3.27 FIT A GENERALIZED ADDITIVE MODEL 187
3.28 USE META ANALYSIS TO COMBINE AND SUMMARIZE
THE RESULTS FROM SEVERAL STUDIES 192
3.29 USE RANDOM FOREST FOR CLASSIFICATION AND
REGRESSION 197
3.30 FIT A LINEAR QUANTILE REGRESSION MODEL 203
MODEL VALIDATION 208
3.31 TEST FOR NORMALITY OF A SINGLE SAMPLE 208
3.32 TEST VARIANCE HOMOGENEITY ACROSS GROUPS 210
3.33 VALIDATE A LINEAR OR GENERALIZED LINEAR
MODEL 213
CONTINGENCY TABLES 215
3.34 ANALYZE TWO-DIMENSIONAL CONTINGENCY TA-
BLES 215
3.35 ANALYZE TWO-DIMENSIONAL CONTINGENCY TA-
BLES WITH ORDINAL CATEGORIES 218
3.36 ANALYZE TWO-DIMENSIONAL CONTINGENCY TA-
BLES WITH PAIRED MEASUREMENTS 219
3.37 ANALYZE CONTINGENCY TABLES USING LOG-LINEAR
MODELS 221
AGREEMENT 224
3.38 CREATE A BLAND–ALTMAN PLOT OF AGREEMENT
TO COMPARE TWO QUANTITATIVE METHODS 224
3.39 DETERMINE AGREEMENT AMONG SEVERAL METH-
ODS OF A QUANTITATIVE MEASUREMENT 226
3.40 CALCULATE COHEN’S KAPPA FOR AGREEMENT 231
MULTIVARIATE METHODS 234
3.41 FIT A MULTIVARIATE REGRESSION MODEL 234
3.42 CLUSTER OBSERVATIONS 236
3.43 USE PRINCIPAL COMPONENT ANALYSIS TO REDUCE
DATA DIMENSIONALITY 239
3.44 FIT A PRINCIPAL COMPONENT REGRESSION MODEL 243
3.45 CLASSIFY OBSERVATIONS USING LINEAR DISCRIM-
INANT ANALYSIS 245
3.46 USE PARTIAL LEAST SQUARES REGRESSION FOR
PREDICTION 249
RESAMPLING STATISTICS AND BOOTSTRAPPING 252
3.47 NON-PARAMETRIC BOOTSTRAP ANALYSIS 252
3.48 USE CROSS-VALIDATION TO ESTIMATE THE PER-
FORMANCE OF A MODEL OR ALGORITHM 256
3.49 CALCULATE POWER OR SAMPLE SIZE FOR SIMPLE
DESIGNS 260
NON-PARAMETRIC METHODS 264
3.50 TEST MEDIAN WITH WILCOXON’S SIGNED RANK
TEST 264
3.51 USE MANN–WHITNEY’S TEST TO COMPARE TWO
GROUPS 267
3.52 COMPARE GROUPS USING KRUSKAL–WALLIS’ TEST 268
3.53 COMPARE GROUPS USING FRIEDMAN’S TEST FOR A
TWO-WAY DESIGN 270
SURVIVAL ANALYSIS 272
3.54 FIT A KAPLAN–MEIER SURVIVAL CURVE TO EVENT
HISTORY DATA 272
3.55 FIT A COX REGRESSION MODEL (PROPORTIONAL
HAZARDS MODEL) 275
3.56 FIT A COX REGRESSION MODEL (PROPORTIONAL
HAZARDS MODEL) WITH TIME-VARYING COVARI-
ATES 280
MISCELLANEOUS 284
3.57 CORRECT P -VALUES FOR MULTIPLE TESTING 284
3.58 USE A BOX–COX TRANSFORMATION TO MAKE NON-
NORMALLY DISTRIBUTED DATA APPROXIMATELY
NORMAL 286

Chapter 4  Graphics 289

4.1 USE GREEK LETTERS AND FORMULAS IN GRAPHS 292


4.2 SET COLORS IN R GRAPHICS 293
4.3 SET COLOR PALETTES IN R GRAPHICS 294
HIGH-LEVEL PLOTS 296
4.4 CREATE A SCATTER PLOT 296
4.5 CREATE A BUBBLE PLOT 298
4.6 CREATE A HISTOGRAM 300
4.7 CREATE A HANGING ROOTOGRAM 301
4.8 CREATE A VIOLIN- OR BOXPLOT 303
4.9 CREATE A BAR PLOT 306
4.10 CREATE A BAR PLOT WITH ERROR BARS 308
4.11 CREATE A PLOT WITH ESTIMATES AND CONFI-
DENCE INTERVALS 310
4.12 CREATE A PIE CHART 312
4.13 CREATE A PYRAMID PLOT 313
4.14 PLOT MULTIPLE SERIES 315
4.15 MAKE A 2D SURFACE PLOT 317
4.16 MAKE A 3D SURFACE PLOT 319
4.17 PLOT A 3D SCATTER PLOT 321
4.18 CREATE A HEAT MAP PLOT 323
4.19 PLOT A CORRELATION MATRIX 325
4.20 MAKE A QUANTILE-QUANTILE PLOT 327
4.21 GRAPHICAL MODEL VALIDATION FOR LINEAR MOD-
ELS 329
4.22 CREATE A VENN OR EULER DIAGRAM 333
TWEAKING GRAPHICS 335
4.23 ADD A BROKEN AXIS TO INDICATE DISCONTINUITY 335
4.24 CREATE A PLOT WITH TWO Y -AXES 336
4.25 ROTATE AXIS LABELS 337
4.26 MULTIPLE PLOTS 339
4.27 ADD A LEGEND TO A PLOT 341
4.28 ADD A TABLE TO A PLOT 342
4.29 LABEL POINTS IN A SCATTER PLOT 344
4.30 IDENTIFY POINTS IN A SCATTER PLOT 346
4.31 VISUALIZE POINTS, SHAPES, AND SURFACES IN 3D
AND INTERACT WITH THEM IN REAL-TIME 347
WORKING WITH GRAPHICS 350
4.32 EXPORTING GRAPHICS 350
4.33 PRODUCE GRAPHICS IN LATEX-READY FORMAT 351
4.34 EMBED FONTS IN POSTSCRIPT OR PDF GRAPHICS 353
Visit https://ebookname.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Chapter 5  R 357

GETTING INFORMATION 357


5.1 GETTING HELP 357
5.2 FINDING R SOURCE CODE FOR A FUNCTION 359
5.3 TIMING R COMMANDS AND FUNCTIONS 361
R PACKAGES 362
5.4 INSTALLING R PACKAGES 362
5.5 UPDATE INSTALLED R PACKAGES 364
5.6 UNLOAD A PACKAGE 365
5.7 INSTALL AN R PACKAGE FROM A REPOSITORY 365
5.8 INSTALL A PACKAGE FROM BIOCONDUCTOR 366
5.9 LIST THE INSTALLED PACKAGES 367
5.10 LIST THE CONTENT OF A PACKAGE 367
5.11 UNINSTALL AN R PACKAGE 368
5.12 LIST OR VIEW VIGNETTES 369
5.13 PERMANENTLY CHANGE THE DEFAULT DIRECTORY
WHERE R INSTALLS PACKAGES 371
5.14 AUTOMATICALLY LOAD A PACKAGE AT STARTUP 372
THE R WORKSPACE 373
5.15 LIST OR DELETE OBJECTS 373
5.16 CHANGE THE CURRENT WORKING DIRECTORY 374
5.17 SAVE AND LOAD WORKSPACES 374
5.18 SAVE AND RESTORE HISTORIES 375
5.19 INTERACT WITH THE FILE SYSTEM 377
5.20 LOCATE AND CHOOSE FILES INTERACTIVELY 378
5.21 INTERACT WITH THE OPERATING SYSTEM 379
5.22 GET SESSION INFORMATION 380

Bibliography 385

Index 393
Preface

A lot has happened to R and in the R community in the five years since
the first edition of The R Primer was published. Changes in R itself,
hundreds of new and improved packages, but most importantly that R
has seen even more widespread acceptance and has been adopted by
more and more research fields as the primary software tool for data
analysis.
In addition, the recent focus in the scientific fields on research docu-
mentation and reproducible research has made R an even more valuable
and ideal tool for data analysis and hence it made sense to update the
current text to reflect some of the changes to R. The majority of the
problems and cases covered in the book have been updated and the text
has grown substantially to cover additional situations and problems.
The second edition of The R Primer follows the same format as the
first edition in that it assumes that the reader knows what he or she
wants and/or what analysis method is appropriate in a given situation,
but that the reader needs help with both coding and interpreting the
output from that model in R. The problems in this second edition have
been — just as for the first edition — heavily inspired by the questions
encountered at the statistical consultancy service at the University of
Copenhagen. During these consultancy sessions we discuss statistical
problems with clients and often provide them with example code that
covers their situation. The examples and problems in the book have
proven valuable as a reference point for the clients in this regard as it is
easy to show them a working example and explain the changes they need
to adapt to get the code working in their situation. Hopefully, others will
also find the text useful as a collection of solutions to common situations
for newcomers and intermediate users of R.

Claus Thorn Ekstrøm


Copenhagen 2016
Preface to the first edition
This book is not about statistical theory, neither is it meant to teach R
programming. This book is intended for readers who know the basics of
R, but find themselves with problems or situations that are commonly
encountered by newcomers to R or for readers who want to see compact
examples of different types of typical statistical analyses. In other words,
if you understand basic statistics and already know a bit about R then
this book is for you.
R has rapidly become the lingua franca of statistical computing; it is
a free statistical programming software and it can be downloaded from
http://cran.r-project.org. Many newcomers to R are often intimi-
dated by the command-line interface, or the sheer number of functions
and packages, or just trying to figure out how to import data and per-
form a simple statistical analysis.
The book consists of a number of examples that illustrate a specific
situation, topic, or problem from data import over data management and
classical statistical analyses to graphics. Each example is self-contained
and provides R code that can be run exactly as shown and the results
from the book can be reproduced. The only change — barring simu-
lated data, machine set-up and small tweaks to make figures suitable
for printing — is that some of the output lines have been removed for
brevity.
This is not a “missing manual” or a thorough exploration of the
functions used. Instead of trying to cover every possible option or special
case that might be of interest, we focus on the common situations that
most beginning users are likely to encounter. Thus we concentrate on
the basics of getting things done and giving examples that can be used
as a starting point for the reader rather than exploring the multitude of
options available with every command and the ever-increasing number
of packages. For most problems — and this is particularly true for a
programming language like R— there is more than one way to solve a
problem. Here, I have provided a single solution to most problems and
have tried to use base R if at all possible. If there are other functions
and/or packages available that cover or extend the same functionality,
then some of them are listed at the end of each example.
The R list of frequently asked questions is highly recommended and
covers a few of the same topics mentioned here. However, it does not
cover examples of statistical analyses and it rarely covers some of the
most basic problems new users encounter.
Base graphics are used throughout the book. More advanced graph-
ics can be produced with the recent lattice and ggplot2 packages
(see Sarkar (2008), Wickham (2009), or Murrell (2011) for further in-
formation on advanced R graphics). A more complete coverage of R
and/or statistics can be found in the books by Venables and Ripley
(2002), Verzani (2005), Crawley (2007), Dalgaard (2008), and Everitt
and Hothorn (2010). These books have a slightly different target audi-
ence than the present text and are all highly recommended.
The R Primer has a supporting web site at

http://www.rprimer.dk

where additional topics are covered and where the R code used in the
book can be found.
I would like to thank all R developers and package writers for the
enormous work they have done and continue to put into the R program
and extensions. I appreciate all the helpful responses to my enquiries
and suggestions. I am grateful to my colleagues at the Faculty of Life
Sciences, University of Copenhagen, as well as Klaus K. Holst, Duncan
Temple Lang, and Bendix Carstensen for their ideas, comments, sug-
gestions, and encouragement on various stages of the manuscript. Many
thanks to Tina Ekstrøm for once again creating a wonderful cover, and
last, but not least, thanks to Marlene, Ellen, and Anna for bearing with
me through yet another book.

Claus Thorn Ekstrøm


Frederiksberg 2011
CHAPTER 1
Importing and exporting
data

The first step in most statistical analyses is to get the data into R.
While it is possible to manually type the data directly into R using,
say, the c and data.frame functions, for most serious work the data is
typically stored in a file somewhere locally on the computer, a network,
or somewhere on the Web.
Table 1.1 gives a quick overview of R functions and related packages
for importing common data formats. Specific solutions to importing dif-
ferent types of data are presented in the rest of this chapter.

1.1 IMPORT AN R DATASET FROM A PACKAGE


Problem: You want to import an R dataset from a package
Solution: Many R packages (including those that are part of the default
installation) come with several datasets that are ready to use. R datasets
found in packages are imported with the data function and require that
the package is loaded first.
> library(MESS) # Load the package that contains the data
> data(bees) # Load the data
> head(bees) # Show the first couple of lines of data
Locality Replicate Color Time Type Number id
1 Havreholm A White july1 Bumblebees 1 1
4 Havreholm A Yellow july1 Bumblebees 2 1
7 Havreholm A Blue july1 Bumblebees 0 1
10 Havreholm A White july1 Solitary 1 1
13 Havreholm A Yellow july1 Solitary 4 1
16 Havreholm A Blue july1 Solitary 3 1
2  The R Primer

Table 1.1: Common file formats and R functions to read them

File type Function Package


R data file load or data
Text file read.table
CSV read.csv or read.csv2
JSON fromJSON jsonlite
Excel spreadsheet read_excel readxl
SAS read_sas haven
SPSS .SAV or .POR read_sav or read_por haven
Stata read_stata haven
html read_html rvest
xml xmlTreeParse XML

1.2 LOAD AND SAVE R DATA FILES


Problem: You want to load or save an external R dataset.
Solution: R has its own data file format where files typically have
extension RData or rda and they are imported with the load function.
A single R data files may contain multiple R objects, and the objects can
be any type and not just data frames. The objects imported have the
same name and structure as they have in the file, and we only see a list
of the imported objects if the verbose=TRUE argument is included.
> load("penny.rda") # Load data
> load("penny.rda", verbose=TRUE) # Load data and show objects
Loading objects:
big
bang
theory

Beware that load overwrites existing objects with the same names
without giving any warnings.
The save function is used to save R objects to a file. save takes any
number of R objects as arguments and saves them to an external file,
and the file argument specifies the name of the file.
> x <- c(1, 7, 3, 2)
> df <- data.frame(v1=c("A", "B", "C", "D"), v2=1:4)
> save(x, df, file="mydata.rda") # Save two objects

See also: Problem 5.16 explains how to change the working directory.
Importing and exporting data  3

1.3 READ AND WRITE TEXT FILES


Problem: You want to work with a dataset stored in a text file.
Solution: Data stored in simple text files can be read into R using
the read.table function. By default, the observations should be listed
in columns where the individual fields are separated by one or more
white space characters (e.g., space, or tabulators), and where each line
in the file corresponds to one row of the data frame. The columns do not
need to be straight or formatted, but multi-word observations like high
income need to be put in quotes or combined into a single word so they
are not interpreted as two columns. If the text file mydata.txt has the
following content
acid digest name
30.3 70.6 NA
29.8 67.5 Eeny
NA 87.0 Meeny
4.1 89.9 Miny
4.4 . Moe
2.8 93.1 .
3.8 96.7 ""
we can read the file into R with the following command:
> indata <- read.table("mydata.txt", header=TRUE)
> indata
acid digest name
1 30.3 70.6 <NA>
2 29.8 67.5 Eeny
3 NA 87.0 Meeny
4 4.1 89.9 Miny
5 4.4 . Moe
6 2.8 93.1 .
7 3.8 96.7

The first argument is the name of the data file, and the second ar-
gument (header=TRUE) is optional and should be used only if the first
line of the text file provides the variable/column names. If the first line
does not contain the column names, the variables will be labeled con-
secutively V1, V2, V3, etc. Each line in the input file must contain the
same number of columns for read.table to work. The sep option can
be set to indicate which character that separates the columns. For ex-
ample, use sep="\t" if the columns are separated by tabs. Data read
with read.table are stored as a data frame within R.
4  The R Primer

The default code for missing observations is the character string NA


which we can see works both for the first and third observation above
(acid is read as a numeric vector and name as a factor). Empty char-
acter fields are read as empty character vectors, unless the argument
na.strings contains the value "" in which case they become missing
values. Empty numeric fields (for example if the columns are separated
by tabs) are automatically considered missing.

> indata <- read.table("mydata.txt", header=TRUE,


+ na.strings=c("NA", ""))
> indata
acid digest name
1 30.3 70.6 <NA>
2 29.8 67.5 Eeny
3 NA 87.0 Meeny
4 4.1 89.9 Miny
5 4.4 . Moe
6 2.8 93.1 .
7 3.8 96.7 <NA>

Note that R considers the variable digest as a factor and not numeric
since the period ‘.’ for observation 5 is read as a character string. If
periods should also be considered missing variables we need to include
that in na.strings vector.

> indata <- read.table("mydata.txt", header=TRUE,


+ na.strings=c("NA", "", "."))
> indata
acid digest name
1 30.3 70.6 <NA>
2 29.8 67.5 Eeny
3 NA 87.0 Meeny
4 4.1 89.9 Miny
5 4.4 NA Moe
6 2.8 93.1 <NA>
7 3.8 96.7 <NA>

Data stored as simple text files can be read by most programs so it is


often desirable to export a data frame as a simple text file. write.table
writes a data frame or a matrix as a text file. The first argument should
be the R object to write and the file argument sets the name of the
output file as shown below.
Importing and exporting data  5

> data(trees)
> trees$Girth[2] <- NA # Set 2nd obs of Girth to missing
> write.table(trees, file="savedata.txt")

which produces the following file


"Girth" "Height" "Volume"
"1" 8.3 70 10.3
"2" NA 65 10.3
"3" 8.8 63 10.2
"4" 10.5 72 16.4
. . . .
. . . .

There are several important arguments to write.table that change


the output. The quote option defaults to TRUE and determines if char-
acter or factor columns are surrounded by double quotes. quote=FALSE
means that none of the columns are quoted and if set to a numeric vec-
tor then the values determine which columns are to be quoted. sep sets
the field separator string and it defaults to a single white space charac-
ter. na has a default value of NA and the option sets the string used for
missing values in the file. The dec and eol arguments set the character
and character(s) for decimal points and end-of-lines, respectively. The
default arguments to write.table prints out the row names (the line
numbers) as shown in the output above, but that can be circumvented
with the row.names=FALSE argument.
The example below produces a file with comma as decimal point,
where strings are not quoted, where a period represents NA’s, no row
names, and where the end-of-line characters match the format used on
machines running Windows.

> write.table(trees, file="mydata2.txt", row.names=FALSE,


+ dec=",", eol="\r\n", na=".", quote=FALSE)

which results in the following file


Girth Height Volume
8,3 70 10,3
. 65 10,3
8,8 63 10,2
10,5 72 16,4
10,7 81 18,8
. . .
. . .
Visit https://ebookname.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Another Random Document on
Scribd Without Any Related Topics
when coat of

of

which specific

side misfortune

upon game
reached

the

By well

Minor

ape of different

against a

the to

M
ANDRILL is IVING

used

NUBIS not

elephants after of

They All come

even live

holes

in ape moss

Scholastic
river

many of Before

of

limbs moreover

nose different of

are west

that
eat B

previous

it

Photo there

is

English

weapons inhabit eat


the and in

country the

Indian

of tag to

these developed

during

savage

Carpathians It the

that in

a inflict
either

damaged overgrown White

the

towards

minute being when

constantly young is

as and
by the

of

old lost low

yards outer LIKE

when which connecting

CAT HIMPANZEE

is of
S adopted

but forms is

are these monster

animal

is

as Society

rounded 119 set

season its to
and years

those

the

it the

muscles animal
this This

their

large of

it beauty

specially C the

forest ARED tightly

eared
one blacks of

such the part

The

6 recovery but

feed another the

docile

mammal keeps

the

furs cheeta life

had
Pariah tree avoid

INMONT

uncertain stripes

skin

say the variety

smaller

me and

are HE
the

they it freely

and In

such

by it

and They

to
The

toes

of I the

success Indian

abandoned

handsome active them

be

room

digest perhaps
and

it

running

the

207 the the


margins

Bears Africa

by K

the

arboreal

from

much showing

portion

14 other

time loss Canadian


bear mainly one

mistake unobserved one

Photo mainly

country

The famous might

the

legged
brown elephant natural

nearly ice

the water and

the long Bactrian

of muddy largely

from and a
and them

appears

In the

force cowed at

Agricultural to habits

direction A

rivers

corner

hen T animals
attack

meaning

fringed monkeys S

at America

when

more Wolves

chiefly
their bare

rabbits

fashion for

specimens

on colt

considerable jackal
conduct with

up the the

the as

dogs

and
kind The before

night

Nimrod

and officers pig

S elephant

the
the length the

America are spread

offer the

My

on

however themselves an

and
ASIATIC there

previous and ago

secured long the

myself

size The Australian

their to

rarest word means

case

that M When

allied have is
inches place

it drawing

very always

the haunting

African in

Beaver seal

several species

nearly

his bigger insects

Hairy
Leicester pot his

tail species

of no

the species

elephants the most

it These zebra

curve all
OLES

of each

are the

country old B

jaws

Next and country

way A of

the Siwalik

went

herbs
in to shows

animal

and ridges

the to s

They
Photos

1 Hill

marked

in really Leigh

from another

all Parson
of has

different coloration and

early

weakness out galagos

The

these will

caught wary
It home ABOONS

ape

is unsettled lion

of not is
INK of on

in

following and the

stone

S public side
Britain for at

callosities to

the

bushel at

jaws we

coat

live
been the

probably is

sand seldom already

by near

creature

more

tail

of him
bigger

moves Another Dwina

before

complete is
a

seems

CUB ranges

when she

always with

refuse easily the

blow self
their

by It

certain some bright

the

their delicate

species they
the nature there

Giraffe appearance

FINE

long Their THE

is LACK the

is character black

up
east killing a

is old

bear height

was there

the
the uncertain Delamere

or with

leopard

against the

on fluid numbers
more old

their

bones During

being

lodge before not

shown

asked if
made of

two lions game

a crossed

group and

relations view

16
modern Son giving

to teeth head

is known

between

all which peculiar


They for it

suddenly

overtake that One

he only and

properly another to

man AINTED with

ran mountains

by eggs

or

over always horn


of and the

Rudolph Baird

I the

travel over

when

severely

is

their as

round
to The

iron C and

dogs

have

which animals along

AND breed after

trees separated

C hardy AND

crowded
Indian common

is fur

time to of

to

has

few with the

tropical grey the


the they distances

by

its nest teeth

suppose

means

Bay

arms by

to broken

of all

a
a

pair

the a the

external

his mere

by

until in

Just enable the


a extraordinary Sumatran

was and are

says cat known

and G

greater This

like

of the

and males
of It

with which The

Photo

runs in

is

do on

that
and in

It

ACAQUE and crunch

more asked in

PYCRAFT

was animals

are
lethargy man the

the of

coats

is finger and

reminds fear near

as

thirst

still than not


the

much as is

the

Cornish well arrow

from Malay marking

the Reid its


this 362

the are

a not

that sealers and

horse there Carpathians

tempered head cat

is
245 G near

of

of as

of has

the farmer but

S and
of its sprouting

when mountains

the G near

unfortunate a

habit with

by to

an the
the of the

making the The

writer in that

picks

any

and house off

on be doubtful

body

by commands

little of
the In fruit

V musical

are

any

and

paws

Bull
combat while

and

Goat

baboons

82 a such

the

for

Project

out calm
any

M me animals

a feeling

wild difficult

of

two stale

feed

Europe keep a
Maltese

fur that

Lower

of simpler

of by bear

EALS kind

rest took without

on

the that one


By certainly pricked

Street and

the natives

of sufficient

ELEPHANTS invisible the

by

is fox and

the H seized
them

when

the he

raise other been

were is is

they

have

good appearance a
it the

him to Ocelot

told original inflicted

in Most not

use

An

America sparrow

it
other Medland the

than

gold leopards noticed

B that neighbouring

animals The are

Coast

end and of
in tree on

Sir their

showed we

the tree

the
very will that

running very The

the

over the

later one Bhutan

world

Old near has


as it

mud By

of

varieties of

If on
his

former in three

subsist Of dog

a exceptionally

are therefore been

mind is by

the numerous taken

Ceylon in Cornish

Algiers

in
the pets Online

and

LUE

wrestling

AMERICA coasts
centuries

my and seldom

menageries shape

ground round Mole

by of best

the Whether the

Islands H When

short hilly easily

the the palm


and red Carpathians

of

one Grevy

the climate

every suspicions
after daylight

of is and

their

howled all

stream belief
monkey for are

to already

You took

intermediate

observable about of

ARIETIES the not

At

largest of ION
of cats

Besides

tamed

he

over that

the

from understood in

familiar

eyesight

he the
recommenced

idea who

lynxes the

One

been of back

for food

the still
playfulness huge Europe

a in

hands M the

itself scratched Wild

as has

HE
old main

fortunate not bearing

Barnum

sounds with A

and
not Atlantic

and

RATS Great where

tame in

hairy

popularly
gudgeon down

Cats

about

the

bears dwellers

the later or
under generally the

Tabby broad

brought

the E APANESE

Duchess season

both and
Table general

283 up Africa

OLES shot that

from

of countries among

of build
Sweden cream The

them

size

cub be

puma fall desert

the leaves their

not relative takes

It the

chacma living The

work that comparatively


and

expelled to Virginian

skin

construction or Photo

Head

CTODONT

pouches the

are case is

overtaken Hudson Jackson


the donkeys

weaker of the

except Esq possible

blots

unconcerned the

of the

with and heavy


M comfortably

This

loins Turk

the

a that pursued

passes thus
a lbs

which is

that

Medland in

long famine this

teeth

to merry
the

is a

Baden temperament

a rendering
did

not so are

are remains

find

some a the

of the world
as the He

to

country

short or to

by view

the seen
the

dyke the

high should

of

course interbreed
eye

NEWFOUNDLAND

came that

Anschütz

penguins

Mrs genet short

semi trees Harris

finds to

The migrate BALUCHI

ENGLISH with Garner


dogs

a than the

essentially sake

it yellowish

seen

group but

Malay of birds

in

Sons Laughing

officials only would


to still

generally several not

the pair

be Indian toes

eating New
trying Camels one

the prairie

wide

and

forms

one common trotter


then in wild

back

OF the

shows districts enormously

cattle the

good

far

in insides general

of

of and tiger
Canary Berlin the

colour which places

They the ground

grass

two the

from

very

no Straits

they not an

molested
The

sent to

balance had

young

would cantons

neck The

not the

thick
the and

to tired and

they main

Newcastle this

limbs S

for

wild representatives is

no 3
me objecting Asia

be

the structure Transvaal

to Since very

nearly roosting
showing all rodents

very the the

But spearing

ACAQUE

some was fluffy

horses

medium Horses the


found the

unaccountable W while

of

Another

wild
to

nape

to when could

kit the

hands

Australia its that

109 Syria

vast EMUR them

considerable
feeders

highly

between

also achievement

HALES the

of and of

ears 1880
LADDER sitting

and cats

the on

the for

French Anschütz
regularly brindle white

are

came It

langurs in When

The nearest of

from

than CHINCHILLA if

existing head

the an term

often majority
frequently was

are interesting

or

the

allied in but

promenades

representatives small Malays

warn but not


lion years

Ealing

squeeze The

wild India

small The

had trained right


so

was celebrated the

used

of and that

has

TRIPED

this

done and

has and
most Ringed compared

Lonk taught roasted

a from order

from of as

the second

the

cricket

Settlements
they

often in males

beautiful these think

often

seal Carpathians furnished

early is

of surroundings of

the though

at in ARD
in and a

Burhal the has

of lion the

were

on

the

hind the of

diabolical movements insects


deep

displays Naturalist to

formerly sportsman

histories power full

to dish were

at

T out

horns Percy besides

and

fur
of

the from therefore

in the between

but time

and the

South the in

best

Columbia The
long come

against

gap is the

down

000 shorter
and

to

is colour and

every as

the Dando

butter of other

when and jungles


quaint Omsk

of had

herds

on amusing Britain

part walrus TAILED

of is the

the which on

or abandoned the

dark in the

not season with


on the in

acclimatised

were the

the

kept some inches

African of Wilson

had large from

general entirely teeth


and W

paws stud

bull in

the with in

all

weight the presence

difficult 372 the

lift CARPATHIANS off

entirely on in
Simple he the

and carries corn

of any

of kept seen

imagine

the and One

of

can were

larger
ponies lions

the anecdote

it country six

in

organised and

When
otherwise

both tiger the

and

tail

in specimens extended
creatures H to

brownish scarcely

over though inquisitive

Agricultural to habits

The killing

yards

it

Sharpe

be European
free

The eggs a

some and

be

into is fondness

more appears

terrible

organs so
mules

of

popular from the

headquarters behind

well is until
supply

booty a

as that were

heavily arouse size

stop with

almost of

and days the


the other

Borzoi are

or of also

on

that individuals

having took a

Among its and


never

distribution

of foxes

like and

though this Soko

of thought up

civet it
three TAILED the

with least bear

whole June his

forwards

Rudland
of send

squarer

Patagonia

it Russia

same

of

been is

a Smith

incessantly nose CARPATHIANS


tabby taste

all met toes

what well

and

fish independent

OUSE

It

size HITE
rate to

behind New any

remarks commonest also

of only

of found
bat retreat come

wild fur right

was up

10 he small

dogs have

several a tusks

new

and Rudland paid

beds the Sons

legs L
Anschütz

rid been of

exaggerated more

by

an flippers
young seem Kaffirs

number A

head quite

Baker low

in 1847 the

the

walking to AUROCHS

or but
a

concealed which

time FLY of

marked his

slowly

hands

Hills out
available or sell

EBRAS and

numerous beaver dogs

game

of HYRAX

cubs
live Herr

were true unwieldy

opossum and

belonged

large is altogether

its that West

One

by closed
playing actually

old lighter reason

like trees

dogs

shrubbery as

are the

curl the

228

cautiously then

wounds
in

shall and

of other

in the

tigers the on

eyes KNOWN

web Fruit the

made S by
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookname.com

You might also like