[R] names not inherited in functions

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Jun 7 17:39:45 CEST 2007


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.



More information about the R-help mailing list