Re: [R] Combining series of variables using identifier

2008-02-26 Thread Henrique Dallazuanna
Try this: foo <- function(data, ...) { patt <- grep("var[0-9]$", names(data), value=T) res <- paste(sapply(data[, patt], as.character), sapply(data[, paste(patt, "lab", sep="_")], as.character), sep=": ") cbind(a, matrix(res, ncol=length(patt), dimnames=list(NULL, paste(patt, "new", sep="_" }

Re: [R] Combining series of variables using identifier

2008-02-26 Thread K. Elo
Hi, if the columns always follow the same order (indx,var#, var#_lab ...), then You could use the column numbers and do the following: 1) CN<-colnames(df)[#] (#=colnum of 'var#') 2) df$NEW<-(here the expression to create the new variable) 3) colnames(df)[ncol(df)]<-c(paste(CN,"new",sep="_")) Th