[R] Assign factor and levels inside function
Tim Howard
tghoward at gw.dec.state.ny.us
Wed Apr 20 21:05:35 CEST 2005
R-help,
After cogitating for a while, I finally figured out how to define a
data.frame column as factor and assign the levels within a function...
BUT I still need to pass the data.frame and its name separately. I can't
seem to find any other way to pass the name of the data.frame, rather
than the data.frame itself. Any suggestions on how to go about it? Is
there something like value(object) or name(object) that I can't find?
#sample dataframe for this example
y <- data.frame(
one=c(1,1,3,3,5,7),
two=c(2,2,6,6,8,8))
> levels(y$one) # check out levels
NULL
# the function I've come up with
fncFact <- function(datfra, datfraNm){
datfra$one <- factor(datfra$one, levels=c(1,3,5,7,9))
assign(datfraNm, datfra, pos=1)
}
>fncFact(y, "y")
> levels(y$one)
[1] "1" "3" "5" "7" "9"
I suppose only for aesthetics and simplicity, I'd like to have only
pass the data.frame and get the same result.
Thanks in advance,
Tim Howard
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 0.1
year 2004
month 11
day 15
language R
More information about the R-help
mailing list