getting s-apply to skip columns with non-numeric data? I have a dataframe ?x? of w columns. Some columns are numeric, some are not. I wish to create a function to calculate the mean and standard deviation of each numeric column, and then ?bind? the column mean and standard deviation to the bottom of the dataframe. e.g. tempmean <- apply(data.frame(x), 2, mean, na.rm = T) xnew <- rbind(x,tempmean) I am running into one small problem what is the best way to have sapply ?skip? the non-numeric data and return NA?s?
Gabor Grothendieck
2006-Aug-17 23:19 UTC
[R] getting sapply to skip columns with non-numeric data?
Use the first few rows of iris as test data and try this where isnum is 1 for each numeric column and NA for others. irish <- head(iris) isnum <- ifelse(sapply(iris, class) == "numeric", 1, NA) iris.data <- data.matrix(iris) rbind(iris, colMeans(iris.data) * isnum, sd(iris.data) * isnum) On 8/17/06, r user <ruser2006 at yahoo.com> wrote:> getting s-apply to skip columns with non-numeric data? > I have a dataframe "x" of w columns. > > Some columns are numeric, some are not. > > I wish to create a function to calculate the mean and > standard deviation of each numeric column, and then > "bind" the column mean and standard deviation to the > bottom of the dataframe. > > e.g. > > tempmean <- apply(data.frame(x), 2, mean, na.rm = T) > xnew <- rbind(x,tempmean) > > I am running into one small problem?what is the best > way to have sapply "skip" the non-numeric data and > return NA's? > > ______________________________________________ > 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. >
Liaw, Andy
2006-Aug-17 23:30 UTC
[R] getting sapply to skip columns with non-numeric data?
There's something that either you have not thought of or neglected to tell us: If you have k variables in the data frame, you will need a data frame of k variables and one row to be able to rbind() to the bottom of the original one. What are you going to put in place for non-numeric variables? Perhaps this might help: R> dat <- data.frame(f=factor(1:3), x=3:5, y=6:4) R> rbind(dat, as.data.frame(lapply(dat, function(x) if (!is.numeric(x)) NA else mean(x)))) f x y 1 1 3 6 2 2 4 5 3 3 5 4 11 <NA> 4 5 Andy From: r user> > getting s-apply to skip columns with non-numeric data? > I have a dataframe "x" of w columns. > > Some columns are numeric, some are not. > > I wish to create a function to calculate the mean and > standard deviation of each numeric column, and then "bind" > the column mean and standard deviation to the bottom of the dataframe. > > e.g. > > tempmean <- apply(data.frame(x), 2, mean, na.rm = T) xnew <- > rbind(x,tempmean) > > I am running into one small problem...what is the best way to > have sapply "skip" the non-numeric data and return NA's? > > ______________________________________________ > 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. > >