| #if you want a series of column names, e.g., col1,col2,col3, ..., #you can use: mydf<-data.frame(matrix(ncol=5,nrow=4)) colnames(mydf)<-c("col1","col2","col3","col4","col5") #Well, it does work, but how about for 50 columns? #A loop! Sounds great. Okay, mydf<-data.frame(matrix(ncol=50,nrow=4)) for (n in 1:ncol(mydf)) { colnames(mydf)[n]<-paste("col",n,sep="") } #But again, it's as urgly as the textbook. Here we offer a much better solution: colnames(mydf)<-paste(rep("col",ncol(mydf)),c(1:ncol(mydf)),sep="") #Smart, eh? At least the nicer looking :) #From http://scriptdemo.blogspot.com |
Showing posts with label R. Show all posts
Showing posts with label R. Show all posts
Friday, December 17, 2010
Generate a series of column names in R
Trim a string in R
| # sub-functions # Copyright: http://scriptdemo.blogspot.com mytrim<-function(oristr) { oristr<-sub("^ +","",oristr) oristr<-sub(" +$","",oristr) # or gsub("(^ +)|( +$)","",oristr) return(oristr) } |
Subscribe to:
Posts (Atom)