Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在矩阵中存储数据_R_Loops_Matrix - Fatal编程技术网

R 如何在矩阵中存储数据

R 如何在矩阵中存储数据,r,loops,matrix,R,Loops,Matrix,我有一个简单的问题:我创建了一个名为se.bootstrap的函数,它接收来自两个向量的元素: bs <- c(10, 25, 200, 500, 5000) sample.size <- c(5, 10, 200, 2000) 有人能帮忙吗? 非常感谢假设se.bootstrap返回标量,您可以: se.bootstrap <- function(a,b) ( a*b) # use whatever function on 2 variables bs <- c

我有一个简单的问题:我创建了一个名为se.bootstrap的函数,它接收来自两个向量的元素:

bs <- c(10, 25, 200, 500, 5000) 
sample.size <- c(5, 10, 200, 2000)
有人能帮忙吗?
非常感谢

假设
se.bootstrap
返回标量,您可以:

se.bootstrap  <- function(a,b) ( a*b)  # use whatever function on 2 variables
bs <- c(10, 25, 200, 500, 5000)    # your vectors
sample.size <- c(5, 10, 200, 2000) # your vectors
res  <- matrix(se.bootstrap(expand.grid(bs,sample.size)$Var1,expand.grid(bs,sample.size)$Var2),nrow = length(bs),ncol = length(sample.size))
# function that returns a scalar
sd.fake <- function(bs, ss) bs*ss

# we prepare the matrix
res <- matrix(nr=length(sample.size), nc=length(bs), dimnames=list(sample.size, bs))

# we loop and fill the matrix
for (i in seq_along(sample.size))
  for (j in seq_along(bs))
    res[i, j] <- sd.fake(sample.size[i], bs[j])

> res
10    25   200     500    5000
5       50   125 1e+03    2500 2.5e+04
10     100   250 2e+03    5000 5.0e+04
200   2000  5000 4e+04  100000 1.0e+06
2000 20000 50000 4e+05 1000000 1.0e+07
返回标量的函数
sd.fake您希望得到什么?
sd.bootstrap
也很有用well@ZahiroMor选项是矢量化的,可能是更好的选项
res
      [,1]  [,2]  [,3]  [,4]
[1,]    50   100 2e+03 2e+04
[2,]   125   250 5e+03 5e+04
[3,]  1000  2000 4e+04 4e+05
[4,]  2500  5000 1e+05 1e+06
[5,] 25000 50000 1e+06 1e+07
# function that returns a scalar
sd.fake <- function(bs, ss) bs*ss

# we prepare the matrix
res <- matrix(nr=length(sample.size), nc=length(bs), dimnames=list(sample.size, bs))

# we loop and fill the matrix
for (i in seq_along(sample.size))
  for (j in seq_along(bs))
    res[i, j] <- sd.fake(sample.size[i], bs[j])

> res
10    25   200     500    5000
5       50   125 1e+03    2500 2.5e+04
10     100   250 2e+03    5000 5.0e+04
200   2000  5000 4e+04  100000 1.0e+06
2000 20000 50000 4e+05 1000000 1.0e+07