Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

r - Delete nth row if the sum of columns b and c exceeds the nth value of column a

How can i delete all rows in which the sum of columns b and c exceeds the value of a.

For example : First row has column a value = 50.000 and column b value = 30.000 and column c value = 30.000 . Column b + c in first row = 60.000 which is greater than column a which value is 50.000 so i want to delete it. How can i do this in R?

I want to do this for all the rows in the dataset.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can create a logical condition by summing the columns 'b' and 'c', checking the output is less than or equal to 'a' and use that logical vector to subset the rows.

df[with(df, (b + c) <=  a),]
#   a  b  c
#2 70 20 30

NOTE: The results are based on the OP's description. This is downvoted unnecessarily.

data

df <- data.frame(a = c(50, 70), b = c(30, 20), c = c(30, 30))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...