rm.col.by.name <- function(dat, colname.list){ l1 = length(colname.list) l2 = which(colnames(dat) %in% colname.list) if(length(l2) == 0) { warning('Did not find any matching columns. Check input column names, please.') } if(length(l2) > 0 & length(l2) < l1) { warning('Did not find all matching columns. Only those found are removed.') return(dat[,-c(l2)]) } if(length(l2) > 0 & length(l2) == l1) { return(dat[,-c(l2)]) } }
Friday, May 27, 2011
One-liner R: remove data frame columns by names
Sometime, I want to remove some columns from my data. What I did in that case, is found out what are the unwanted column ids and removed those. It's just a couple of lines in R. But why not write a function to do the job and also some error handling. So next time, I just copy and paste!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment