-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathna.Rmd
44 lines (31 loc) · 842 Bytes
/
na.Rmd
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
# Missing values
**NA** (Not Available) is a recognized element in R.
* Finding missing values in a vector
```{r, eval=F}
# Create vector
x <- c(4, 2, 7, NA)
# Find missing values in vector:
is.na(x)
# Remove missing values
na.omit(x)
x[ !is.na(x) ]
```
* Some functions can deal with NAs, either by default, or with specific arguments:
```{r, eval=F}
x <- c(4, 2, 7, NA)
# default arguments
mean(x)
# set na.rm=TRUE
mean(x, na.rm=TRUE)
```
* In a matrix or a data frame, keep only rows where there are no NA values:
```{r, eval=F}
# Create matrix with some NA values
mydata <- matrix(c(1:10, NA, 12:2, NA, 15:20, NA), ncol=3)
# Keep only rows without NAs
mydata[complete.cases(mydata), ]
# or
na.omit(mydata)
```
<br>
Check this [R blogger post on missing/null values](https://www.r-bloggers.com/r-null-values-null-na-nan-inf/)