使用for循环命名向量中的组件
我试图将向量添加到for循环中的列表中,并希望根据计数器的值命名组件 这就是我在不使用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
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="")
}