Null-Hypothesis Null-Hypothesis - 3 months ago 21
R Question

R data frame remove column using grep - columns don't get remove

I have following loop:

columns.to.remove <- c('C_', 'WWTest_', 'TotalClick', 'IPA_', 'Sell', 'G_')
for (i in length(columns.to.remove)) {

final.frame <- final.frame[, -grep(columns.to.remove[i], colnames(final.frame))]
}


But when i look at the data frame by trying to do
final.frame$G_
its still shows up as all.

Answer
for (i in columns.to.remove) {
    final.frame <- final.frame[!grepl(i, colnames(final.frame))]
}

Edit: Doing this without a loop

columns.to.remove.combined <- paste(columns.to.remove, collapse = '|')
final.frame <- final.frame[!grepl(columns.to.remove.combined, colnames(final.frame))]