@@ -36,3 +36,221 @@ fred <- FALSE
3636plugh <- NULL
3737xyzzy <- Inf
3838thud <- NaN
39+
40+ #==== complex_data_manipulation
41+ #---- nested_data_pipeline
42+ # Complex data manipulation with nested function calls and pipeline operators
43+ library(dplyr)
44+ library(tidyr)
45+
46+ result <- raw_data %>%
47+ filter(
48+ !is.na(value) &
49+ year >= 2020 &
50+ category %in% c("A", "B", "C")
51+ ) %>%
52+ group_by(
53+ region,
54+ category,
55+ quarter = paste0("Q", ceiling(month(date) / 3))
56+ ) %>%
57+ summarise(
58+ mean_value = mean(value, na.rm = TRUE),
59+ median_value = median(value, na.rm = TRUE),
60+ count = n(),
61+ .groups = "drop"
62+ ) %>%
63+ pivot_wider(
64+ names_from = quarter,
65+ values_from = c(mean_value, median_value, count),
66+ names_sep = "_"
67+ ) %>%
68+ mutate(
69+ total_mean = rowMeans(
70+ select(., starts_with("mean_value")),
71+ na.rm = TRUE
72+ ),
73+ variance_ratio = case_when(
74+ total_mean > 100 ~ "high",
75+ total_mean > 50 ~ "medium",
76+ TRUE ~ "low"
77+ )
78+ ) %>%
79+ arrange(desc(total_mean))
80+
81+ #==== statistical_modeling
82+ #---- complex_regression_analysis
83+ # Advanced statistical modeling with complex nested expressions
84+ library(lme4)
85+ library(broom.mixed)
86+
87+ # Complex mixed-effects model with nested grouping
88+ model <- lmer(
89+ response ~
90+ poly(time, 3) * treatment +
91+ I(log(baseline_measure + 1)) +
92+ scale(covariate1) +
93+ I(covariate2^2) +
94+ interaction_term +
95+ (1 + time | subject_id) +
96+ (1 | site_id/clinic_id),
97+ data = analysis_data,
98+ weights = 1 / sqrt(variance_weights),
99+ control = lmerControl(
100+ optimizer = "bobyqa",
101+ optCtrl = list(maxfun = 100000),
102+ check.conv.singular = .makeCC(
103+ action = "warning",
104+ tol = 1e-4
105+ )
106+ )
107+ )
108+
109+ # Complex post-hoc analysis with nested comparisons
110+ contrasts_result <- emmeans(
111+ model,
112+ pairwise ~ treatment | time,
113+ at = list(
114+ time = c(0, 30, 60, 90),
115+ baseline_measure = mean(analysis_data$baseline_measure, na.rm = TRUE)
116+ ),
117+ adjust = "tukey"
118+ ) %>%
119+ map_dfr(
120+ ~ .x %>%
121+ as_tibble() %>%
122+ mutate(
123+ effect_size = estimate / sqrt(
124+ sum(residuals(model)^2) / df.residual(model)
125+ ),
126+ significant = ifelse(
127+ p.value < 0.05,
128+ ifelse(p.value < 0.01, "**", "*"),
129+ "ns"
130+ )
131+ ),
132+ .id = "comparison_type"
133+ )
134+
135+ #==== complex_control_flow
136+ #---- nested_error_handling_validation
137+ # Complex control flow with nested conditionals and error handling
138+ process_data_batch <- function(data_list, config = list()) {
139+ tryCatch({
140+ # Validate inputs with nested conditions
141+ if (is.null(data_list) || length(data_list) == 0) {
142+ stop("Input data_list cannot be NULL or empty")
143+ }
144+
145+ for (i in seq_along(data_list)) {
146+ current_data <- data_list[[i]]
147+
148+ # Nested validation with complex conditions
149+ if (!is.data.frame(current_data)) {
150+ warning(paste("Element", i, "is not a data frame, skipping"))
151+ next
152+ }
153+
154+ required_cols <- c("id", "timestamp", "value")
155+ missing_cols <- setdiff(required_cols, names(current_data))
156+
157+ if (length(missing_cols) > 0) {
158+ if (config$strict_mode %||% FALSE) {
159+ stop(paste(
160+ "Missing required columns in element", i, ":",
161+ paste(missing_cols, collapse = ", ")
162+ ))
163+ } else {
164+ warning(paste(
165+ "Missing columns in element", i, ", filling with defaults:",
166+ paste(missing_cols, collapse = ", ")
167+ ))
168+
169+ # Complex default value assignment
170+ for (col in missing_cols) {
171+ current_data[[col]] <- switch(col,
172+ "id" = paste0("auto_", seq_len(nrow(current_data))),
173+ "timestamp" = rep(Sys.time(), nrow(current_data)),
174+ "value" = rep(NA_real_, nrow(current_data)),
175+ rep(NA, nrow(current_data))
176+ )
177+ }
178+ }
179+ }
180+
181+ # Complex data transformation with nested operations
182+ processed <- current_data %>%
183+ filter(!is.na(value)) %>%
184+ mutate(
185+ normalized_value = case_when(
186+ is.finite(value) && value >= 0 ~
187+ (value - min(value, na.rm = TRUE)) /
188+ (max(value, na.rm = TRUE) - min(value, na.rm = TRUE)),
189+ is.finite(value) && value < 0 ~
190+ pmax(-1, pmin(0, value / abs(min(value, na.rm = TRUE)))),
191+ TRUE ~ NA_real_
192+ ),
193+ quality_flag = ifelse(
194+ abs(scale(value)[, 1]) > 3,
195+ "outlier",
196+ ifelse(
197+ is.na(value) || !is.finite(value),
198+ "invalid",
199+ "valid"
200+ )
201+ )
202+ ) %>%
203+ arrange(timestamp, id)
204+
205+ data_list[[i]] <- processed
206+ }
207+
208+ # Final aggregation with complex grouping
209+ final_result <- bind_rows(data_list, .id = "batch_id") %>%
210+ group_by(batch_id) %>%
211+ summarise(
212+ total_records = n(),
213+ valid_records = sum(quality_flag == "valid", na.rm = TRUE),
214+ mean_value = mean(normalized_value, na.rm = TRUE),
215+ outlier_rate = mean(quality_flag == "outlier", na.rm = TRUE),
216+ completeness = mean(!is.na(normalized_value)),
217+ .groups = "drop"
218+ ) %>%
219+ mutate(
220+ quality_score = (
221+ 0.4 * completeness +
222+ 0.3 * (1 - outlier_rate) +
223+ 0.3 * pmin(1, valid_records / total_records)
224+ ),
225+ grade = case_when(
226+ quality_score >= 0.9 ~ "A",
227+ quality_score >= 0.8 ~ "B",
228+ quality_score >= 0.7 ~ "C",
229+ quality_score >= 0.6 ~ "D",
230+ TRUE ~ "F"
231+ )
232+ )
233+
234+ return(final_result)
235+
236+ }, error = function(e) {
237+ if (config$debug_mode %||% FALSE) {
238+ cat("Debug info - Error occurred at:",
239+ format(Sys.time(), "%Y-%m-%d %H:%M:%S"), "\n")
240+ print(str(data_list))
241+ }
242+
243+ stop(paste("Error in process_data_batch:", e$message))
244+
245+ }, warning = function(w) {
246+ if (config$verbose %||% TRUE) {
247+ message("Warning in process_data_batch: ", w$message)
248+ }
249+ invokeRestart("muffleWarning")
250+
251+ }, finally = {
252+ if (config$cleanup %||% TRUE) {
253+ gc() # Force garbage collection
254+ }
255+ })
256+ }
0 commit comments