Using R and RStudio For Data Management
Using R and RStudio For Data Management
R and
Using
RStudio
for Data Management,
Statistical Analysis
and Graphics
Second Edition
Nicholas J. Horton
Department of Mathematics and Statistics
Amherst College
Massachusetts, U.S.A.
Ken Kleinman
Department of Population Medicine
Harvard Medical School and
Harvard Pilgrim Health Care Institute
Boston, Massachusetts, U.S.A.
i i
i i
i i
Contents
v
i i
i i
i i
vi CONTENTS
2 Data management 11
2.1 Structure and metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.1 Access variables from a dataset . . . . . . . . . . . . . . . . . . . . . 11
2.1.2 Names of variables and their types . . . . . . . . . . . . . . . . . . . 11
2.1.3 Values of variables in a dataset . . . . . . . . . . . . . . . . . . . . . 12
2.1.4 Label variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.5 Add comment to a dataset or variable . . . . . . . . . . . . . . . . . 12
2.2 Derived variables and data manipulation . . . . . . . . . . . . . . . . . . . . 12
2.2.1 Add derived variable to a dataset . . . . . . . . . . . . . . . . . . . . 13
2.2.2 Rename variables in a dataset . . . . . . . . . . . . . . . . . . . . . . 13
2.2.3 Create string variables from numeric variables . . . . . . . . . . . . . 13
2.2.4 Create categorical variables from continuous variables . . . . . . . . 13
2.2.5 Recode a categorical variable . . . . . . . . . . . . . . . . . . . . . . 14
2.2.6 Create a categorical variable using logic . . . . . . . . . . . . . . . . 14
2.2.7 Create numeric variables from string variables . . . . . . . . . . . . . 15
2.2.8 Extract characters from string variables . . . . . . . . . . . . . . . . 15
2.2.9 Length of string variables . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.10 Concatenate string variables . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.11 Set operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.12 Find strings within string variables . . . . . . . . . . . . . . . . . . . 16
2.2.13 Find approximate strings . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.14 Replace strings within string variables . . . . . . . . . . . . . . . . . 17
2.2.15 Split strings into multiple strings . . . . . . . . . . . . . . . . . . . . 17
2.2.16 Remove spaces around string variables . . . . . . . . . . . . . . . . . 17
2.2.17 Convert strings from upper to lower case . . . . . . . . . . . . . . . 17
2.2.18 Create lagged variable . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2.19 Formatting values of variables . . . . . . . . . . . . . . . . . . . . . . 18
2.2.20 Perl interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.21 Accessing databases using SQL . . . . . . . . . . . . . . . . . . . . . 18
2.3 Merging, combining, and subsetting datasets . . . . . . . . . . . . . . . . . 19
2.3.1 Subsetting observations . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.2 Drop or keep variables in a dataset . . . . . . . . . . . . . . . . . . . 19
2.3.3 Random sample of a dataset . . . . . . . . . . . . . . . . . . . . . . 20
2.3.4 Observation number . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3.5 Keep unique values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3.6 Identify duplicated values . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3.7 Convert from wide to long (tall) format . . . . . . . . . . . . . . . . 21
2.3.8 Convert from long (tall) to wide format . . . . . . . . . . . . . . . . 21
2.3.9 Concatenate and stack datasets . . . . . . . . . . . . . . . . . . . . . 22
2.3.10 Sort datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3.11 Merge datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4 Date and time variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.4.1 Create date variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.4.2 Extract weekday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4.3 Extract month . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4.4 Extract year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4.5 Extract quarter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4.6 Create time variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5 Further resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6.1 Data input and output . . . . . . . . . . . . . . . . . . . . . . . . . . 25
i i
i i
i i
CONTENTS vii
i i
i i
i i
viii CONTENTS
i i
i i
i i
CONTENTS ix
i i
i i
i i
x CONTENTS
6.6.8 Contrasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
i i
i i
i i
CONTENTS xi
i i
i i
i i
xii CONTENTS
i i
i i
i i
CONTENTS xiii
10 Simulation 155
10.1 Generating data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
10.1.1 Generate categorical data . . . . . . . . . . . . . . . . . . . . . . . . 155
10.1.2 Generate data from a logistic regression . . . . . . . . . . . . . . . . 156
10.1.3 Generate data from a generalized linear mixed model . . . . . . . . . 156
10.1.4 Generate correlated binary data . . . . . . . . . . . . . . . . . . . . 157
10.1.5 Generate data from a Cox model . . . . . . . . . . . . . . . . . . . . 158
10.1.6 Sampling from a challenging distribution . . . . . . . . . . . . . . . 159
10.2 Simulation applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
10.2.1 Simulation study of Student’s t-test . . . . . . . . . . . . . . . . . . 161
10.2.2 Diploma (or hat-check) problem . . . . . . . . . . . . . . . . . . . . 162
10.2.3 Monty Hall problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
10.2.4 Censored survival . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
10.3 Further resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
i i
i i
i i
xiv CONTENTS
i i
i i
i i
CONTENTS xv
C References 243
D Indices 255
D.1 Subject index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
D.2 R index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
i i
i i