[R] Sampling problems

R. Michael Weylandt michael.weylandt at gmail.com
Thu Mar 8 14:51:00 CET 2012


Please use dput() to give a reproducible example: I can make this work
on a data frame quite easily --

x <- data.frame(1:10, letters[1:10], rnorm(10))
str(x)
print(x)
x[sample(nrow(x), 5), ]

So it's not a problem with something being a data frame or having factors.

Michael

On Thu, Mar 8, 2012 at 5:16 AM, Oritteropus <lucasantini85 at hotmail.com> wrote:
> Thanks, but it doesn't work either, it gives me the same message error.
> It works just if my first sample is taken in this way:
>
> mysample <- sample(1:nrow(MeanA), 20, replace=FALSE)
>
> However, in this way it sample just the number of rows:
>  [1] 71 24 12 36  2 39 69 62 43 38  9 44 13 54 50 63 67 66 37 28
>
> but not the data inside.  I need to sample in this way:
>
> mysample <- MeanA[sample(1:nrow(MeanA), 20, replace=FALSE),]
>
> to get a sample like this
>
> HRkm        Mean.mf         Mean.mfm         Loc         Diet         Terr
> Soc         Type         Soc.Ter         W.cat.0.25         W.cat.0.5
> -2.49                -0.43                2.57                       A
> O                T                       S                   D
> TS                          b
> 23                     -2.05                0.67                       T
> C                N                       S                    D
> NS                       A
>
> This is an example of my dataframe
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Sampling-problems-tp4453752p4456048.html
> 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.



More information about the R-help mailing list