Allison Wiener
Allison Wiener

Reputation: 19

R code: why is my function producing no errors and no results?

I am writing a function and am having some problems. My function doesn't create the new variable, and after running it there are no errors and no change as if I never ran it. The code before the function works just fine and if I pass the arguments through the code directly (bypassing the function aspect of the code), it runs flawlessly.

Is there anything you think I'm missing? I have been looking at this for two days and have run out of ideas to try. Any suggestions would be greatly appreciated!

path <- "C:/Documents/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")

opQty002.0084.01 <- oppsQty[oppsQty$Part=="002-0084-01",]

 productNumberData <- function(nameNum,prodNum){
    # CREATING YEAR VARIABLE #
    year2009 <- ifelse((nameNum$Line.Created.date <= 
                        as.Date("12/30/2009","%m/%d/%Y")),"2009","0")
    year2010 <- ifelse((nameNum$Line.Created.date > 
                        as.Date("12/30/2009","%m/%d/%Y")&
                        nameNum$Line.Created.date <= 
                        as.Date("12/30/2010","%m/%d/%Y")),"2010",year2009)
    year2011 <- ifelse((nameNum$Line.Created.date > 
                        as.Date("12/30/2010","%m/%d/%Y")&
                        nameNum$Line.Created.date <= 
                        as.Date("12/30/2011","%m/%d/%Y")),"2011",year2010)
    nameNum$line.YEAR <- ifelse((nameNum$Line.Created.date > 
                                as.Date("12/30/2011","%m/%d/%Y")&
                                nameNum$Line.Created.date <= 
                                as.Date("12/30/2012","%m/%d/%Y")),"2012",year2011)
 }
 productNumberData(opQty002.0084.01,"002-0084-01")
 #opQty002.0084.01$line.YEAR does not exist

Upvotes: 0

Views: 1712

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60984

What you are seeing here is the result of what is known as scoping. In any decent programming language, R included, variables are not valid globally. Instead, variables exist in a certain scope. The variable you are creating is part of the scope of the function. Therefore it is not found outside the function, where you try to use it, which leads to the error. Scoping rules in R do allow variables outside a function to be referenced, where the variable outside the function is used only when there is no variable with that name in the scope of the function.

Scoping reduces the dependency between pieces of code in a larger R script. In this way code in a function is far less likely to cause unwanted side effects in other functions.

The solution I would use is to put all the objects you create in a larger data structure, probably a list. The code would look something like:

spam = function(object_in) {
  A = f(object_in)
  B = g(object_in)
  list(A, B)
 }
result = spam(obj)
result[["A"]]

Upvotes: 1

Related Questions