Skip to content

Function to create variables with prefix

4 messages · Uwe Ligges, Moira Burke, jim holtman

#
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:
2 days later
#
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: