ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 06.04.2021

Просмотров: 863

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
background image

The R Inferno

Patrick Burns

1

30th April 2011

1

This document resides in the tutorial section of http://www.burns-stat.com. More

elementary material on R may also be found there. S+ is a registered trademark of
TIBCO Software Inc. The author thanks D. Alighieri for useful comments.


background image

Contents

Contents

1

List of Figures

6

List of Tables

7

1 Falling into the Floating Point Trap

9

2 Growing Objects

12

3 Failing to Vectorize

17

3.1

Subscripting

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

3.2

Vectorized if

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

3.3

Vectorization impossible

. . . . . . . . . . . . . . . . . . . . . . .

22

4 Over-Vectorizing

24

5 Not Writing Functions

27

5.1

Abstraction

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

5.2

Simplicity

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

5.3

Consistency

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

6 Doing Global Assignments

35

7 Tripping on Object Orientation

38

7.1

S3 methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

38

7.1.1

generic functions

. . . . . . . . . . . . . . . . . . . . . . .

39

7.1.2

methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

7.1.3

inheritance

. . . . . . . . . . . . . . . . . . . . . . . . . .

40

7.2

S4 methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

40

7.2.1

multiple dispatch

. . . . . . . . . . . . . . . . . . . . . . .

40

7.2.2

S4 structure

. . . . . . . . . . . . . . . . . . . . . . . . . .

41

7.2.3

discussion

. . . . . . . . . . . . . . . . . . . . . . . . . . .

42

7.3

Namespaces

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

42

1


background image

CONTENTS

CONTENTS

8 Believing It Does as Intended

44

8.1

Ghosts

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

8.1.1

differences with S+

. . . . . . . . . . . . . . . . . . . . . .

46

8.1.2

package functionality

. . . . . . . . . . . . . . . . . . . . .

46

8.1.3

precedence

. . . . . . . . . . . . . . . . . . . . . . . . . .

47

8.1.4

equality of missing values

. . . . . . . . . . . . . . . . . .

48

8.1.5

testing NULL

. . . . . . . . . . . . . . . . . . . . . . . . .

48

8.1.6

membership

. . . . . . . . . . . . . . . . . . . . . . . . . .

49

8.1.7

multiple tests

. . . . . . . . . . . . . . . . . . . . . . . . .

49

8.1.8

coercion

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

8.1.9

comparison under coercion

. . . . . . . . . . . . . . . . .

51

8.1.10 parentheses in the right places

. . . . . . . . . . . . . . .

51

8.1.11 excluding named items

. . . . . . . . . . . . . . . . . . . .

51

8.1.12 excluding missing values

. . . . . . . . . . . . . . . . . . .

52

8.1.13 negative nothing is something

. . . . . . . . . . . . . . . .

52

8.1.14 but zero can be nothing

. . . . . . . . . . . . . . . . . . .

53

8.1.15 something plus nothing is nothing

. . . . . . . . . . . . .

53

8.1.16 sum of nothing is zero

. . . . . . . . . . . . . . . . . . . .

54

8.1.17 the methods shuffle

. . . . . . . . . . . . . . . . . . . . . .

54

8.1.18 first match only

. . . . . . . . . . . . . . . . . . . . . . . .

55

8.1.19 first match only (reprise)

. . . . . . . . . . . . . . . . . .

55

8.1.20 partial matching can partially confuse

. . . . . . . . . . .

56

8.1.21 no partial match assignments

. . . . . . . . . . . . . . . .

58

8.1.22 cat versus print

. . . . . . . . . . . . . . . . . . . . . . . .

58

8.1.23 backslashes

. . . . . . . . . . . . . . . . . . . . . . . . . .

59

8.1.24 internationalization

. . . . . . . . . . . . . . . . . . . . . .

59

8.1.25 paths in Windows

. . . . . . . . . . . . . . . . . . . . . .

60

8.1.26 quotes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60

8.1.27 backquotes

. . . . . . . . . . . . . . . . . . . . . . . . . .

61

8.1.28 disappearing attributes

. . . . . . . . . . . . . . . . . . .

62

8.1.29 disappearing attributes (reprise)

. . . . . . . . . . . . . .

62

8.1.30 when space matters

. . . . . . . . . . . . . . . . . . . . .

62

8.1.31 multiple comparisons

. . . . . . . . . . . . . . . . . . . . .

63

8.1.32 name masking

. . . . . . . . . . . . . . . . . . . . . . . .

63

8.1.33 more sorting than sort

. . . . . . . . . . . . . . . . . . . .

63

8.1.34 sort.list not for lists

. . . . . . . . . . . . . . . . . . . . .

64

8.1.35 search list shuffle

. . . . . . . . . . . . . . . . . . . . . . .

64

8.1.36 source versus attach or load

. . . . . . . . . . . . . . . . .

64

8.1.37 string not the name

. . . . . . . . . . . . . . . . . . . . .

65

8.1.38 get a component

. . . . . . . . . . . . . . . . . . . . . . .

65

8.1.39 string not the name (encore)

. . . . . . . . . . . . . . . .

65

8.1.40 string not the name (yet again)

. . . . . . . . . . . . . . .

65

8.1.41 string not the name (still)

. . . . . . . . . . . . . . . . . .

66

8.1.42 name not the argument

. . . . . . . . . . . . . . . . . . .

66

8.1.43 unexpected else

. . . . . . . . . . . . . . . . . . . . . . . .

67

8.1.44 dropping dimensions

. . . . . . . . . . . . . . . . . . . . .

67

2


background image

CONTENTS

CONTENTS

8.1.45 drop data frames

. . . . . . . . . . . . . . . . . . . . . . .

68

8.1.46 losing row names

. . . . . . . . . . . . . . . . . . . . . . .

68

8.1.47 apply function returning a vector

. . . . . . . . . . . . . .

69

8.1.48 empty cells in tapply

. . . . . . . . . . . . . . . . . . . . .

69

8.1.49 arithmetic that mixes matrices and vectors

. . . . . . . .

70

8.1.50 single subscript of a data frame or array

. . . . . . . . . .

71

8.1.51 non-numeric argument

. . . . . . . . . . . . . . . . . . . .

71

8.1.52 round rounds to even

. . . . . . . . . . . . . . . . . . . .

71

8.1.53 creating empty lists

. . . . . . . . . . . . . . . . . . . . .

71

8.1.54 list subscripting

. . . . . . . . . . . . . . . . . . . . . . . .

72

8.1.55 NULL or delete

. . . . . . . . . . . . . . . . . . . . . . . .

73

8.1.56 disappearing components

. . . . . . . . . . . . . . . . . .

73

8.1.57 combining lists

. . . . . . . . . . . . . . . . . . . . . . . .

74

8.1.58 disappearing loop

. . . . . . . . . . . . . . . . . . . . . . .

74

8.1.59 limited iteration

. . . . . . . . . . . . . . . . . . . . . . .

74

8.1.60 too much iteration

. . . . . . . . . . . . . . . . . . . . . .

75

8.1.61 wrong iterate

. . . . . . . . . . . . . . . . . . . . . . . . .

75

8.1.62 wrong iterate (encore)

. . . . . . . . . . . . . . . . . . . .

75

8.1.63 wrong iterate (yet again)

. . . . . . . . . . . . . . . . . .

76

8.1.64 iterate is sacrosanct

. . . . . . . . . . . . . . . . . . . . .

76

8.1.65 wrong sequence

. . . . . . . . . . . . . . . . . . . . . . . .

76

8.1.66 empty string

. . . . . . . . . . . . . . . . . . . . . . . . .

76

8.1.67 NA the string

. . . . . . . . . . . . . . . . . . . . . . . . .

77

8.1.68 capitalization

. . . . . . . . . . . . . . . . . . . . . . . . .

78

8.1.69 scoping

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

8.1.70 scoping (encore)

. . . . . . . . . . . . . . . . . . . . . . .

78

8.2

Chimeras

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

80

8.2.1

numeric to factor to numeric

. . . . . . . . . . . . . . . .

82

8.2.2

cat factor

. . . . . . . . . . . . . . . . . . . . . . . . . . .

82

8.2.3

numeric to factor accidentally

. . . . . . . . . . . . . . . .

82

8.2.4

dropping factor levels

. . . . . . . . . . . . . . . . . . . .

83

8.2.5

combining levels

. . . . . . . . . . . . . . . . . . . . . . .

83

8.2.6

do not subscript with factors

. . . . . . . . . . . . . . . .

84

8.2.7

no go for factors in ifelse

. . . . . . . . . . . . . . . . . . .

84

8.2.8

no c for factors

. . . . . . . . . . . . . . . . . . . . . . . .

84

8.2.9

ordering in ordered

. . . . . . . . . . . . . . . . . . . . . .

85

8.2.10 labels and excluded levels

. . . . . . . . . . . . . . . . . .

85

8.2.11 is missing missing or missing?

. . . . . . . . . . . . . . . .

86

8.2.12 data frame to character

. . . . . . . . . . . . . . . . . . .

87

8.2.13 nonexistent value in subscript

. . . . . . . . . . . . . . . .

88

8.2.14 missing value in subscript

. . . . . . . . . . . . . . . . . .

88

8.2.15 all missing subscripts

. . . . . . . . . . . . . . . . . . . . .

89

8.2.16 missing value in if

. . . . . . . . . . . . . . . . . . . . . .

90

8.2.17 and and andand

. . . . . . . . . . . . . . . . . . . . . . .

90

8.2.18 equal and equalequal

. . . . . . . . . . . . . . . . . . . . .

90

8.2.19 is.integer

. . . . . . . . . . . . . . . . . . . . . . . . . . .

91

3


background image

CONTENTS

CONTENTS

8.2.20 is.numeric, as.numeric with integers

. . . . . . . . . . . .

91

8.2.21 is.matrix

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

92

8.2.22 max versus pmax

. . . . . . . . . . . . . . . . . . . . . . .

92

8.2.23 all.equal returns a surprising value

. . . . . . . . . . . . .

93

8.2.24 all.equal is not identical

. . . . . . . . . . . . . . . . . . .

93

8.2.25 identical really really means identical

. . . . . . . . . . . .

93

8.2.26 = is not a synonym of

<

-

. . . . . . . . . . . . . . . . . .

94

8.2.27 complex arithmetic

. . . . . . . . . . . . . . . . . . . . . .

94

8.2.28 complex is not numeric

. . . . . . . . . . . . . . . . . . .

94

8.2.29 nonstandard evaluation

. . . . . . . . . . . . . . . . . . .

95

8.2.30 help for for

. . . . . . . . . . . . . . . . . . . . . . . . . .

95

8.2.31 subset

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

8.2.32 = vs == in subset

. . . . . . . . . . . . . . . . . . . . . .

96

8.2.33 single sample switch

. . . . . . . . . . . . . . . . . . . . .

96

8.2.34 changing names of pieces

. . . . . . . . . . . . . . . . . .

97

8.2.35 a puzzle

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

97

8.2.36 another puzzle

. . . . . . . . . . . . . . . . . . . . . . . .

98

8.2.37 data frames vs matrices

. . . . . . . . . . . . . . . . . . .

98

8.2.38 apply not for data frames

. . . . . . . . . . . . . . . . . .

98

8.2.39 data frames vs matrices (reprise)

. . . . . . . . . . . . . .

98

8.2.40 names of data frames and matrices

. . . . . . . . . . . . .

99

8.2.41 conflicting column names

. . . . . . . . . . . . . . . . . .

99

8.2.42 cbind favors matrices

. . . . . . . . . . . . . . . . . . . . . 100

8.2.43 data frame equal number of rows

. . . . . . . . . . . . . . 100

8.2.44 matrices in data frames

. . . . . . . . . . . . . . . . . . . 100

8.3

Devils

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

8.3.1

read.table

. . . . . . . . . . . . . . . . . . . . . . . . . . . 101

8.3.2

read a table

. . . . . . . . . . . . . . . . . . . . . . . . . . 101

8.3.3

the missing, the whole missing and nothing but the missing

102

8.3.4

misquoting

. . . . . . . . . . . . . . . . . . . . . . . . . . 102

8.3.5

thymine is TRUE, female is FALSE

. . . . . . . . . . . . 102

8.3.6

whitespace is white

. . . . . . . . . . . . . . . . . . . . . . 104

8.3.7

extraneous fields

. . . . . . . . . . . . . . . . . . . . . . . 104

8.3.8

fill and extraneous fields

. . . . . . . . . . . . . . . . . . . 104

8.3.9

reading messy files

. . . . . . . . . . . . . . . . . . . . . . 105

8.3.10 imperfection of writing then reading

. . . . . . . . . . . . 105

8.3.11 non-vectorized function in integrate

. . . . . . . . . . . . 105

8.3.12 non-vectorized function in outer

. . . . . . . . . . . . . . 106

8.3.13 ignoring errors

. . . . . . . . . . . . . . . . . . . . . . . . 106

8.3.14 accidentally global

. . . . . . . . . . . . . . . . . . . . . . 107

8.3.15 handling ...

. . . . . . . . . . . . . . . . . . . . . . . . . . 107

8.3.16 laziness

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

8.3.17 lapply laziness

. . . . . . . . . . . . . . . . . . . . . . . . 108

8.3.18 invisibility cloak

. . . . . . . . . . . . . . . . . . . . . . . 109

8.3.19 evaluation of default arguments

. . . . . . . . . . . . . . . 109

8.3.20 sapply simplification

. . . . . . . . . . . . . . . . . . . . . 110

4