Remove Negative Values from Vector & Data Frame in R (2 Examples)

 

In this R tutorial you’ll learn how to delete negative data.

The article is structured as follows:

Let’s dive into it:

 

Example 1: Delete Negative Elements from Vector

In Example 1, I’ll explain how to remove negative values from a vector object in R.

For this example, we first have to create an example vector:

vec <- c(1, 5, - 3, 2, 2, - 5, 7)    # Create example vector
vec                                  # Print example vector
# [1]  1  5 -3  2  2 -5  7

Next, we can use a logical condition to keep only those vector elements that are larger than or equal to zero:

vec_new <- vec[vec >= 0]             # Remove negative vector elements
vec_new                              # Print updated vector
# [1] 1 5 2 2 7

As you can see, the previous R code has returned a new vector object containing only positive numbers.

 

Example 2: Delete Rows with Negative Values from Data Frame

In this example, I’ll demonstrate how to delete all rows with negative values in a data frame.

First, we have to create an example data frame in R:

data <- data.frame(x1 = - 2:4,       # Create example data frame
                   x2 = 5:- 1,
                   x3 = 1:7)
data                                 # Print example data frame

 

table 1 data frame remove negative values from vector data frame r

 

As shown in Table 1, we have created a new data frame containing seven rows and three columns using the previous R code. Some of the data cells in our data set are negative.

In the next step, we can use a logical condition to replace all negative values by NA:

data_new1 <- data                    # Duplicate data frame
data_new1[data_new1 < 0] <- NA       # Replace negative values by NA
data_new1                            # Print updated data frame

 

table 2 data frame remove negative values from vector data frame r

 

As shown in Table 2, we have managed to create another data frame that contains NA values at each position where our input data frame contained a negative integer number.

Now, we can remove all rows with NA values from this updated data frame to create another data frame without those rows. For this task, we can apply the na.omit function as shown below:

data_new2 <- na.omit(data_new1)      # Remove rows with NA values
data_new2                            # Print updated data frame

 

table 3 data frame remove negative values from vector data frame r

 

In Table 3 you can see that we have created a final data set containing only rows with positive integers.

 

Video & Further Resources

Do you need further info on the contents of this article? Then you might want to have a look at the following video instruction that I have published on my YouTube channel. I’m explaining the R programming codes of this article in the video:

 

The YouTube video will be added soon.

 

In addition, you might have a look at the related R articles on this website.

 

You have learned in this tutorial how to exclude and remove negative values in the R programming language. In case you have any additional comments or questions, please tell me about it in the comments section below. Besides that, please subscribe to my email newsletter for updates on the newest posts.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top