[R] error message from apply()

jim holtman jholtman at gmail.com
Wed Feb 6 03:38:26 CET 2008


The error message was coming from the call to colMeans where 'x' was
not a matrix; it was a vector that resulted from the 'apply' call.
Did you intend to use 'mean' instead like this example:

> data2_1 <- matrix(c(0.9584190, 0.2710325, -0.9495618, -0.1301772, -0.7539687,
+ 0.5344464, -0.8205933, 0.1581723, -0.5351588, 0.04448065, 0.9936430,
+ 0.2278786, -0.8160700, -0.3314779, -0.4047975, 0.1168152, -0.7458182, -
+ 0.2231588, -0.5051651, -0.74871174, 0.9450363, 0.4797723, -0.9033313, -
+ 0.5825065, 0.8523742, 0.7402795, -0.7134312, -0.8162558, 0.6345438, -
+ 0.05704138), 3,10)
>
> num <- apply(data2_1, 2, function(x) {sum(x > (mean(x, na.rm = TRUE) +
+ 1*sd(x, na.rm = TRUE)), na.rm = TRUE)})
> num
 [1] 0 1 1 1 0 0 1 1 0 0
>


On Feb 5, 2008 8:43 PM, Ng Stanley <stanleyngkl at gmail.com> wrote:
> Hi,
>
> I keep getting the error message. Please help.
>
> Error in colMeans(x, na.rm = TRUE) :   'x' must be an array of at least two
> dimensions
>
> The codes are:
>
> data2_1 <- matrix(c(0.9584190, 0.2710325, -0.9495618, -0.1301772, -0.7539687,
> 0.5344464, -0.8205933, 0.1581723, -0.5351588, 0.04448065, 0.9936430,
> 0.2278786, -0.8160700, -0.3314779, -0.4047975, 0.1168152, -0.7458182, -
> 0.2231588, -0.5051651, -0.74871174, 0.9450363, 0.4797723, -0.9033313, -
> 0.5825065, 0.8523742, 0.7402795, -0.7134312, -0.8162558, 0.6345438, -
> 0.05704138), 3,10)
>
> num <- apply(data2_1, 2, function(x) {sum(x > (colMeans(x, na.rm = TRUE) +
> 1*sd(x, na.rm = TRUE)), na.rm = TRUE)})
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list