1

我正在为一个名为 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

非常感谢您的帮助!谢谢!

4

1 回答 1

2

目前您将 prodNum 作为数值传递,因此

280-0213-00被评估为67 (280-213-0= 67)

您应该将(并考虑)prodNum作为字符串传递(因为这是您想要的)

IE。"280-0213-00"

于 2012-06-20T05:45:37.110 回答