Re: [R] Function to create variables with prefix

From: Moira Burke <moira_at_cmu.edu>
Date: Thu, 17 Jul 2008 14:46:24 -0700

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. Received on Fri 18 Jul 2008 - 00:31:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Jul 2008 - 03:32:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive