Re: [R] names not inherited in functions

2007-08-18 Thread david dav
Thank you to all.
And thank you for the extra tips. I had a kind of feeling my
 "names(data.frame(var))"
would seem awkward!

David

__
R-help@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.


Re: [R] names not inherited in functions

2007-08-17 Thread Duncan Murdoch
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 <[EMAIL PROTECTED]>:
>> 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@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@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@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.


Re: [R] names not inherited in functions

2007-08-17 Thread Gabor Grothendieck
Within a function deparse(substitute(x)) will give the name of x, as a character
variable.  Search the archives for
  deparse substitute
to find many examples.

On 8/17/07, david dav <[EMAIL PROTECTED]> 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)
>
> # 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))
>
> 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) }
>
> 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 <[EMAIL PROTECTED]>:
> > 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@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@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@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.


Re: [R] names not inherited in functions

2007-08-17 Thread Charles C. Berry

David,

I think you are looking for

deparse(substitute(var))

See

?deparse

Notice in

?data.frame

this bit:

  ...: these arguments are of either the form 'value' or 'tag =
   value'.  Component names are created based on the tag (if
   present) or THE DEPARSED ARGUMENT ITSELF. [emphasis added]

Chuck


On Fri, 17 Aug 2007, 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)
>
> # 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))
>
> 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) }
>
> 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 <[EMAIL PROTECTED]>:
>> 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@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@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.
>

Charles C. Berry(858) 534-2098
 Dept of Family/Preventive Medicine
E mailto:[EMAIL PROTECTED]  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
R-help@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.


Re: [R] names not inherited in functions

2007-08-17 Thread david dav
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)

# 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))

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) }

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 <[EMAIL PROTECTED]>:
> 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@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@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.


Re: [R] names not inherited in functions

2007-06-07 Thread Uwe Ligges
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@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@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] names not inherited in functions

2007-06-06 Thread david dav
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@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.