数据帧缓冲区包含用于不同产品两列itemnumber和bufferlevels。
buffer.variant buffer.buf_size
1 mimic1 3
2 mimic2 0
3 mimic3 0
4 mimic4 0
5 mimic5 3Fulfillment是一个data.frame,其中first列作为date,variants from buff作为其他列。我想写一个循环: 1.>looks up the column name in the first row of buff 2.>Assigns the first row and ith column cell with the value in the第二column of buff,即buffer level
variants=names(fulfillment[,-1])
for(i in 1:ncol(fulfillment)){
fulfillment[1,i+1]=buff[which(buff[,1]==variants[i]),2] }代码失败,并显示以下错误:
Error in `[<-.data.frame`(`*tmp*`, 1, i + 1, value = integer(0)): replacement has length zero发布于 2016-09-28 11:07:48
所以我能够解决这个问题。问题出在R返回Null时。我在条件中添加了对空值的检查。@HubertL指出了另一个小问题,但我只是在开始打印迭代器i的值时才意识到这一点。在程序流进入代码的“Then”部分之前,它被初始化为1,并被弃用为0。代码如下:
variants=names(fulfillment[,-1])
for(i in 1:(ncol(fulfillment)-1)){
if(!is.null(match(variants[i],buff[,1]))){
print(paste0("step ",i))
print(paste("old value ",fulfillment[1,i+1]))
fulfillment[1,i+1]=buff[match(variants[i],buff[,1]),2]}
print(paste0("New value ",fulfillment[1,i+1]))
}https://stackoverflow.com/questions/39731944
复制相似问题