[R] Using a function to consolidate variables
David Young
dyoung at telefonica.net
Thu Mar 18 18:34:17 CET 2010
Dear List,
I'm getting the error: object of type 'closure' is not subsettable
And am not sure how to get around the problem. I've included two
short code sets below. One that shows what I want to do and works,
but without using the function much, and another that tries to use the
function but causes the error.
# THIS WORKS AND SHOWS WHAT I'D LIKE TO DO
a <- c(1,2,3)
b <- c(5,6,0)
c <- data.frame(a,b)
# TAKES THE MAX OF TWO VARS AND SAVES IT TO A NEW VARIABLE AND DELETES OLD VARIABLES
consolidate.fun <- function( data, var1, var2, saved.max ) {
max <- apply( data[,c(var1, var2)], 1, max)
# THIS WORKS BUT I HAVE TO CALL THE DATA FRAME BY NAME RATHER THEN USING THE FUNCTION DATA NAME
c[,"var.max"] <<- max
c$a <<- NULL
c$b <<- NULL
}
consolidate.fun( data=c , var1="a", var2="b", saved.max="var.max" )
c
# THIS SHOWS THE MORE COMPLETE USE OF THE FUNCTION'S INTENT, BUT
# CAUSES THE ERROR IN R.
a <- c(1,2,3)
b <- c(5,6,0)
c <- data.frame(a,b)
# TAKES THE MAX OF TWO VARS AND SAVES IT TO A NEW VARIABLE AND DELETES OLD VARIABLES
consolidate.fun <- function( data, var1, var2, saved.max ) {
max <- apply( data[,c(var1, var2)], 1, max)
# SOURCE OF ERROR. I'VE TRIED CALLING THE DATA FRAME IN DIFFERENT
# WAYS, BUT NONE SEEM TO WORK FOR ME.
data[, saved.max ] <<- max
data[, var1 ] <<- NULL
data[, var2 ] <<- NULL
}
consolidate.fun( data=c , var1="a", var2="b", saved.max="var.max" )
c
Any helpful comments would be appreciated.
mailto:dyoung at telefonica.net
More information about the R-help
mailing list