-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathr-programming-done-wrong.qmd
186 lines (131 loc) · 3.53 KB
/
r-programming-done-wrong.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# R Programming Done Wrong
The motivation behind this chapter is to provide a reference guide to the more
common mistakes when writing _R_ code.
## Beginners Luck
### Checking for Equality vs. Assignment
The most common error by far that affects programmers is making an assignment
when trying to check for equality (and vice versa)
```r
# Assigning in `if`
if(x = 42) { cat("Life!") }
## Error: unexpected '=' in "if(x ="
# Correct
if(x == 42) { cat("Life!") }
```
```r
# Equality Check instead of Assignment
x == 42
## No Error, but prints `TRUE` or `FALSE`
# Correct
x = 42
```
### Missing object
```r
x
Error: object 'x' not found
# Correct
x = 1
x
```
### `if` vectorization usage
As emphasis on vectorization grows, there is a tendency to compare two vectors
using the default `if()` instead of `ifelse()`
```r
x = 1:5
y = 2:6
if(x > y) { TRUE } else { FALSE }
## Warning messages:
## In if (x > y) { : the condition has length > 1 and only the first element will be used
# Correct, if element-wise comparison required
ifelse(x > y, TRUE, FALSE)
# Correct, if totality-comparison requested
if(all(x > y)) { TRUE } else { FALSE }
```
### Vector Recycling
Sometimes the length of vectors are not equal or the data does not divide evenly
or oddly when perform a vectorized computation.
```r
x = 1:5
y = 2:3
x + y
## Warning message:
## In x + y : longer object length is not a multiple
## of shorter object length
# Correct
x = 1:4
y = 2:3
x + y
# Repeats y twice
# 1 + 2, 2 + 3, 3 + 2, 4 + 3
```
## Omitting Symbols
### Mismatched curly brackets `{}`, parentheses `()`, or brackets `[]`
Often it is ideal to use parentheses or curly
brackets for order of operations, though this sometimes causes a mismatch. A
mismatch may also be present with the brackets subset operator `[]`
```r
2*(x + y))
## Error: unexpected ')' in "2*(x + y))"
2*{x + y}}
## Error: unexpected '}' in "2*{x + y}}"
x]1
## Error: unexpected ']' in "x]"
# Corrected
2*((x + y))
2*{{x + y}}
x[1]
```
### No Multiplication Symbol
When working on computations, sometimes we just "slip" and opt not to write a
multiplication sign thinking the interpreter can understand the context.
```{r no_multiplication, cache = TRUE, eval = FALSE, echo = TRUE}
2x+4
## Error: unexpected symbol in "2x"
# Correct
2*x + 4
```
### Manual Data Entry
Sometimes it's easier as we'll see next week to manually enter data.
The issue with this is sometimes you forget simple things like a `,`.
```{r manual_entry, cache = TRUE, eval = FALSE, echo = TRUE}
c(1, 2 3, 4)
## Error: unexpected numeric constant in "c(1,2 3"
# Correct
c(1, 2, 3, 4)
```
### Strings in character values
At times, there may come a need to place a quotation inside of a string. To do
this, requires using an escape character `\` or using `''` instead.
```r
"toad"princess"
## Error: unexpected symbol in ""toad"princess"
# Corrected
"toad\"princess"
'toad"princess'
```
## Special Values
### Handling Missing Value Operations
The `NA` character indicates the presence of a missing value.
These missing values can play havoc with computations.
```r
x = c(1,NA,2)
3 + x
# No Error, but: [1] 4 NA 5
sum(x)
# No Error, but: [1] NA
# Corrected
1 + na.omit(x) # Deletes NA
sum(x, na.rm = T) # Removes NA inside function
```
### Finiteness of Values
R can have some funky finiteness problems due to how `NA` values are created.
```r
x = c(NA,-Inf, Inf ,NaN)
is.na(x)
# No error, but: [1] TRUE FALSE FALSE TRUE
is.infinite(x)
# No error, but: [1] FALSE TRUE TRUE FALSE
# Correct
is.finite(x)
# [1] FALSE FALSE FALSE FALSE
```