Home > Mobile >  Add values from multiple arguments to the end sum
Add values from multiple arguments to the end sum

Time:10-22

I want to create a function that has multiple arguments, such as sex(male or female), vacation(yes or no in the last 3 months), covid(yes or no in the last 3 months). What I want to do is that depending on what the user inputs, a time should be given for, let's say, when they are eligible to visit a grandparent. Let's just say that a male has to wait for 2 months, and a female has to wait 1 month (in this example, always a waiting time for the example's sake). And if they have had covid in the last 3 months, it doesn't effect anything, but if they haven't had covid, they need to wait 1 extra month. And if they have been on vacation they need to wait an extra month as well.

So,

male = 2, female = 1

vacation; yes = 1, vacation; no = 0

covid; yes = 0, covid; no = 1

So a male who has been home all the time an have had covid needs to wait

2 0 0 = 2 months.

And a female who has been on vacation and have not had covid needs to wait

1 1 1 = 3 months.

The function I'm trying to do is like this

visit_grandparent <- function(sex, vacation, covid){
   if sex = "male"
      #some return or add command
      if sex = "female"
         #some return or add command
      #and here it should continue?
   return()   #return a list? or a value?
}

And to test it one could do

visit_grandparent(sex="female", vacation="yes", covid=NULL)
[1] three months

I also stumbled across the today()-function, which maybe could be a better way to keep track and give a specific date when a person is allowed to visit. (Instead of giving "three months" as above or a number "3").

I'm thinking that I need some form of if-commands, but it seems like a very unnecessary and long way to write a function, as I then had to do it for all different types. But in some way it still feels right

CodePudding user response:

visit_grandparent <- function(sex, vacation, covid){
  s <- 0
  if(sex == "male"){
    s <- s   2
  }else if(sex == "female"){
    s <- s   1
  }
  if(vacation == TRUE){
    s <- s   1
  }
  if(covid == FALSE){
    s <- s   1
  } 
  return(s)
}

visit_grandparent(sex = "female", vacation = TRUE, covid = FALSE)
[1] 3

If you are able to convert all variables to binary, keep in mind that sum returns a count of TRUEs. This could make the function must simpler:

sex <- TRUE
vacation <- TRUE
covid <- FALSE

sum(c(sex,vacation,covid))
[1] 2

CodePudding user response:

Though there already is an accepted answer, here is a vectorized one.

visit_grandparent <- function(sex, vacation, covid){
  out <- (sex == "male")   1L
  out <- out   vacation
  out <- out   !covid
  out
}
# a male who has been home all the time an have had covid needs to wait
visit_grandparent(sex = "male", vacation = FALSE, covid = TRUE)
# [1] 2

# a female who has been on vacation and have not had covid needs to wait
visit_grandparent(sex = "female", vacation = TRUE, covid = FALSE)
# [1] 3
  •  Tags:  
  • r
  • Related