[R] rowVars

Brahm, David David.Brahm at geodecapital.com
Wed Apr 5 00:04:32 CEST 2006

I've written a rowVars function, but I don't think it throws the error
you mention.  You're welcome to use it if you like.

rowVars <- function(x, na.rm=FALSE, dims=1, unbiased=TRUE,
                    SumSquares=FALSE, twopass=FALSE) {
  if (SumSquares) return(rowSums(x^2, na.rm, dims))
  N <- rowSums(!is.na(x), FALSE, dims)
  Nm1 <- if (unbiased) N-1 else N
  if (twopass) {x <- if (dims==0) x - mean(x, na.rm=na.rm) else
                     sweep(x, 1:dims, rowMeans(x,na.rm,dims))}
  (rowSums(x^2, na.rm, dims) - rowSums(x, na.rm, dims)^2/N) / Nm1

-- David Brahm (brahm at alum.mit.edu) 

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of r user
Sent: Friday, March 31, 2006 1:40 PM
To: rhelp
Subject: [R] rowVars

I am using the R 2.2.1 in a Windows XP environment.

I have a dataframe with 12 columns and 1,000 rows.
(Some of the rows have 1 or fewer values.)

I am trying to use rowVars to calculate the variance
of each row.

I am getting the following message:
"Error in na.remove.default(x) : length of 'dimnames'
[1] not equal to array extent"

Is there a good work-around?

More information about the R-help mailing list