[select] Select only rows if its value in a particular column is less than the value in the other column

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

Any help would be appreciated!

Bazon

This question is related to select r rows

The answer is


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

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


If you use dplyr package you can do:

library(dplyr)
filter(df, aged <= laclen)

You can also do

subset(df, aged <= laclen)

Examples related to select

Warning: Use the 'defaultValue' or 'value' props on <select> instead of setting 'selected' on <option> SQL query to check if a name begins and ends with a vowel Angular2 *ngFor in select list, set active based on string from object SQL: Two select statements in one query How to get selected value of a dropdown menu in ReactJS DATEDIFF function in Oracle How to filter an array of objects based on values in an inner array with jq? Select unique values with 'select' function in 'dplyr' library how to set select element as readonly ('disabled' doesnt pass select value on server) Trying to use INNER JOIN and GROUP BY SQL with SUM Function, Not Working

Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to rows

SQL count rows in a table Converting rows into columns and columns into rows using R Delete rows containing specific strings in R How to append rows to an R data frame Excel Create Collapsible Indented Row Hierarchies Excel CSV. file with more than 1,048,576 rows of data Find which rows have different values for a given column in Teradata SQL More than 1 row in <Input type="textarea" /> Update multiple rows with different values in a single SQL query Repeat rows of a data.frame