Skip to content
Prev 316710 / 398506 Next

use name (not values!) of a dataframe inside a funktion

Another R-ish way of modifying an object with a function is to use
'replacement functions' (there must be other names, I'm not sure
what the standard is) that let you use syntax like
     someProperty(myData, ...) <- newProperty
To do this define a function called `someProperty<-` whose last argument
is named 'value' and which returns a modified version of its first argument.
When R sees the above syntax it does the equivalent of
   myData <- `someProperty<-`(myData, ..., value=newProperty)

E.g.,
if (value) dataframe[] <- lapply(dataframe, log2)
       else dataframe[] <- lapply(dataframe, function(x)2^x)
       dataframe
    }
x         y
1 0.000000  0.000000
2 1.000000 -1.000000
3 1.584963 -1.584963
4 2.000000 -2.000000
5 2.321928 -2.321928
x         y
1 1 1.0000000
2 2 0.5000000
3 3 0.3333333
4 4 0.2500000
5 5 0.2000000

(Usually such a function is used to set a property in the data, such as a logScale
flag, that is used or queried later.)


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com