15.1 C
London
Friday, July 5, 2024
HomeStatistics TutorialRHow to Remove Rows in R (With Examples)

How to Remove Rows in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to remove specific row numbers in R:

#remove 4th row
new_df #remove 2nd through 4th row
new_df #remove 1st, 2nd, and 4th row
new_df 

You can use the following syntax to remove rows that don’t meet specific conditions:

#only keep rows where col1 value is less than 10 and col2 value is less than 6
new_df 10 & col26)

And you can use the following syntax to remove rows with an NA value in any column:

#remove rows with NA value in any column
new_df omit(df)

The following examples show how to use each of these functions in practice.

Example 1: Remove Rows by Number

The following code shows how to remove rows by specific row numbers in R:

#create data frame
df frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, 8, 9, 25),
                 rebs=c(3, 3, 6, 5, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4
5      E  25    8     NA

#remove 4th row
df[-c(4), ]

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
5      E  25    8     NA

#remove 2nd through 4th row
df[-c(2:4), ]

  player pts rebs blocks
1      A  17    3      1
5      E  25    8     NA

#remove 1st, 2nd, and 4th row
df[-c(1, 2, 4), ]

  player pts rebs blocks
3      C   8    6      2
5      E  25    8     NA

Example 2: Remove Rows by Condition

The following code shows how to remove rows that don’t meet a specific condition:

#create data frame
df frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, 8, 9, 25),
                 rebs=c(3, 3, 6, 5, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4
5      E  25    8     NA

#only keep rows where pts is less than 10 and rebs is less than 6
subset(df, pts10 & rebs6)

  player pts rebs blocks
4      D   9    5      4

Example 3: Remove Rows with NA Values

The following code shows how to remove rows with a NA value in any row:

#create data frame
df frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, 8, 9, 25),
                 rebs=c(3, 3, 6, 5, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4
5      E  25    8     NA

#remove rows with NA value in any row:
na.omit(df)

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4

Additional Resources

How to Append Rows to a Data Frame in R
How to Remove Duplicate Rows in R
How to Sum Specific Rows in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories