[Rd] Bug in base function sample ( ) (PR#13727)

chajewski at fordham.edu chajewski at fordham.edu
Thu May 28 09:30:13 CEST 2009


Full_Name: Michael Chajewski
Version: 2.9.0
OS: Windows XP
Submission from: (NULL) (150.108.71.185)


I was programming a routine which kept reducing the array from which a random
sample was taken, resulting in a single number. I discovered that when R
attempts to sample from an object with only one number it does not
reproduce/report the number but instead chooses a random number between 1 and
that number. 

Example 1:

# I am assigning a single number
gg <- 7

# Creating an array to store sampled values
ggtrack <- 0

# I am sampling 10,000 observations from my single value
# object and storing them
for (i in 1:10000) {
	g0 <- sample(gg, (i/i))
	ggtrack <- c(ggtrack,g0)
}

# Deleting the initial value in the array
ggtrack <- ggtrack[-1]

# The array ought to be 10,000 samples long (and it is)
length(ggtrack)

# The array should contain 10,000 "7", but it does not
# See the histogram of sampled values
hist(ggtrack)

Example 2:

# Here is the same example, but now with
# two number. Note that now the function performs
# as expected and only samples between the two.

gg <- c(7,2)
ggtrack <- 0
for (i in 1:10000) {
	g0 <- sample(gg, (i/i))
	ggtrack <- c(ggtrack,g0)
}

ggtrack <- ggtrack[-1]
length(ggtrack)
hist(ggtrack)


Highest Regards,
Michael Chajewski



More information about the R-devel mailing list