Select Page

# How to Solve R Error: missing values are not allowed in subscripted assignments of data frames

by | Programming, R, Tips

This error occurs when you attempt to subscript a data frame that contains NA values. You can solve this error by excluding the NA values during the subscript operation, for example:

`data[data\$col1<0 & !is.na(data\$col1),]\$col2`

This tutorial will go through the error in detail and how to solve it with code examples.

## Example

Consider the following 100 row data frame:

```set.seed(0)

data <- data.frame(x=c(NA, rnorm(99)), y=c(1:5))

The first variable contains an `NA` value and samples from the normal distribution. The second variable has five integer values that serve as categories. Let’s look at the head of the data frame:

```          x y
1         NA 1
2  1.2629543 2
3 -0.3262334 3
4  1.3297993 4
5  1.2724293 5
6  0.4146414 1```

Next, we will try to replace the value in the `x` column that are less than zero with zero.

`data[data\$x<0,]\$x <- 0`

Let’s run the code to see the result:

```Error in `[<-.data.frame`(`*tmp*`, data\$x < 0, , value = list(x = c(0,  :
missing values are not allowed in subscripted assignments of data frames```

The error occurs because the `x` column contains `NA` values.

### Solution

We can solve this error by using the `is.na` function in the subscript operation.

```data[data\$x<0 & !is.na(data\$x),]\$x <- 0

By using `!is.na()` we are excluding the `NA` values during the subscript operation. Let’s run the code to see the result:

```          x y
1         NA 1
2  1.2629543 2
3  0.0000000 3
4  1.3297993 4
5  1.2724293 5
6  0.4146414 1
7  0.0000000 2
8  0.0000000 3
9  0.0000000 4
10 0.0000000 5
11 2.4046534 1
12 0.7635935 2
13 0.0000000 3
14 0.0000000 4
15 0.0000000 5
16 0.0000000 1
17 0.0000000 2
18 0.2522234 3
19 0.0000000 4
20 0.4356833 5```

We successfully substituted the values less than zero with zero in the x column.

## Summary

Congratulations on reading to the end of this tutorial!

For further reading on R related errors, go to the articles:

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!