@@ -2271,9 +2271,7 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
2271
2271
i <- 1
2272
2272
2273
2273
calculate_rain_condition <- function (data ){
2274
- data <- data %> %
2275
- dplyr :: select(- rain_total_name ) %> %
2276
- unique()
2274
+ data <- data %> % dplyr :: select(- rain_total_name ) %> % unique()
2277
2275
2278
2276
for (i in 1 : nrow(data )) {
2279
2277
# Create a condition to filter the daily data based on the year, day, and plant day/length
@@ -2319,28 +2317,24 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
2319
2317
# we now split by our different rain_total_actual conditions.
2320
2318
# we do this here to avoid calculating it multiple times.
2321
2319
for (rain_i in rain_totals ){
2322
- filtered_data <- filtered_data_1 %> % dplyr :: mutate(rain_total = rain_i ) %> %
2323
- dplyr :: select(c(rain_total_name , plant_length_name , plant_day_name , dplyr :: everything()))
2320
+ filtered_data <- filtered_data_1 %> % dplyr :: mutate(rain_total = rain_i )
2324
2321
2325
2322
# take the rows < 0 and run a check. We want to check
2326
2323
# if (anyNA(rain_values) && sum_rain < data[[rain_total_name]][i]) { sum_rain <- NA
2327
2324
# we do this here because we want to avoid running rain_total in calculate_rain_condition for efficiency purposes.
2328
2325
filtered_data <- filtered_data %> %
2329
2326
dplyr :: mutate(rain_total_actual = ifelse(rain_total_actual < 0 , ifelse(- 1 * rain_total_actual < rain_total , NA , - 1 * rain_total_actual ), rain_total_actual ))
2330
2327
2331
- if (! missing(station )){
2332
- filtered_data <- filtered_data %> %
2333
- dplyr :: group_by(.data [[station ]], .data [[year ]])
2334
- } else {
2335
- filtered_data <- filtered_data %> %
2336
- dplyr :: group_by(.data [[year ]])
2337
- }
2328
+ if (! missing(station )) filtered_data <- filtered_data %> % dplyr :: group_by(.data [[station ]], .data [[year ]])
2329
+ else filtered_data <- filtered_data %> % dplyr :: group_by(.data [[year ]])
2338
2330
2331
+ # return(filtered_data)
2332
+
2339
2333
filtered_data <- filtered_data %> %
2340
2334
# first add a column (T/F) that states that it is in the rainfall period or not.
2341
- dplyr :: mutate(plant_day_cond = start_day < = plant_day ,
2342
- length_cond = plant_day + plant_length < = end_day ,
2343
- rain_cond = rain_i < = rain_total_actual ) %> %
2335
+ dplyr :: mutate(plant_day_cond = .data [[ start_day ]] < = plant_day ,
2336
+ length_cond = plant_day + plant_length < = .data [[ end_day ]] ,
2337
+ rain_cond = rain_i < = rain_total_actual ) %> %
2344
2338
dplyr :: ungroup()
2345
2339
2346
2340
if (start_check == " both" ){
@@ -2380,9 +2374,12 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
2380
2374
}
2381
2375
}
2382
2376
2377
+ if (! missing(station )) column_order <- c(station , plant_day_name , plant_length_name , rain_total_name )
2378
+ else column_order <- c(plant_day_name , plant_length_name , rain_total_name )
2379
+
2383
2380
if (return_crops_table ){
2384
2381
# here we get crop_def and import it as a new DF
2385
- crops_def_table <- dplyr :: bind_rows(crops_def_table )
2382
+ crops_def_table <- dplyr :: bind_rows(crops_def_table ) % > % dplyr :: select(c(all_of( column_order ), everything())) % > % dplyr :: arrange( dplyr :: across( dplyr :: all_of( column_order )))
2386
2383
crops_name <- " crop_def"
2387
2384
crops_name <- next_default_item(prefix = crops_name , existing_names = self $ get_data_names(), include_index = FALSE )
2388
2385
data_tables <- list (crops_def_table )
@@ -2395,9 +2392,8 @@ DataBook$set("public", "crops_definitions", function(data_name, year, station, r
2395
2392
self $ import_data(data_tables = data_tables )
2396
2393
}
2397
2394
if (definition_props ){
2398
- if (! missing(station )) prop_data_frame <- dplyr :: bind_rows(proportion_df ) %> % dplyr :: select(c(station , rain_total_name , plant_length_name , plant_day_name , everything()))
2399
- else prop_data_frame <- dplyr :: bind_rows(proportion_df ) %> % dplyr :: select(c(rain_total_name , plant_length_name , plant_day_name , everything()))
2400
-
2395
+ prop_data_frame <- dplyr :: bind_rows(proportion_df ) %> % dplyr :: select(c(all_of(column_order ), everything())) %> % dplyr :: arrange(dplyr :: across(dplyr :: all_of(column_order )))
2396
+
2401
2397
prop_name <- " crop_prop"
2402
2398
prop_name <- next_default_item(prefix = prop_name , existing_names = self $ get_data_names(), include_index = FALSE )
2403
2399
data_tables <- list (prop_data_frame )
0 commit comments