Skip to content

colMeans in a data.frame with numeric and character data

4 messages · Diogo André Alagador, Dimitris Rizopoulos, Douglas Bates +1 more

#
try this:

dat <- data.frame(x = rnorm(10), y = rexp(10), z = letters[1:10])
colMeans(data.matrix(dat[sapply(dat, is.numeric)]))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting Diogo Andr? Alagador <dalagador at armail.pt>:
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
On Thu, Mar 27, 2008 at 3:05 PM, Dimitris Rizopoulos
<Dimitris.Rizopoulos at med.kuleuven.be> wrote:
Alternatively
x          y          z
-0.5260131  1.0523121         NA
Warning message:
In mean.default(X[[3L]], ...) :
  argument is not numeric or logical: returning NA

If you don't like the warning message showing up you can wrap the
expression in suppressWarnings().
#
summaryBy in the doBy package can do that.  The builtin iris data
set has 4 numeric columns and one factor column:
Sepal.Length Sepal.Width Petal.Length Petal.Width
1     5.843333    3.057333        3.758    1.199333


On Thu, Mar 27, 2008 at 3:22 PM, Diogo Andr? Alagador
<dalagador at armail.pt> wrote: