为了拥有一个矩阵,我想'd like to apply over each element of a vector, a function that outputs a vector. After applying the function to each element of that vector, I should have many vectors, which I' .
代码应该等同于以下内容:
my_function <- function(x) x:(x+10)
my_vec <- 1:10
x <- vector()
for(i in seq_along(vec)){
x <- rbind(x,my_function(my_vec[i]))
}
当然, my_function
和 my_vec
就是例子 .
2 回答
这是一个替代方案:
速度几乎相同:
.
尝试:
或者像Heroka建议的那样,使用
sapply
. 我更喜欢lapply
,然后以我喜欢的方式绑定我的输出(rbind
/cbind
)而不是潜在的转置 .