Home > Software design >  use dplyr to find a specific column matching the condition from another table
use dplyr to find a specific column matching the condition from another table

Time:06-16

I have a criteria table:

    criteria <-data.frame(
        dx = c(0.244897959,
             6.112244898),
      slim_frac = c(0L, 1L),
           klim = c(1L, 40L),
     frackhfrac = c(0L, 1L),
            por = c(0.04, 0.16),
   matrix_k_log = c(-11L, -2L),
   srv_perm_log = c(-6L, 1L),
  frac_perm_log = c(-3L, 4L),
        pb_init = c(0L, 10000L),
            bhp = c(200L, 2000L)

criteria table

I want to find all matching rows from my main table and extract the "srv_volumn" column, so dx needs to be within (0.244898, 6.112245) and slim_factor is (0,1) till bhp is within (200, 2000). Do I need to apply a map function to go through all rows of the criteria table?

Here's part of my main database:

         main <- tibble::tribble(
  ~matrix_k, ~matrix_k_log, ~well_dd,   ~srv_perm, ~srv_perm_log, ~frac_perm, ~frac_perm_log,        ~por, ~inter_frac_spac, ~fracs_per_mile_well, ~models_per_7500_well, ~mod_radius,       ~dx, ~model_area, ~jlim, ~jlim2,  ~iws,  ~pb_init, ~klim, ~klim2, ~thickness, ~target_frac_len, ~flim, ~flength1, ~flength1_frac, ~flength2, ~flim2, ~eflim, ~eflim_1, ~eflim2, ~eflim2_2, ~frac_length_ratio, ~slim_frac, ~srv_width_along_well, ~slim, ~slim1_1, ~slim2, ~slim2_1, ~frackhfrac, ~f_height, ~fhlim, ~efhlim, ~efhlim2,    ~fh_fl, ~well_cell, ~int_well_spac_frac,  ~srv_area, ~srv_volume, ~rock_comp, ~srv_vol_frac,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,   1.2970541,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   98L,    258.72,           0.98,    258.72,    98L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 25842.279,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,   1.1791401,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   99L,     237.6,           0.99,     237.6,    99L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 23732.705,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,   1.0808784,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   99L,     217.8,           0.99,     217.8,    99L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 21754.979,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,  0.99773388,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   99L, 201.04615,           0.99, 201.04615,    99L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 20081.519,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,  0.92646719,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   99L, 186.68572,           0.99, 186.68572,    99L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 18647.126,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,  0.86470271,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   99L,    174.24,           0.99,    174.24,    99L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 17403.983,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
    7.8e-08,     -7.108125,    1000L, 0.026474569,    -1.5771711, 0.57349005,    -0.24147411,  0.11467016,        411.56523,            12.829072,             18.223114, 0.023894001, 4.1894411,  0.81065879,   52L,   101L, 166.4, 4869.1044,   23L,    24L,       115L,        256.57068,   99L,    163.35,           0.99,    163.35,    99L,    52L,      53L,     52L,       53L,                 1L, 0.42605267,             87.674232,   22L,      23L,    79L,      78L,  0.86856627, 99.885121,    20L,     20L,      21L, 16316.235,        11L,                   1,  2.7630667,   276.30667,      4e-06,    0.37387814,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   6.0353762,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   18L,     475.2,           0.18,     475.2,    18L,    18L,      19L,     18L,       19L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 36712.924,         9L,          0.34615385, 0.49134259,   39.307407,      4e-06,    0.11369702,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   3.0176881,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   36L,     475.2,           0.36,     475.2,    36L,    36L,      37L,     36L,       37L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 36712.924,         9L,          0.69230769, 0.98268517,   78.614814,      4e-06,    0.22739405,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   2.0117921,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   54L,     475.2,           0.54,     475.2,    54L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 36712.924,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   1.5088441,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   72L,     475.2,           0.72,     475.2,    72L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 36712.924,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   1.2070752,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   90L,     475.2,            0.9,     475.2,    90L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 36712.924,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,    1.005896,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,     435.6,           0.99,     435.6,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 33653.513,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.86219661,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L, 373.37143,           0.99, 373.37143,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 28845.869,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.75442203,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,     326.7,           0.99,     326.7,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 25240.135,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.67059735,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,     290.4,           0.99,     290.4,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 22435.676,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.60353762,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,    261.36,           0.99,    261.36,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 20192.108,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.54867057,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,     237.6,           0.99,     237.6,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 18356.462,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.50294802,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,     217.8,           0.99,     217.8,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 16826.757,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   0.4642597,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L, 201.04615,           0.99, 201.04615,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L,  15532.39,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,   0.4310983,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L, 186.68572,           0.99, 186.68572,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 14422.935,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.40235841,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,    174.24,           0.99,    174.24,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 13461.405,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   1.27e-09,    -8.8955454,    1000L, 0.004173795,    -2.3794689, 0.56748402,    -0.24604636,  0.11778517,        191.50713,            27.570775,             39.163033,  0.01629904, 1.9439503,  0.37721101,   52L,   101L, 166.4, 4305.5723,   23L,    24L,       115L,        469.93566,   99L,    163.35,           0.99,    163.35,    99L,    52L,      53L,     52L,       53L,                 1L, 0.47037139,             45.039737,   24L,      25L,    77L,      76L,  0.67180727, 77.257836,    16L,     16L,      17L, 12620.068,         9L,                   1,  1.4194341,   113.55473,      4e-06,    0.32845807,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   17.928434,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   14L,     369.6,           0.14,     369.6,    14L,    14L,      15L,     14L,       15L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3962.0403,         2L,          0.26923077,  2.2709871,   34.064807,      4e-06,    0.02347828,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,    8.964217,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   27L,     356.4,           0.27,     356.4,    27L,    27L,      28L,     27L,       28L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3820.5389,         2L,          0.51923077,  4.3797608,   65.696412,      4e-06,   0.045279539,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   5.9761446,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   41L,     360.8,           0.41,     360.8,    41L,    41L,      42L,     41L,       42L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L,  3867.706,         2L,          0.78846154,  6.6507479,   99.761219,      4e-06,   0.068757819,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   4.4821085,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   54L,     356.4,           0.54,     356.4,    54L,    52L,      53L,     52L,       53L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3820.5389,         2L,                   1,  8.4350949,   126.52642,      4e-06,   0.087205038,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   3.5856868,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   67L,    353.76,           0.67,    353.76,    67L,    52L,      53L,     52L,       53L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3792.2386,         2L,                   1,  8.4350949,   126.52642,      4e-06,   0.087205038,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   2.9880723,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   81L,     356.4,           0.81,     356.4,    81L,    52L,      53L,     52L,       53L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3820.5389,         2L,                   1,  8.4350949,   126.52642,      4e-06,   0.087205038,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   2.5612049,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   94L, 354.51429,           0.94, 354.51429,    94L,    52L,      53L,     52L,       53L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3800.3244,         2L,                   1,  8.4350949,   126.52642,      4e-06,   0.087205038,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   2.2410542,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   99L,     326.7,           0.99,     326.7,    99L,    52L,      53L,     52L,       53L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3502.1606,         2L,                   1,  8.4350949,   126.52642,      4e-06,   0.087205038,
   7.29e-05,     -4.136994,    1000L, 0.003622004,    -2.4410511,   47.36649,      1.6754712, 0.060275867,          568.883,            9.2813461,              13.18373, 0.028091894, 5.7947245,   1.9920482,   52L,   101L, 166.4, 4650.3077,   33L,    34L,       165L,        353.11059,   99L,     290.4,           0.99,     290.4,    99L,    52L,      53L,     52L,       53L,                 1L, 0.94097399,             267.65205,   48L,      49L,    53L,      52L, 0.064968519, 10.719806,     3L,      3L,       4L, 3113.0317,         2L,                   1,  8.4350949,   126.52642,      4e-06,   0.087205038
  )

CodePudding user response:

This is one of the most trick questions one can ask in the tidyverse-um, so I would first provide a general solution with the mpg dataset:

library(tidyverse)

mpg_criteria <-
  tribble(~X, ~min, ~max,
          "displ",    2.0,    5.4,
          "year",  1999.0, 2008.0,
          "cyl",      4.0,    8.0,
          "cty",     11.0,   21.0,
          "hwy",     16.3,   30.0,
          )

mpg_criteria$X <- syms(mpg_criteria$X)
mpg_criteria_cond <- pmap(mpg_criteria, ~ partial(filter, ... =, between(!!..1, !!..2, !!..3)))
filter_fn <- compose(!!!mpg_criteria_cond)
mpg_filtered <- filter_fn(mpg)

so the solution for your question is something along

criteria$X <- syms(criteria$X)
criteria_cond <- pmap(criteria, ~ partial(filter, ... =, between(!!..1, !!..2, !!..3)))
filter_fn <- compose(!!!criteria_cond)
main_filtered <- filter_fn(main)

Edit

The question was edited meanwhile, so here is the "original" format of criteria:

criteria <- data.frame(
  stringsAsFactors = FALSE,
  X = c("dx","slim_frac","klim","frackhfrac","por","matrix_k_log","srv_perm_log","frac_perm_log","pb_init","bhp"),
  min = c(0.244897959, 0, 1, 0, 0.04, -11, -6, -3, 0, 200),
  max = c(6.112244898, 1, 40, 1, 0.16, -2, 1, 4, 10000, 2000)
)

CodePudding user response:

Here's an approach using dplyr and a reshaped-long version of the data.

When I ran the code below on the sample data, I didn't find any out of range. So I made an altered version of the data set where row 1 and 2 were intentionally assigned values out of range from the criteria.

library(tidyverse)
main_errors <- main %>%
  mutate(orig_row  = row_number(), .before = 1) %>%
  mutate(dx        = if_else(orig_row == 1, 0,   dx),
         slim_frac = if_else(orig_row == 2, 1.5, slim_frac))

Here, I reshape long using tidyr::pivot_longer, so every value in the original wide table gets its own row, with the column name in the "X" column. Then I join to the criteria on the X variable that they have in common. Then, I filter to just keep rows where a criteria was specified (otherwise it will be NA), and where the value is either too low or two high. The out_of_criteria table will show the specific rows we want to exclude, and the variables that were out of range.

out_of_criteria <- main_errors %>%
  pivot_longer(-orig_row, names_to = "X") %>%
  mutate(X = fct_inorder(X)) %>%   # Optional, to preserve original column order
  left_join(criteria) %>%
  filter(!is.na(min), value < min | value > max)

out_of_criteria
## A tibble: 2 × 5
#  orig_row X         value   min   max
#     <int> <chr>     <dbl> <dbl> <dbl>
#1        1 dx          0   0.245  6.11
#2        2 slim_frac   1.5 0      1   

Finally, we can "anti-join" to remove the rows with errors from the main table. Note that rows 1 and 2 are now excluded.

main_errors %>%
  anti_join(out_of_criteria, by = "orig_row")

# A tibble: 30 × 51
   orig_row     matrix_k matrix_k_log well_dd srv_perm srv_perm_log frac_perm frac_perm_log    por inter_frac_spac fracs_per_mile_…
      <int>        <dbl>        <dbl>   <int>    <dbl>        <dbl>     <dbl>         <dbl>  <dbl>           <dbl>            <dbl>
 1        3      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 2        4      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 3        5      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 4        6      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 5        7      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 6        8      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 7        9      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 8       10      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
 9       11      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
10       12      6.8e-10        -9.17    1000   0.0285        -1.54     0.408        -0.390 0.0516            321.             16.5
# … with 20 more rows, and 40 more variables: models_per_7500_well <dbl>, mod_radius <dbl>, dx <dbl>, model_area <dbl>,
#   jlim <int>, jlim2 <int>, iws <dbl>, pb_init <dbl>, klim <int>, klim2 <int>, thickness <int>, target_frac_len <dbl>,
#   flim <int>, flength1 <dbl>, flength1_frac <dbl>, flength2 <dbl>, flim2 <int>, eflim <int>, eflim_1 <int>, eflim2 <int>,
#   eflim2_2 <int>, frac_length_ratio <int>, slim_frac <dbl>, srv_width_along_well <dbl>, slim <int>, slim1_1 <int>, slim2 <int>,
#   slim2_1 <int>, frackhfrac <dbl>, f_height <dbl>, fhlim <int>, efhlim <int>, efhlim2 <int>, fh_fl <dbl>, well_cell <int>,
#   int_well_spac_frac <dbl>, srv_area <dbl>, srv_volume <dbl>, rock_comp <dbl>, srv_vol_frac <dbl>
  • Related