Search code examples
rsamplerandomr-commander

R (R commander) - Sample


I'm struggling with sample function How can I sample 50 samples from dataset variable? In my dataset, there are 82 variables and I dunno how to sample from just one variable...;; I just wanna sample(randomly, without replacement) 50 samples from a variable at R commander(or R)

plz help!


Solution

  • You can use either the column index or the column name. Here's an example that samples ten values from the first column of the iris data set. Then the second line shows that sample being implemented three times.

    sample(iris$Sepal.Length, 10L)   ## or sample(iris[[1]], 10)
    # [1] 6.7 6.6 4.9 6.3 5.2 5.2 5.1 5.0 4.9 6.4
    replicate(3L, sample(iris$Sepal.Length, 10L))
    #       [,1] [,2] [,3]
    #  [1,]  5.1  5.6  5.6
    #  [2,]  6.9  5.0  5.1
    #  [3,]  4.4  6.9  6.5
    #  [4,]  7.2  5.8  5.5
    #  [5,]  5.7  7.7  6.7
    #  [6,]  4.8  5.0  5.0
    #  [7,]  5.0  6.2  5.8
    #  [8,]  5.9  5.6  6.0
    #  [9,]  4.9  4.9  6.1
    # [10,]  5.6  6.1  7.2