data = read.csv(file= "~/Downloads/data.csv")
temp=(data$temp)
n=75
N=length(temp)
s=sample(1:N, n)
ybar=mean(temp[s])
我想运行样本 100 次,其中 n 为 75。然后计算每个样本的平均值,并从设定的数字(50)中减去每个平均值。
Maybe a short loop is the way to go. Notice the bracket on the left-side of the equals sign in the last line of code - it's the key to using a loop for your calculation!
# set a seed - always a good idea when using randomness like 'sample()'
set.seed(123)
# pre-allocate an "empty" vector to fill in with results
ybar_vec = vector(length=n)
# do your calculation "n" times
for(i in 1:n) {
s = sample(N)
ybar_vec[i] = 50 - mean(temp[s]) # store i^th calc as i^th element of ybar_vec
}