[R] Retaining variable name in a function

Henrique Dallazuanna wwwhsd at gmail.com
Wed Mar 17 20:23:28 CET 2010


Try this:

transform(DF, newA = factor(a), a = NULL)

On Wed, Mar 17, 2010 at 4:06 PM, AC Del Re <delre at 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 <-factor(var) but I need it as a  function for other
> purposes). e.g.:
>
> # this was an attempt but fails.
>
> facts <- function(meta, mod, modname = "spec") {
>  meta$mod <- factor(meta$mod)
>  colnames(meta)['mod'] <- modname
>  return(meta)
> }
>
> # ideally, would like to just specify the data.frame (=meta) and
> # variable to convert to factor (=mod) (similar to function input below).
> But am
> # also interested in having the option to create a new variable and
> # name (similar to the function input above).
>
> facts <- function(meta, mod) {
>  meta$mod <- factor(meta$mod)
>  return(meta)
> }
>
> Thanks for any help!
>
> AC
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list