Re: [R] names not inherited in functions
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
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] exclude1 in summary.formula from Hmisc
Here is a peace of the data and code : sex <-c(2,2,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,1,1,2,2) AGN <- c("C","C","C","C","C","A","A","C","B","B","C","C","C","C","C","C","B","B","C","C","C") X <- c(2,2,2,2,1,2,2,2,2,2,1,1,1,1,1,1,1,1,1,2,2) varqual <- data.frame(sex,AGN,X) desqual <- summary.formula(varqual$X ~., data = subset( varqual, select = -X), method = "reverse", overall = T, test = T, long = T, exclude1 = F) desqual doesn't show the results for sex ==1 as it is redundant. I also tried long =T wich didn't change anything here. Bonus question if I may : This function is a little bit complex for me and I couldn't figure out how to get either Yates' continuity correction or Fisher Test instead of Chisquare. Does it ask a lot of program coding ? Regards. David [[alternative HTML version deleted]] __ 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] exclude1 in summary.formula from Hmisc
Dear Users, Still having troubles with sharing my results, I'm trying to display a contingency table using summary.formula. Computing works well but I'd like to display information on redundant entries say, males AND females. I wonder if this code is correct : desqualjum <- summary.formula(varqual1$X ~., data = subset( varqual1, select = -X), method = "reverse", overall = T, test = T, exclude1 = FALSE) where varqual1 is the data frame containing the variable "sex". I'm using R 2.5.1 and Hmisc 3.4-2 on a Mac (OSX.4, intel) but I had the same trouble with former versions of R and the package. Thank you for your help. David [[alternative HTML version deleted]] __ 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
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.