2
votes

So I have my problem below and R code: (The nile data is one of R's included datasets)

seed random number generator Define an empty or 1000-element vector, "sample1," to write sample means to Write for-loop to drawn 1000 random samples, n = 25, and write mean of sample to prepared vector

data <- as.vector(Nile)

set.seed(123)

sample1 <- vector()

for(i in 1:1000){
  r <- vector()
  r[i] <- data[sample(1:100,size=25,replace=1)]
  sample1[i] <-  mean(r[i])
}

and I am getting a warning message in my output saying:

Warning in r[i] <- data[sample(1:100, size = 25, replace = 1)]: number of items to replace is not a multiple of replacement length

Could anyone help me out?

1
Because you are assigning a vector to an element of a vector r[i] <- data[.....]. You can get rid of the whole r thing and compute mean on the data[blah...]Gopala
As a followup to @Gopala: you can write it as sample1 <- sapply(1:1000, function(i) { mean(data[sample(1:100, size=25, replace=TRUE)]) })ekstroem

1 Answers

2
votes

As mentioned in the comments, the problem is that you are trying to add a vector to an element of a vector. The dimensions don't add up.

The fix in this case is quite simply to remove the step, as it's redundant. In general if you need to store multiple vectors you can do that in a matrix, data frame or list structure, depending on if the vectors are of known length, same length, same class etc.

data <- as.vector(Nile)

set.seed(123)

sample1 <- vector()

for(i in 1:1000){
  d <- data[sample(1:100, size=25, replace=TRUE)]
  sample1[i] <-  mean(d)
}

Instead of using a for loop, in this case you can use replicate, which is a relative of lapply and its ilk.

set.seed(123)

sample2 <- replicate(1000, mean(data[sample(1:100, size=25, replace=TRUE)]))

# as you can see, the results are identical
head(sample1); head(sample2)
#[1] 920.16 915.12 925.96 919.36 859.36 928.96
#[1] 920.16 915.12 925.96 919.36 859.36 928.96