我在第一个“%in%”语句中遇到连续错误。我已经检查了另一个“长度零的参数”答案,并没有看到任何应用。
仅在data$var[1] %in% Group1Zips
中测试在R控制台中返回FALSE,因此我必须丢失超出返回NULL值的内容。
非常感谢任何帮助。
NewColumn= vector()
builder = function(data, TRXVAR, ZIPVAR){
Group1 <- 0
Group1Zips = vector()
Group2 <- 0
Group2Zips = vector()
Group3 <- 0
Group3Zips = vector()
Group4 <- 0
Group4Zips = vector()
Group5 <- 0
Group5Zips = vector()
for (i in 1:nrow(data)){
if (data$ZIPVAR[i] %in% Group1Zips){
Group1 = Group1 + TRXVAR[i]
append(NewColumn,"Group1")
break }
else if (data[i, ZIPVAR] %in% Group2Zips){
Group2 = Group1 + data[i, TRXVAR]
append(NewColumn, "Group2")
break
}
else if (data[i, ZIPVAR] %in% Group3Zips){
Group3 = Group3 + data[i, TRXVAR]
append(NewColumn, "Group3")
break
}
else if (data[i, ZIPVAR] %in% Group4Zips){
Group4 = Group4 + data[i, TRXVAR]
append(NewColumn, "Group4")
break
}
else if (data[i, ZIPVAR] %in% Group5Zips){
Group5 = Group5 + data[i, TRXVAR]
append(NewColumn, "Group5")
break
}
else if (Group1 < Group2){
Group1 = Group1 + data[i, TRXVAR]
append(Group1Zips, data[i, ZIPVAR])
append(NewColumn, "Group1")
break
}
else if (Group2 < Group3){
Group2 = Group2 + data[i, TRXVAR]
append(Group2Zips, data[i, ZIPVAR])
append(NewColumn, "Group2")
break
}
else if (Group3 < Group4){
Group3 = Group3 + data[i, TRXVAR]
append(Group3Zips, data[i, ZIPVAR])
append(NewColumn, "Group3")
break
}
else if (Group4 < Group5){
Group4 = Group4 + data[i, TRXVAR]
append(Group4Zips, data[i, ZIPVAR])
append(NewColumn, "Group4")
break
}
else if (Group5 < Group1){
Group5 = Group5 + data[i, TRXVAR]
append(Group5Zips, data[i, ZIPVAR])
append(NewColumn, "Group5")
break
}
else {
Group1 = Group1 + data[i, TRXVAR]
append(Group1Zips, data[i, ZIPVAR])
append(NewColumn, "Group1")
}
}
return(nrow(NewColumn))
}
编辑**
以下是数据样本:
PROVIDER.ID ZIP TRX_ALL SPEC_CODE
2432188006 10013 4331 NEP
0050676082 90012 3050 IM
4954985007 77479 3043 CD
0250771087 90031 3020 OPH
答案 0 :(得分:1)
您的代码在某个地方缺少“}”,我认为您没有提供足够的数据来重现这一点,但它看起来很像您的if语句应该是:
if(data[i, ZIPVAR] %in% Group1Zips){
您编写它的方式是在您提供的任何对象中查找名为“ZIPVAR”的列,而不是查找以ZIPVAR值命名的列。