[英]For loop not iterating through columns r
I'm trying to write a loop that will iterate through columns 64:111
in a data frame, and set [64,1] = 0
, then [65,1:2] = 0
, then [66,1:3] = 0
etc (Months_out starts at 0
and increments by 1
). 我正在尝试编写一个循环,该循环将遍历数据帧中的列
64:111
,并设置[64,1] = 0
,然后设置[65,1:2] = 0
,然后设置[66,1:3] = 0
等等(Months_out从0
开始并以1
递增)。 I can't tell why my loop is only running once, what am I doing wrong? 我不知道为什么我的循环只运行一次,我在做什么错呢?
for (i in 64:111) {
Prod1[cbind(1:Prod1$Months_Out+1,i)] <- 0
}
For one thing, the data.frame subset goes like df[row,column]
It seems like you might have that backwards. 一方面,data.frame子集就像
df[row,column]
似的。 Second, I'm not sure why you are using cbind()
within the brackets of a dataframe subset. 其次,我不确定为什么要在数据
cbind()
集的括号内使用cbind()
。
Here's how I might do it: 这是我可能的方法:
rows <- 1
for (i in 64:111) {
Prod1[1:rows, i] <- 0
rows = rows+1
}
Does that work for you? 那对你有用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.