[R] Retaining variable name in a function

2010-03-17 Thread AC Del Re
Hi All, Im interested in creating a function that will convert a variable within a data.frame to a factor while retaining the original name (yes, I know that I can just: var -factor(var) but I need it as a function for other purposes). e.g.: # this was an attempt but fails. facts -

Re: [R] Retaining variable name in a function

2010-03-17 Thread Sarah Goslee
How about this? It changes a column to a factor, and optionally renames it. facts - function(meta, mod, newmodname) { meta[,mod] - factor(meta[,mod]) if(!missing(newmodname)) { colnames(meta)[colnames(meta) == mod] - newmodname } meta } testdata - data.frame(a=c(1,2,3),

Re: [R] Retaining variable name in a function

2010-03-17 Thread Henrique Dallazuanna
Try this: transform(DF, newA = factor(a), a = NULL) On Wed, Mar 17, 2010 at 4:06 PM, AC Del Re de...@wisc.edu wrote: Hi All, Im interested in creating a function that will convert a variable within a data.frame to a factor while retaining the original name (yes, I know that I can just:  var

Re: [R] Retaining variable name in a function

2010-03-17 Thread AC Del Re
Wow, wonderful! Thank you for all the response -- and so quick! ac On Wed, Mar 17, 2010 at 2:19 PM, Sarah Goslee sarah.gos...@gmail.com wrote: How about this? It changes a column to a factor, and optionally renames it. facts - function(meta, mod, newmodname) { meta[,mod] -