Re: [R] names not inherited in functions

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Thu, 07 Jun 2007 17:39:45 +0200

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. Received on Thu 07 Jun 2007 - 15:55:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 17 Aug 2007 - 20:35:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.