Skip to content

Error in apply(x2, 1, diff) : dim(X) must have a positive length

6 messages · hithit168, David Winsemius, R. Michael Weylandt

#
Anyone knows hat might be the cause of this error? Thanks for any help!
+ #generates four different 'means' using 
+ #difference scores from x2, an n x 2 matrix
+ #for use w/ bootstrap comparisons 
+     diffs = apply(x2,1,diff)  
+     mn1=mean(diffs) 
+     mn2=mean(diffs,tr=.2) 
+     mn3=mean(diffs,tr=.3) 
+     mn4=median(diffs) 
+ mns=c(mn1,mn2,mn3,mn4)  
+ list(mnds=round(mns,3)) }
Error in apply(x2, 1, diff) : dim(X) must have a positive length

--
View this message in context: http://r.789695.n4.nabble.com/Error-in-apply-x2-1-diff-dim-X-must-have-a-positive-length-tp4382435p4382435.html
Sent from the R help mailing list archive at Nabble.com.
#
On Feb 12, 2012, at 7:05 PM, hithit168 wrote:

            
You have given apply a list.
#
Thank you for your help, David. I was trying to run bootrapping on the
dataset "shoes" from MASS package. But I still have some problem here.
A B
[1,] 13.2 14.0
[2,] 8.2 8.8
[3,] 10.9 11.2
[4,] 14.3 14.2
[5,] 10.7 11.8
[6,] 6.6 6.4
[7,] 9.5 9.8
[8,] 10.8 11.3
[9,] 8.8 9.3
[10,] 13.3 13.6
[1] "matrix"
[1] 0.8 0.6 0.3 -0.1 1.1 -0.2 0.3 0.5 0.5 0.3
+ diffs=apply(x2,1,diff) 
+ mn1=mean(diffs)
+ mn2=mean(diffs,tr=.1)
+ mn3=mean(diffs,tr=.2)
+ mn4=median(diffs)
+ mns=c(mn1,mn2,mn3,mn4) 
+ list(mnds=round(mns,3))}
$mnds
[1] 0.410 0.400 0.417 0.400
Error in apply(x2, 1, diff) : dim(X) must have a positive length

I am not sure what I am doing wrong. When I run apply(y,1,diff) I am able to
get output, and when I run dif.mns with just the original dataset, I am also
able to get output.



--
View this message in context: http://r.789695.n4.nabble.com/Error-in-apply-x2-1-diff-dim-X-must-have-a-positive-length-tp4382435p4384129.html
Sent from the R help mailing list archive at Nabble.com.
#
What package is the bootstrap() function from? There are many
functions by that name....

My hunch is that it takes a vector at a time and puts it through
dif.mns so that leads to the error in the apply() call but I can't
verify.

Michael
On Mon, Feb 13, 2012 at 10:34 AM, hithit168 <ccchristy at live.com> wrote:
#
Yes, I just checked out the bootstrap package: the problem is that it
passes vectors (not matrices) to your function and thats why you get
the error you do. Just use diff() instead of apply(,, diff) in your
function and you'll be fine.

Michael
On Mon, Feb 13, 2012 at 12:48 PM, hithit168 <ccchristy at live.com> wrote: