У меня есть 2 кадра данных, первый имеет данные за многие месяцы, а второй имеет пороговые значения (минимальные и максимальные, разные для каждого месяца).Теперь я хочу заменить любые значения вне порогов на NA.
Структура фреймов данных следующая: у данных есть столбцы с именами «месяц», «а», «b» и «с».Пороги имеют значения «месяц», «a.min», «a.max», «b.min» и «b.max».
thresholds <- structure(list(month = 1:3, a.min = c(1L, 2L, 0L), a.max = c(9L, 8L, 3L), b.min = c(50L, 60L, 30L), b.max = c(70L, 75L, 90L)), .Names = c("month", "a.min", "a.max", "b.min", "b.max"), row.names = c(NA, -3L), class = "data.frame")
df <- structure(list(a = c(3.693, 0.534, 3.068, 2.633, 3.047, 3.072, 3.278, 3.533, 3.406, 2.893, 2.722, 0.513, 1.994, 1.743, 1.958, 2.03, 2.222, 2.207, 2.393, 2.731, 15.464, 4.065, 3.458, 3.142, 2.705, 17.285, 1.794, 2.139, 2.455, 2.83, 3.008, 3.358, 3.663, 2.936, 2.636, 2.42, 3.403, 2.83, 2.74, 3.119, 2.376, 3.285, 3.267, 2.966, 3.675, 2.803, 3.097, 3.381, 2.774, 3.335, 3.857, 2.854, 3.093, 2.368, 2.8, 2.643, 3.047, 2.559, 2.119, 1.712, 1.614, 1.474, 1.82, 2.147, 2.405, 2.543, 2.374, 2.962, 3.375, 3.002, 2.785, 2.643, 2.304, 2.052, 2.116, 2.203, 2.574, 2.537, 2.306, 1.316, 2.164, 1.855, 1.501, 1.331, 1.417, 1.158, 0.792, 0.183, 0.567, 1.406, 0.975, 1.48, 0.473, 0.689, 0.046, 0.498, 1.847, 2.079, 2.454, 3.372), b = c(72.26, 77.25, 72.3, 75.79, 72.98, 83.6, 79.16, 80.9, 80.2, 80.2, 73.33, 72, 63.7, 47.14, 30.86, 47.2, 56.69, 46.94, 56.74, 50.95, 65.32, 71.82, 67.36, 65.04, 60, 53.26, 39.08, 46.73, 57.16, 80.9, 63.45, 52.17, 56.59, 54.27, 54.87, 43.51, 59.04, 50.24, 40.62, 46.33, 43.49, 55.31, 55.21, 55.76, 60.77, 49.29, 45.27, 34.23, 51.32, 81.9, 82.6, 79.03, 69.54, 70.3, 77.78, 96.4, 95.9, 93.2, 101.9, 93.2, 93, 93.8, 79.67, 63.16, 59.23, 61.44, 48.7, 60.45, 69.92, 69.54, 67.86, 73.45, 95.6, 87.8, 78.91, 71.7, 84.1, 93.4, 89.5, 88.5, 88.2, 88.2, 98.7, 117.9, 141, 157.2, 155.8, 149.6, 95.2, 91.1, 113.4, 66.98, 39.31, 41.21, 255.8, 247.5, 248.2, 251, 255.1, 250.4),c = c(384.399, 388.0435, 391.158, 394.1089, 396.2393, 397.7653,405.9039, 413.3497, 413.8737, 412.4252,401.0619, 395.5369,393.344, 390.2218, 380.8314, 370.9777, 365.3473, 365.9187,362.2083, 368.0958, 369.2954, 369.1633, 367.9333, 364.1945,359.7283, 357.4523,357.9721, 356.7934, 355.4262, 358.4297,357.7325, 362.7329, 365.4261, 363.8837,362.5658, 363.5668,369.6555, 366.5757, 360.5511, 360.7731, 360.5672, 363.6154,367.0974, 363.4489, 373.0476, 379.0865, 382.3346, 386.7982,394.0651, 398.8354,398.6193, 401.3643, 401.9453, 405.3331,417.1013, 425.4676, 423.6085, 421.9701,410.8265, 404.4327,401.7433, 397.9707, 389.2195, 379.0507, 371.2411, 370.1493,365.7072, 367.7261, 370.8189, 368.1045, 365.2104, 366.9838,370.7158, 371.3767,370.1482, 367.5164, 365.9738, 367.5455,368.9097, 366.8438, 361.4221, 363.1824,364.9451, 362.9793,364.1421, 360.9064, 359.4199, 358.8081, 354.5116, 352.878,351.8854, 354.0268, 364.0585, 368.6769, 382.3471, 385.0213,385.3837, 390.994, 388.8896, 386.261), month = c(1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("a", "b", "c","month"), row.names = c(NA, -100L), class = "data.frame")
Я вручную создал некоторые ожидаемые выходные данные.По существу, для месяца 1 ограничения a.min и a.max применяются к столбцу a.Затем, в течение месяца 2, применяются следующие ограничения.Для столбца b применяются ограничения b.min и b.max:
outcome <- structure(list(month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), a = c(3.693, NA,3.68, 2.633, 3.47, 3.72, 3.278, 3.533, 3.46, 2.893, 2.722, NA,1.994, 1.743, 1.958, 2.3, 2.222, 2.27, 2.393, 2.731, NA, 4.65,3.458, 3.142, 2.75, NA,1.794, 2.139, 2.455, 2.83, 3.8, 3.358,3.663, 2.936, 2.636, 2.42, 3.43, 2.83, 2.74, 3.119, 2.376, 3.285,3.267, 2.966, 3.675, 2.83, 3.97, 3.381, 2.774, 3.335, 3.857,2.854, 3.93, 2.368, 2.8, 2.643, 3.47, 2.559, 2.119, NA, NA, NA,NA, 2.147, 2.45, 2.543, 2.374, 2.962, 3.375, 3.2, 2.785, 2.643,2.34, 2.52, 2.116, 2.23, 2.574, 2.537, 2.36, NA, 2.164, NA, NA,NA, NA, NA, NA, NA, NA, 1.46, 0.975, 1.48, 0.473, 0.689, 0.46,0.498, 1.847, 2.79, 2.454, NA), b = c(NA, NA, NA, NA, NA, NA,NA, NA, NA, NA, NA, NA, 63.7, NA, NA, NA, 56.69, NA, 56.74, 5.95,65.32, NA, 67.36, 65.4, 6, 53.26, NA, NA, 57.16, NA, 63.45, 52.17,56.59, 54.27, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6.77, NA,NA,NA, NA,NA, NA, NA, 69.54, 7.3, NA, NA, NA, NA, NA, NA, NA,NA, NA, 63.16, NA, 61.44, NA, 6.45, 69.92, 69.54, 67.86, 73.45, NA, NA, NA, 71.7, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 66.98, 39.31, 41.21, NA, NA, NA, NA, NA, NA), c = c(384.399, 388.435, 391.158, 394.189, 396.2393, 397.7653,45.939,413.3497, 413.8737, 412.4252, 41.619, 395.5369, 393.344,39.2218, 38.8314, 37.9777, 365.3473, 365.9187, 362.283, 368.958,369.2954, 369.1633, 367.9333, 364.1945, 359.7283,357.4523,357.9721,356.7934,355.4262,358.4297,357.7325,362.7329,365.4261, 363.8837, 362.5658, 363.5668, 369.6555, 366.5757,36.5511, 36.7731, 36.5672, 363.6154, 367.974, 363.4489, 373.476,379.865, 382.3346, 386.7982, 394.651, 398.8354, 398.6193,41.3643, 41.9453, 45.3331, 417.113, 425.4676, 423.685, 421.971,41.8265, 44.4327, 41.7433, 397.977, 389.2195, 379.57, 371.2411,37.1493, 365.772, 367.7261, 7.8189,368.145,365.214,366.9838,37.7158, 371.3767, 37.1482, 367.5164, 365.9738, 367.5455,368.997, 366.8438, 361.4221, 363.1824, 364.9451, 362.9793,364.1421, 36.964, 359.4199, 358.881, 354.5116, 352.878, 351.8854, 354.268, 364.585, 368.6769, 382.3471, 385.213, 385.3837, 39.994, 388.8896, 386.261)), .Names = c("month", "a", "b","c"), row.names = c(NA, -100L), class = "data.frame")
Теперь мой вопрос: как я могу сделать это в R ???И как я могу сделать это на фреймах данных с десятками столбцов?