Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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
使用for循环命名向量中的组件_R_Vector_Components_Naming - Fatal编程技术网

使用for循环命名向量中的组件

使用for循环命名向量中的组件,r,vector,components,naming,R,Vector,Components,Naming,我试图将向量添加到for循环中的列表中,并希望根据计数器的值命名组件 这就是我在不使用for循环的情况下向列表添加向量的方法 momentum$mom1 <- (prices[period.ends, ] / mlag(prices[period.ends,],1)-1) momentum$mom3 <- (prices[period.ends, ] / mlag(prices[period.ends,],3)-1) momentum$mom6 <- (prices[perio

我试图将向量添加到for循环中的列表中,并希望根据计数器的值命名组件

这就是我在不使用for循环的情况下向列表添加向量的方法

momentum$mom1 <- (prices[period.ends, ] / mlag(prices[period.ends,],1)-1)
momentum$mom3 <- (prices[period.ends, ] / mlag(prices[period.ends,],3)-1)
momentum$mom6 <- (prices[period.ends, ] / mlag(prices[period.ends,],6)-1)

momentum$mom1对列表的双方括号赋值将评估其参数:

> z=list()
> n="foo"
> z[[n]]=99
> z
$foo
[1] 99
因此,你不可复制的例子如下:

for (i in periods){
    momentum[[paste0("mom",i)]] <- (prices[period.ends,]/mlag(prices[period.ends,],i)-1)
for(i以周期为单位){
动量[[paste0(“mom”,i)]
momentum=list()

非常感谢,这正是我想要的。
for (i in periods){
    momentum[[paste0("mom",i)]] <- (prices[period.ends,]/mlag(prices[period.ends,],i)-1)
momentum = list()

periods <- c(1,3,6)

n <- length(periods)
for (i in 1:n){
  momentum[[i]] <- "stuff"
  names(momentum)[i] <- paste("mom",i,sep="")
}