Re: [R] error message from apply()

From: jim holtman <jholtman_at_gmail.com>
Date: Wed, 6 Feb 2008 05:42:29 -0500

Is 'thr' supposed to be the mean and sd of all the values in data2_1? If so, then

thr <- mean(data2_1, na.rm=TRUE) + sd(data2_1,na.rm=TRUE)

I am not exactly sure of "what is the problem that you are trying to solve". You just have to make sure that the object you are creating by precomputing has the right structure to do what you want.

On Feb 6, 2008 12:56 AM, Stanley Ng <stanleyngkl_at_gmail.com> wrote:
> Now I understand why 3 by 3 data2_1 works and not the 3x10 data2_1.
>
> How can I precompute thr and pass it safely to function(x) for the column
> operation ?
>
>
> -----Original Message-----
> From: jim holtman [mailto:jholtman_at_gmail.com]
> Sent: Wednesday, February 06, 2008 11:33
> To: Ng Stanley
> Cc: r-help
> Subject: Re: [R] error message from apply()
>
> You matrix only has 3 rows, so when you do 'apply(data2_1,2,...)' you are
> extracting columns which only have a length of 3 while thr has a length of
> 10
>
> > str(data2_1)
> num [1:3, 1:10] 0.958 0.271 -0.950 -0.130 -0.754 ...
> > str(thr)
> num [1:10] 1.060 0.528 0.104 0.925 -0.256 ...
> >
> That is why you get the error message of a size mismatch.
>
> On Feb 5, 2008 10:21 PM, Ng Stanley <stanleyngkl_at_gmail.com> wrote:
> > Replacing colMeans by mean removed the warning messages. Thanks
> >
> > However, when I precompute thr, and pass it to function(x), the error
> > returns. Using the shorter data2_1, doesn't give any warnings. What is
> > happening ?
> >
> > 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) # data2_1 <-
> > matrix(c(0.9584190, 0.2710325, -0.9495618, -0.1301772, - 0.7539687,
> > 0.5344464, -0.8205933, 0.1581723, -0.5351588), 3,3)
> >
> > thr <- colMeans(data2_1, na.rm = TRUE) + sd(data2_1, na.rm = TRUE)
> >
> > num <- apply(data2_1, 2, function(x) {
> > sum(x > (thr), na.rm = TRUE)
> > })
> >
> >
> >
> > On 2/6/08, jim holtman <jholtman_at_gmail.com> wrote:
> > >
> > > 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?
> > >
> >
> > [[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?
>
> ______________________________________________
> 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?

______________________________________________
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.
Received on Wed 06 Feb 2008 - 10:44:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Feb 2008 - 11:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive