向量化样本函数:从样本总体中抽样并创建样本向量列表
vectorize sample function: sample from sample population and create list of sampled vectors
我正在尝试对样本进行向量化:多次从同一人群中抽取样本,每次样本大小不同。
我有一个向量 a = c(boo = 5, foo=7)
,并且 b =LETTERS[1:10]
。
我想创建一个列表,其中包含从 b 采样的值,其中采样大小由 a 的值决定。
我已经试过了mapply(sample, b, size = a)
但是没用...
mapply(function(x,y) sample(x, size=y), list(b), a)
[[1]]
[1] "D" "F" "H" "A" "C"
[[2]]
[1] "C" "D" "B" "I" "F" "G" "E"
mapply
获取 ...
参数中提供的每个对象的第一个元素,并将函数应用于它们。
我们必须使用list(b)
,这样它才能作为一个单独的元素使用。如果我们只是在那里输入 b
,该函数将尝试 b[1]
,依此类推。
我们可以这样想象每次迭代:
#mapply form
mapply(myfunc, x, y)
#first time
myfunc(x[1], y[1])
#second time
myfunc(x[2], y[2])
#and so on
我正在尝试对样本进行向量化:多次从同一人群中抽取样本,每次样本大小不同。
我有一个向量 a = c(boo = 5, foo=7)
,并且 b =LETTERS[1:10]
。
我想创建一个列表,其中包含从 b 采样的值,其中采样大小由 a 的值决定。
我已经试过了mapply(sample, b, size = a)
但是没用...
mapply(function(x,y) sample(x, size=y), list(b), a)
[[1]]
[1] "D" "F" "H" "A" "C"
[[2]]
[1] "C" "D" "B" "I" "F" "G" "E"
mapply
获取 ...
参数中提供的每个对象的第一个元素,并将函数应用于它们。
我们必须使用list(b)
,这样它才能作为一个单独的元素使用。如果我们只是在那里输入 b
,该函数将尝试 b[1]
,依此类推。
我们可以这样想象每次迭代:
#mapply form
mapply(myfunc, x, y)
#first time
myfunc(x[1], y[1])
#second time
myfunc(x[2], y[2])
#and so on