[R] names not inherited in functions
Duncan Murdoch
murdoch at stats.uwo.ca
Fri Aug 17 20:18:15 CEST 2007
On 8/17/2007 1:17 PM, david dav wrote:
> Dear R list,
> After a huge delay, I come back to this question. Using names of
> variables inside a function is a problem I run into quite often.
> Maybe this little example should help to get my point:
> Suppose I want to make a function "llabel" to get the labels of the
> variables from a data frame.
> If no label is defined, "llabel" should return the name of the variable.
>
> library(Hmisc)
> v1 <- c(1,2)
> v2 <- c(1,2)
> v3 <- c(1,3)
> tablo <- data.frame(v1,v2,v3)
> rm(v1,v2,v3)
>
> label(tablo$v1) <- "var1"
> attach(tablo)
I don't think attach() does what most people think it does. I'd
recommend avoiding it. with() is better. But this isn't your problem...
>
> # This does the trick on one variable.
> if (label(v1) !="") label(v1) else names(data.frame(v1))
> if (label(v2) !="") label(v2) else names(data.frame(v2))
This isn't actually working. "data.frame(v1)" creates a new data frame
containing v1, so names(data.frame(v1)) is just a long-winded way to say
"v1". (This isn't true for every possible choice of v1, but it's true
when v1 is a numeric vector.)
>
> But if I call this statement in a "llabel" function,
>
> llabel <- function(var) {
> if (label(var) !="" )
> res <- label(var)
> else res <- names(data.frame(var))
> return (res) }
Use "else res <- deparse(substitute(var))" as the 4th line. This says
to return the expression that was passed to the function if the label()
function returned a blank.
Duncan Murdoch
>
> I just get "var"s instead of the names when no label is defined :
>
> llabel(v1) # works
> llabel(v2) # gives "var" instead of "v2"
>
> Thanks for your help.
>
> David
>
>
> 2007/6/7, Uwe Ligges <ligges at statistik.uni-dortmund.de>:
>> Not sure what you are going to get. Can you shorten your functions and
>> specify some example data? Then please tell us what your expected result is.
>>
>> Best,
>> Uwe Ligges
>>
>>
>>
>>
>> david dav wrote:
>> > Dear all,
>> >
>> > I 'd like to keep the names of variables when calling them in a function.
>> > An example might help to understand my problem :
>> >
>> > The following function puts in a new data frame counts and percent of
>> > a data.frame called as "tablo"
>> > the step " nom.chiffr[1] <- names(vari) " is useless as names from the
>> > original data.frame aren't kept in the function environement.
>> >
>> > Hoping I use appropriate R-vocabulary, I thank you for your help
>> >
>> > David
>> >
>> > descriptif <- function (tablo) {
>> > descriptifvar <- function (vari) {
>> > table(vari)
>> > length(vari[!is.na(vari)])
>> > chiffr <- cbind(table(vari),100*table(vari)/(length(vari[!is.na(vari)])))
>> > nom.chiffr <- rep(NA, dim(table(vari)))
>> > if (is.null(names(vari))) nom.chiffr[1] <- paste(i,"") else
>> > nom.chiffr[1] <- names(vari)
>> > chiffr <- data.frame ( names(table(vari)),chiffr)
>> > rownames(chiffr) <- NULL
>> > chiffr <- data.frame (nom.chiffr, chiffr)
>> > return(chiffr)
>> > }
>> >
>> > res <- rep(NA, 4)
>> > for (i in 1 : ncol(tablo))
>> > res <- rbind(res,descriptifvar(tablo[,i]))
>> > colnames(res) <- c("variable", "niveau", "effectif", "pourcentage")
>> > return(res[-1,])
>> > }
>> > # NB I used this function on a data.frame with only factors in
>> >
>> > ______________________________________________
>> > R-help at stat.math.ethz.ch 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.
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
More information about the R-help
mailing list