delete rows in a list of dataframes based on range of values

31 Views Asked by At

I have various lists of dataframes

    $data1
      var1 site
    1    1    10
    2    2    14
    3    3    18
    4    4    29
    5    5    67
    6    6    77
    $data2
       var1 site
    1    1    4
    2    2    35
    3    3    55
    4    4    78
    5    5    99
    6    6    110

I would like to exclude all those rows that at column $site have a value lower then 10 and less then 10 of the maximum value, whatever the max value is

Output:

  $data1
      var1 site
    2    2    14
    3    3    18
    4    4    29

    $data2
       var1 site

    2    2    35
    3    3    55
    4    4    78
    5    5    99

As the maximum value is always different I would have to split in sublists and get the maximum

sorted <- lapply(my_list, function(x) { x[ x$site > 10 & x$site < 67, ] })
sorted <- lapply(my_list, function(x) { x[ x$site > 10 & x$site < 100, ] })

Is there a way to indicate in one line of code to subset all rows > 10 and less then 10 of the maximum value? whatever the max value is

1

There are 1 best solutions below

0
Gregor Thomas On BEST ANSWER

You can use the max() function to find the maximum of a vector, e.g.,

lapply(my_list, function(x) x[ x$site > 10 & x$site < (max(x$site) - 10), ])