histelheim
histelheim

Reputation: 5088

Utilizing a character vector inside a function

I have the following function:

SubsetObject <- function(data)
{
  data <- read.table(data)
  data <- subset(data, action!="synchronize")
  # data <- subset(data, action!="assigned")
  # data <- subset(data, action!="subscribed")
  # data <- subset(data, action!="unsubscribed")
  # data <- subset(data, action!="merged")
  # data <- subset(data, action!="mentioned")
  data <- subset(data, action!="referenced")
  # data <- subset(data, action!="head_ref_cleaned")
  # data <- subset(data, action!="head_ref_deleted")
  # data <- subset(data, action!="head_ref_restored")
 (data)
}

Currently, it's using a very awkward manual approach to subsetting - I simply comment out the action character strings that I want to keep. Instead, I would like to be able to pass a character vector to the function, like so:

SubsetObject(data, exclude = c("synchronize", "referenced"))

And then do the subsetting so that those character strings are excluded form the action variable. How can I achieve this?

Upvotes: 1

Views: 38

Answers (1)

Sam Dickson
Sam Dickson

Reputation: 5239

You could probably just use %in%:

data <- subset(data,!action %in% c("synchronize", "referenced"))

Upvotes: 3

Related Questions