arrow, business, financial

Take away Rows from Information Body In response to Situation

Posted on
banner 336x280

You’ll significance the subset() serve as to take away rows with sure values in an information body in R:

#best stock rows the place col1 worth is not up to 10 and col2 worth is not up to 8
new_df <- subset(df, col1<10 & col2<8) 

Refer to examples display tips on how to significance this syntax in apply with please see information body:

banner 468x60
#manufacture information body
df <- information.body(a=c(1, 3, 4, 6, 8, 9),
                 b=c(7, 8, 8, 7, 13, 16),
                 c=c(11, 13, 13, 18, 19, 22),
                 d=c(12, 16, 18, 22, 29, 38))

#view information body
df

  a  b  c  d
1 1  7 11 12
2 3  8 13 16
3 4  8 13 18
4 6  7 18 22
5 8 13 19 29
6 9 16 22 38

Instance 1: Take away Rows Equivalent to Some Worth

Refer to code presentations how to take away all rows the place the price in column ‘c’ is the same as 13:

#take away rows the place column 'c' is the same as 13
new_df <- subset(df, c != 13) 

#view up to date information body
new_df

  a  b  c  d
1 1  7 11 12
4 6  7 18 22
5 8 13 19 29
6 9 16 22 38

Instance 2: Take away Rows Equivalent to Considered one of A number of Values

Refer to code presentations how to take away all rows the place the price in column ‘b’ is the same as 7 or 8:

#take away rows the place worth in column b is the same as 7 or 8
new_df <- subset(df, !(b %in% c(7, 8)))

#view up to date information body
new_df

  a  b  c  d
5 8 13 19 29
6 9 16 22 38

Instance 3: Take away Rows In response to A couple of Statuses

Refer to code presentations how to take away all rows the place the price in column ‘b’ is the same as 7 or the place the price in column ‘d’ is the same as 38:

#take away rows the place worth in column b is 7 or worth in column d is 38
new_df <- subset(df, b != 7 & d != 38)

#view up to date information body
new_df

  a  b  c  d
2 3  8 13 16
3 4  8 13 18
5 8 13 19 29

Extra Sources

The way to Take away Reproduction Rows in R
The way to Significance %in% Operator in R
The way to Recode Values in R

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *