我正在为部件号销售数据上的一个名为opps的数据集编写一个函数,我正在尝试将数据分解为特定于部件号的较小数据集。我试图将数据集命名为参数“modNum”。这是我到目前为止所拥有的 -
# modNum (Modified Product Number) takes a product number that looks
# like "950-0004-00" and makes it "opQty950.0004.00"
productNumber <- function(prodNum,modNum){
path <- "C:/Users/Data/"
readFile <- paste(path,"/opps.csv",sep="")
oppsQty <- read.csv(file=readFile,sep=",")
oppsQty$Line.Created.date <- as.Date(as.character(oppsQty$Line.Created),
"%m/%d/%Y")
modNum <- oppsQty[oppsQty$Part.Number=="prodNum",]
}
productNumber(280-0213-00,opQty280.0213.00)
#Error: object 'opQty910.0002.01' not found
我相信我遇到问题的路线是
modNum <- oppsQty[oppsQty$Part.Number=="prodNum",]
并且这是因为为了使代码工作,prodNum周围必须有括号,但是当我将括号放在代码中时, prodNum不再被视为要填充的参数。当我将括号放在这样的参数中时, -
productNumber(280-0213-00,"opQty280.0213.00")
我还有问题 我怎么能绕过这个?
我尝试将oppsQty $ Part.Number变量重写为数字(如下所示),以便我可以一起消除括号,但我仍然有错误...
productNumber <- function(prodNum,nameNum){
path <- "C:/Users/Data"
readFile <- paste(path,"/opps.csv",sep="")
oppsQty <- read.csv(file=readFile,sep=",")
oppsQty$Line.Created.date <- as.Date(as.character(oppsQty$Line.Created),
"%m/%d/%Y")
#ifelse(oppsQty$Part.Number=="Discount",
# oppsQty$Part.Number=="000000000",
# oppsQty$Part.Number)
oppsQty$Part <- paste(substr(oppsQty$Part.Number,1,3),
substr(oppsQty$Part.Number,5,8),
substr(oppsQty$Part.Number,10,11),sep = "")
oppsQty$Part <- as.numeric(oppsQty$Part)
oppsQty$Part[is.na(oppsQty$Part)] <- 0
nameNum <- oppsQty[oppsQty$Part==prodNum,]
}
> productNumber(401110201,opQty401.1102.01)
Warning message:
In productNumber(401110201, opQty401.1102.01) : NAs introduced by coercion
非常感谢帮助! 谢谢!
答案 0 :(得分:2)
目前您将prodNum作为数值传递,因此
280-0213-00
评估为67
(280-213-0= 67)
你应该传递(并考虑)prodNum
作为字符串(因为这是你想要的)
即。 "280-0213-00"