# Missing values

**R-exercises**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Today we’re training how to handle missing values in a data set. Before starting the exercises, please first read section 2.5 of An Introduction to R.

Solutions are available here.

**Exercise 1**

If ` X `

what will be the output for the R statement ` length(X) `

**Exercise 2**

If ` X = c(NA,3,14,NA,33,17,NA,41) `

write some R code that will remove all occurrences of NA in X.

a. `X[!is.na(X)]`

b. `X[is.na(X)]`

c. `X[X==NA]= 0`

**Exercise 3**

If ` Y = c(1,3,12,NA,33,7,NA,21) `

what R statement will replace all occurrences of NA with 11?

a. `Y[Y==NA]= 11`

b. `Y[is.na(Y)]= 11`

c. `Y[Y==11] = NA`

**Exercise 4**

If ` X = c(34,33,65,37,89,NA,43,NA,11,NA,23,NA) `

then what will count the number of occurrences of NA in X?

a. `sum(X==NA)`

b. `sum(X == NA, is.na(X))`

c. `sum(is.na(X))`

**Exercise 5**

Consider the following vector ` W `

Write some R code that will return `TRUE`

for value of `W`

missing in the vector.

**Exercise 6**

Load ‘Orange’ dataset from R using the command ` data(Orange) `

. Replace all values of `age=118`

to NA.

**Exercise 7**

Consider the following vector ` A `

.

Write some R code that will calculate the mean of A without the missing value.

**Exercise 8**

Let:

` c1 `

;

` c2 `

;

` c3 `

.

If ` X `

, write a code that will display all rows with missing values.

**Exercise 9**

Consider the following data obtained from ` df `

Write some R code that will return a data frame which removes all rows with NA values in `Name`

column

**Exercise 10**

Consider the following data obtained from ` df `

Write some R code that will remove all rows with NA values and give the following output

```
```Name Sales Price

2 Joseph 18 52

3 Martin 21 33

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.