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
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.
Guanrao Chen
2007-Jun-07 16:46 UTC
[R] aggregate by two columns, "sum" not working while "mean" is
Dear Fellow Rers, I have a table looks like this: ca, la, 12 ca, sd, 22 ca, la, 33 nm, al, 9 ma, lx, 18 ma, bs, 90 ma, lx, 22 I want to sum the 3rd column grouped by the first and the second column, so the result look like this table: ca, la, 45 < ca, sd, 22 nm, al, 9 ma, lx, 40 < ma, bs, 90 The two rows with < are sums. I tried aggregate(table,list(table$V1,table$V2),sum/mean), sum was not working while mean worked. Can anybody give a hint? Thanks. Guanrao