Ritter, Christian C GSMCIL-GSTMS/2
2005-Jun-30 08:16 UTC
[R] request/suggestion: modified names
For some time now I use a modified version of names (extract direction) of the following type: Names<- function (x,filter="^") { grep(filter,names(x),value=TRUE) } Request: Has anyone already written a version which goes the other way (that is, which allows assignment of the type Names(x,filter)<-...). Naive versions are obvious, but I think something would have to be done to check validity. So if anyone has already done it for me, I would be very happy if you could share it. Such a function would be quite useful when working with large data frames (as I typically do) and need to edit names in a convenient way. Suggestion: Wouldn't the filter argument make sense in general, that is in "names" and not only in a custom version "Names"? Have a nice day, Christian Ritter Functional Specialist Statistics Shell Coordination Centre S.A. Monnet Centre International Laboratory, Avenue Jean Monnet 1, B-1348 Louvain-La-Neuve, Belgium Tel: +32 10 477 349 Fax: +32 10 477 219 Email: christian.ritter@shell.com Internet: http://www.shell.com/chemicals [[alternative HTML version deleted]]
On 6/30/05, Ritter, Christian C GSMCIL-GSTMS/2 <christian.ritter at shell.com> wrote:> For some time now I use a modified version of names (extract direction) of the following type: > Names<- > function (x,filter="^") > { > grep(filter,names(x),value=TRUE) > } > > Request: > Has anyone already written a version which goes the other way (that is, which allows assignment of the type Names(x,filter)<-...). Naive versions are obvious, but I think something would have to be done to check validity. So if anyone has already done it for me, I would be very happy if you could share it. Such a function would be quite useful when working with large data frames (as I typically do) and need to edit names in a convenient way. > > Suggestion: > Wouldn't the filter argument make sense in general, that is in "names" and not only in a custom version "Names"? >This is not a direct answer to your question, as stated, but if what is desired is really a method to do ad hoc editing (as opposed to renaming columns on a programmatic basis) then one can use 'fix': irish <- head(iris) # test data fix(irish) At this point a spreadsheet pops up and one can edit the header directly in a GUI. (I am on Windows XP -- not sure if this works on other OSes). Exit the spreadsheet and its done. This can alternately be done in JGR, a front end to R. Press ctrl-B to access the object browser. Pick out your data frame and a spreadsheet will popup. Right click the column name and choose Rename.