[R] Function to create variables with prefix

jim holtman jholtman at gmail.com
Fri Jul 18 04:33:04 CEST 2008


What you should be doing is to return a value from the function and
you can then assign this to an object of your choice:

sizeattributes <- c("length", "width", "height")
blue <- myfunction(bluefrenchwidgets, sizeattributes, "norm_")
red <- myfunction(redcanadianwidgets, sizeattributes, "norm_")

I don't think you really what to create objects in your global space.
You can with 'assign' but it is usually better to return a list that
has elements of your subsets.  This is much easier to manage since you
can alway use 'names' on the list to find out exactly what it
contains.

HTH

On Thu, Jul 17, 2008 at 5:46 PM, Moira Burke <moira at cmu.edu> wrote:
> Thanks to everyone who replied.  I really appreciate your help, and have
> been trying variations on the solutions you've offered.  Unfortunately, none
> do quite what I'm hoping to do.  Uwe's suggestion seems to be the closest (I
> think), but the new variables created within the function didn't exist
> outside of the scope of the function.  (If there's something basic in R that
> I'm just missing, please let me know!)
>
> Here's a rephrasing of what I'm trying to do:
>
> Let's say I have a data.frame called mywidgets.  It has these variables:
>
> mywidgets$length
> mywidgets$width
> mywidgets$height
> mywidgets$color
> mywidgets$country
>
> And I have some subsets of mywidgets, let's say:  bluefrenchwidgets  and
> redcanadianwidgets.  And I'm planning to make lots more subsets in the
> future.  So I want to create a function that performs some operation (such
> as scaling) within a subset easily.
>
> The function would be called sort of like this:
>
> sizeattributes <- c("length", "width", "height")
> myfunction(bluefrenchwidgets, sizeattributes, "norm_")
> myfunction(redcanadianwidgets, sizeattributes, "norm_")
>
> And then a bunch of scaled variables would be created within those subsets,
> e.g.:
>
> bluefrenchwidgets$norm_length
> redcanadianwidgets$norm_width
> etc.
>
> Can someone suggest either a function that will do this, or another way to
> approach the problem?
>
> Thanks,
> Moira
>
>
> On Tue, Jul 15, 2008 at 3:06 AM, Uwe Ligges <ligges at statistik.tu-dortmund.de>
> wrote:
>
>> Example:
>>
>> myfunction <- function(mydata, myvariables, prefix="norm_"){
>>        newnames <- paste(prefix, myvariables, sep="")
>>        mydata[newnames] <- scale(mydata[myvariables])
>>        mydata
>> }
>>
>> mydata <- data.frame(a=1:2, b=3:2)
>> myfunction(mydata, c("a", "b"))
>>
>>
>> Moira Burke wrote:
>>
>>> Hi.  I'm a new R user and think what I'm trying to do is pretty basic, but
>>> haven't been able to find the answer in the mailing list archives.  How do
>>> I
>>> write a function that creates new variables in a given data.frame with a
>>> prefix attached to the variable names?  I do a lot of repetitive logging
>>> and
>>> scaling of variables, and would like a way to consistently generate and
>>> name
>>> versions of the variables.
>>>
>>> The function would take a data.frame, an array of variables, and a prefix
>>> string.  It performs a transformation on the variables (e.g. logs and
>>> scales), and creates new variables with the same names as the inputs, but
>>> with the prefix_string prepended.
>>>
>>> So, a sample call would look like:
>>>
>>> myfunction(mydata, myvariables, "norm_")
>>>
>>> And if myvariables contained variables named "var1", "var2", etc., the
>>> function would generate:
>>>
>>> mydata$norm_var1
>>> mydata$norm_var2
>>>
>>> Thanks,
>>> Moira
>>>
>>>        [[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.
>>>
>>
>>
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list