[R] error in sample ()

jim holtman jholtman at gmail.com
Wed Oct 17 01:22:50 CEST 2007


If you follow the posting and provide a reproduciable example, it
would be easier to suggest a solution.  One thing you need to so is an
'str(disperser.mx)' and I will bet it shows it as a dataframe and not
a matrix.  Here is a sample of code that shows if it is a matrix it
works, otherwise it gives the results you saw:

> x <- matrix(runif(30*73), nrow=30)
> newmat <- matrix(sample(x), nrow=30)
> str(x)
 num [1:30, 1:73] 0.2023 0.9582 0.5327 0.0313 0.3813 ...
> str(newmat)
 num [1:30, 1:73] 0.2530 0.0842 0.4102 0.2834 0.3377 ...
> x.df <- as.data.frame(x)
> str(x.df)
'data.frame':   30 obs. of  73 variables:
 $ V1 : num  0.2023 0.9582 0.5327 0.0313 0.3813 ...
 $ V2 : num  0.9777 0.4319 0.1258 0.0315 0.3368 ...
 $ V3 : num  0.165 0.658 0.259 0.117 0.516 ...
.....a lot deleted...
> newmat.df <- matrix(sample(x.df), nrow=30)
Warning message:
In matrix(sample(x.df), nrow = 30) :
  data length [73] is not a sub-multiple or multiple of the number of rows [30]
>


On 10/16/07, Silvia Lomascolo <slomascolo at zoo.ufl.edu> wrote:
>
> I am trying to get a random matrix based on an original matrix called
> disperser.mx, with dimensions 30x73
>
> When I write the following code:
>
> >scramble = sample (disperser.mx)
> >newmat = matrix(scramble, nrow=30)
>
> I get the following warning message and a very weird matrix with 30 rows but
> only 3 columns shown below:
>
> Warning message:
> data length [73] is not a sub-multiple or multiple of the number of rows
> [30] in matrix in: matrix(scramble, nrow = 30)
>
>     [,1]       [,2]       [,3]
>  [1,] Integer,30 Integer,30 Integer,30
>  [2,] Integer,30 Integer,30 Integer,30
>  [3,] Integer,30 Integer,30 Integer,30
>  [4,] Integer,30 Integer,30 Integer,30
>  [5,] Integer,30 Integer,30 Integer,30
>  [6,] Integer,30 Integer,30 Integer,30
>  [7,] Integer,30 Integer,30 Integer,30
>  [8,] Integer,30 Integer,30 Integer,30
>  [9,] Integer,30 Integer,30 Integer,30
> ...
>
> Can anyone please help me figure out why I get such a weird result?
> --
> View this message in context: http://www.nabble.com/error-in-sample-%28%29-tf4637263.html#a13244007
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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