如果特定列中的值小于另一列中的值,则仅选择行 [英] Select only rows if its value in a particular column is less than the value in the other column

查看:53
本文介绍了如果特定列中的值小于另一列中的值,则仅选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 并且需要选择年龄(死亡年龄)小于或等于 laclen(哺乳期长度)的行.我正在尝试创建一个新的数据框,只包含行/id,其中列年龄"的值小于其相应的laclength"值.

I am using R and need to select rows with aged (age of death) less than or equal to laclen (lactation length). I am trying to create a new data frame to only include rows/ids whereby the value of column'aged' is less than its corresponding 'laclength' value.

df:
 id1   id2    laclen    aged
9830  64526    26       6 
7609  64547    28       0 
9925  64551     3       0 
9922  64551     3       5 
9916  64551     3       8 
9917  64551     3       8 
9914  64551     3       2 

新的数据框应该是这样的:

the new data frame should look like this:

dfnew:
id1   id2    laclen    aged
9830  64526    26       6 
7609  64547    28       0 
9925  64551     3       0 
9914  64551     3       2

任何帮助将不胜感激!

巴松

推荐答案

df[df$aged <= df$laclen, ] 

应该可以解决问题.方括号允许您根据逻辑表达式进行索引.

Should do the trick. The square brackets allow you to index based on a logical expression.

这篇关于如果特定列中的值小于另一列中的值,则仅选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆