0

I am passing in a csv file with two columns, the first being the major and the second being the courses in that major. For some reason when I run this, current_major is not showing up in my enviorment and when I run it in the console, it tells me that the object "current" is not found. I am having a lot of trouble understanding what is going wrong here.

course_data <- read.csv("file location")
majorCompare <- function(current, new){
  current_major <- which(course_data$major == current)
}
majorCompare("Animal Science", "American Studies")

1 Answer 1

2

Your function is creating the object current_major within the scope of the function, and then it disappears into thin air when the function is finished. That's why you don't see it.

You could use the superassignment operator <<-

majorCompare <- function(current, new){
  current_major <<- which(course_data$major == current)
}

majorCompare("Animal Science", "American Studies")

But functions should ideally return something and not change your global environment. So do this instead.

majorCompare <- function(current, new){
   which(course_data$major == current)
}

current_major <- majorCompare("Animal Science", "American Studies")

You've also declared new as a function argument but it is never used within the function body.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.