doggysaywhat - 5 months ago 25

R Question

I had a question regarding the colMeans function. Is there a version of this that will not return an error when it runs into a column of length one? For example

`temp<-cbind(c(2,2),c(3,4))`

colMeans(temp)

[1] 2.0 3.5

But for this one

`temp2<-c(2,2)`

colMeans(temp2)

Error in colMeans(temp2) :

'x' must be an array of at least two dimensions

But, if I apply the function mean to each column it properly comes up with the value of 2 and 2.

I wrote a function to do this

`testfun<-function(i,x){`

mean(x[,i])

}

sapply(1:ncol(x),testfun,x)

which gives the same results as colMeans.

I've heard that colMeans is supposed to be much faster than this method. So, is there a version of colMeans that will work when my column is of size 1.

Answer

As @Paul points out, `colMeans`

expects "an array of two or more dimensions" for its `x`

argument (from `?colMeans`

). But `temp2`

is not an array

```
is.array(temp2)
# [1] FALSE
```

`temp2`

can be made into an array:

```
(tempArray <- array(temp2, dim = c(1, 2)))
# [,1] [,2]
# [1,] 2 2
colMeans(tempArray)
# [1] 2 2
```

Perhaps `temp2`

came from subsetting an array, such as

```
array(temp2, dim = c(2, 2))[1, ]
```

But this is not an array. To keep it as an array, add `drop = FALSE`

inside the brackets:

```
array(temp2, dim = c(2, 2))[1, , drop = FALSE]
# [,1] [,2]
# [1,] 2 2
```

Then you can use colMeans on the subsetted array.