2

I want to create a function with optional parameters. I am working on a dataset with income, states, and a few flags. The function takes the data, state and flag and filters it. If state and flag is missing, it should return the entire data, and so on. So far I have this code which is working fine. However, I want to know if there is a simpler code than this.

myfun <- function(data, states, flag){
  if(missing(states)) {
    if(missing(flag)) {
      new_data <- data
    } else {
      new_data <- subset(data, data$Flag == flag)
    }
  } else if(missing(flag)) {
    new_data <- subset(data, data$State == states)
  } else {
    new_data <- subset(data, (data$State == states & data$Flag == flag))
  }

  temp_data <- toJSON(new_data)
  save(temp_data, file = "Mydata.JSON")
  return(new_data)      
}

I know we can have optional parameters by having a default parameter like flag = Y. But how do I pass both the parameters in default parameters i.e. flag = Y & N, or all the 50 states. I am new to R and any help would be greatly appreciated.

Update: Got a solution I was looking for thanks to Daniel Winkler

test2 <- function(data,states = unique(data$State),flag = c("Y","N"))
{my_data<-subset(data, (data$State %in% states & data$Flag %in% flag))}
  • 1
    It would be good to have a reproducible example but I think you could pass multiple flags with ```myfun(data = mydata, states = mystates, flag = c('Y', 'N')``` and then change all the ```data$Flag == flag``` to ```data$Flag %in% flag```. Not sure if that is what you want though ;) – Daniel Winkler Jun 24 '17 at 18:06
  • 1
    For all states you could save the states to a vector with something like ```all_states <- unique(data$State)``` and then pass that to the states as argument in the function. Again ```%in%``` instead of ```==```. – Daniel Winkler Jun 24 '17 at 18:07
  • I guess you got the solution which I wanted. I was not aware of the %in% part. I will try it and let you know if it is working. :) Thank you – user3121788 Jun 24 '17 at 18:13
  • If you're using `$` anyway, just use `[` subsetting instead of `subset`. – alistaire Jun 24 '17 at 18:37
  • [Hard to say without sample data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610), but I think this could be simplified to something like `myfun <- function(.data, states = state.abb, flag = 'Y') .data[.data$State %in% states & .data$Flag %in% flag, ]` – alistaire Jun 24 '17 at 18:43
  • test2 <- function(data,states = unique(data$State),flag = c("Y","N")) {my_data<-subset(data, (data$State %in% states & data$Flag %in% flag))} – user3121788 Jun 24 '17 at 20:03

1 Answers1

1

For passing multiple values to the function:

myfun<-function(data,states,flag){


if(missing(states))
  {
    if(missing(flag))
    {
      new_data<-data
    }
    else
    {
      new_data<-subset(data, data$Flag %in% flag)
    }
  }     
  else if(missing(flag))
  {
    new_data<-subset(data, data$State %in% states)
  }
  else
  {
    new_data<-subset(data, (data$State %in% states & data$Flag %in% flag))
  }

  temp_data <- toJSON(new_data)
  save(temp_data, file="Mydata.JSON")
  return(new_data)      
}

Then call with:

all_states <- unique(mydata$States)
myfun(data = mydata, states = all_states, flag = c('Y', 'N'))
Daniel Winkler
  • 487
  • 3
  • 11
  • Its working :) :). However I just have one concern. instead of getting the unique values before hand. Is there a way to get the unique values of the data I pass in the function. eg: myfun(data, states = unique(data$States), flag = c('Y', 'N'))... this is an example.. when I run this.. it gives me an error – user3121788 Jun 24 '17 at 18:45
  • My bad, it did work the first time. here is the final function test2 <- function(data,states = unique(data$State),flag = c("Y","N")) {my_data<-subset(data, (data$State %in% states & data$Flag %in% flag))} Thank you so much for the help. I really appreciate it. :) :) – user3121788 Jun 24 '17 at 20:00