r112127 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112126‎ | r112127 | r112128 >
Date:18:21, 22 February 2012
Author:rfaulk
Status:deferred
Tags:
Comment:
- modify methods to handle string objects when appending data frames
- add method filter.list.by.regex to create a new list from elements matching regular expression
Modified paths:
  • /trunk/tools/wsor/message_templates/R/R_helper_functions.R (modified) (history)

Diff [purge]

Index: trunk/tools/wsor/message_templates/R/R_helper_functions.R
@@ -158,7 +158,7 @@
159159 # Assumes: the two data frames have the same column names
160160 #
161161
162 -append.data.frames <- function(df_1, df_2) {
 162+append.data.frames <- function(df_1, df_2, string_frames=c(0)) {
163163
164164 df_cols <- length(colnames(df_1))
165165 df_rows_1 <- length(df_1[[1]])
@@ -169,13 +169,22 @@
170170
171171 for (i in 1:df_cols)
172172 for (j in 1:df_rows_1)
173 - df_return[colnames(df_return)[i]][[1]][j] <- df_1[colnames(df_1)[i]][[1]][j]
174 -
 173+ {
 174+ if (i %in% string_frames)
 175+ df_return[colnames(df_return)[i]][[1]][j] <- toString(df_1[colnames(df_1)[i]][[1]][j])
 176+ else
 177+ df_return[colnames(df_return)[i]][[1]][j] <- df_1[colnames(df_1)[i]][[1]][j]
 178+ }
 179+
175180 for (i in 1:df_cols)
176181 for (j in 1:df_rows_2)
177182 {
178183 row_index <- j + df_rows_1
179 - df_return[colnames(df_return)[i]][[1]][row_index] <- df_2[colnames(df_1)[i]][[1]][j]
 184+
 185+ if (i %in% string_frames)
 186+ df_return[colnames(df_return)[i]][[1]][row_index] <- toString(df_2[colnames(df_1)[i]][[1]][j])
 187+ else
 188+ df_return[colnames(df_return)[i]][[1]][row_index] <- df_2[colnames(df_1)[i]][[1]][j]
180189 }
181190
182191 # create the new data list
@@ -194,13 +203,12 @@
195204 # Constructs a concatenated data.frame from files
196205 #
197206
198 -build.data.frames <- function(template_indices, fname_first_part, fname_last_part) {
 207+build.data.frames <- function(template_indices, fname_first_part, fname_last_part, string_frames=c(0)) {
199208
200209 # Initialize the data frame
201210
202211 filename <- paste(fname_first_part, template_indices[1], fname_last_part, sep="")
203212 metrics = read.table(filename, na.strings="\\N", sep="\t", comment.char="", quote="", header=T)
204 -
205213 output <- paste("Processing data from",filename,"....")
206214 print(output)
207215
@@ -216,10 +224,10 @@
217225 output <- paste("Processing data from",filename,"....")
218226 print(output)
219227
220 - temp_frame = read.table(filename, na.strings="\\N", sep="\t", comment.char="", quote="", header=T)
221 - metrics <- append.data.frames(metrics, temp_frame)
 228+ temp_frame = read.table(filename, na.strings="\\N", sep="\t", comment.char="", quote="", header=T)
 229+ metrics <- append.data.frames(metrics, temp_frame, string_frames=string_frames)
222230 }
223 -
 231+
224232 metrics
225233 }
226234
@@ -275,3 +283,19 @@
276284 value_list
277285 }
278286
 287+
 288+# FUNCTION :: filter.list.by.regex
 289+#
 290+# Take a list and filter out elements that don't match the pattern
 291+#
 292+
 293+filter.list.by.regex <- function(pattern, value_list) {
 294+ new_list <- c()
 295+ for (i in 1:length(value_list))
 296+ if (length(grep(pattern, value_list[i], perl = TRUE)) > 0)
 297+ new_list <- c(new_list, TRUE)
 298+ else
 299+ new_list <- c(new_list, FALSE)
 300+ new_list
 301+}
 302+