# Delete Negative Elements in Vector & Data Frame in R (2 Examples)

In this tutorial you’ll learn how to delete negative data in the R programming language.

## Example 1: Removing All Negative Elements from a Vector Object

x <- c(- 1, 2, - 3, 4, - 5, 6) # Create example vector x # Print example vector # [1] -1 2 -3 4 -5 6 |

x <- c(- 1, 2, - 3, 4, - 5, 6) # Create example vector x # Print example vector # [1] -1 2 -3 4 -5 6

x[x >= 0] # Remove negative vector elements # [1] 2 4 6 |

x[x >= 0] # Remove negative vector elements # [1] 2 4 6

## Example 2: Removing All Rows Containing Negative Values from a Data Frame

my_df <- data.frame(A = - 3:5, # Constructing example data B = 5:- 3, C = "x") my_df # A B C # 1 -3 5 x # 2 -2 4 x # 3 -1 3 x # 4 0 2 x # 5 1 1 x # 6 2 0 x # 7 3 -1 x # 8 4 -2 x # 9 5 -3 x |

my_df <- data.frame(A = - 3:5, # Constructing example data B = 5:- 3, C = "x") my_df # A B C # 1 -3 5 x # 2 -2 4 x # 3 -1 3 x # 4 0 2 x # 5 1 1 x # 6 2 0 x # 7 3 -1 x # 8 4 -2 x # 9 5 -3 x

my_df_pos <- my_df[rowSums(my_df < 0) == 0, ] # Remove negative rows my_df_pos # A B C # 4 0 2 x # 5 1 1 x # 6 2 0 x |

my_df_pos <- my_df[rowSums(my_df < 0) == 0, ] # Remove negative rows my_df_pos # A B C # 4 0 2 x # 5 1 1 x # 6 2 0 x

## Related Articles & Further Resources

You may find some further R programming tutorials on topics such as variables, RStudio, and missing data in the following list: