Unused argument error in nested ifelse statements

15,912

Solution 1

There are a few things wrong with the ifelse chain, but I'd like to first mention a way to write this kind of selectors in a more readable fashion.

days.of.week <- c("Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday")
x <- 21  # some value
day <- days.of.week[(x%%7) + 1]
day
[1] "Thursday"


Now... about the use of ifelse and the unused argument error...
First off, remember that ifelse() is a function, therefore when you write a statement like
... ifelse(rem == 0, day="Thursday, ..., R will interpret the day="..." part as if you were passing the named argument day to the function.
Furthermore, in general, you should avoid using = [most of the time], you probably mean to use <-.
Anyway, corrected the ifelse chain should look some' like

rem <- 21%%7
day <- ifelse(rem==0, "Thursday", 
         ifelse (rem==1, "Friday", 
           ifelse (rem==2, "Saturday", 
             ifelse (rem==3, "Sunday", 
               ifelse (rem==4, "Monday", 
                 ifelse(rem==5, "Tuesday", "Wednesday")
               )
             )
           )
         )
       )

Solution 2

There's a much better way to conditional recoding than using nested if else statements. Use dplyr::case_when. It'll change your life for real. Here's what the code would look like with case_when, so much cleaner:

    day <- case_when(
      rem==0 ~ "Thursday", 
      rem==1 ~ "Friday", 
      rem==2 ~ "Saturday", 
      rem==3 ~ "Sunday", 
      rem==4 ~ "Monday", 
      rem==5 ~ "Tuesday",
      rem==6 ~ "Wednesday"
    )

Solution 3

dayFinder <- function(x) weekdays(as.Date("1970/1/1") + x)
dayFinder(21)
# [1] "Thursday"
dayFinder(c(21, 101))
# [1] "Thursday" "Sunday"  
Share:
15,912

Related videos on Youtube

user1807967
Author by

user1807967

Updated on July 11, 2022

Comments

  • user1807967
    user1807967 almost 2 years

    I'm making a function that outputs the day of the week, given a number of days since 1/1/1970. The function worked fine when it was a chain of if then statements, but I want to use the function on vectors, so I've needed to build this silly looking chain of ifelse statements.

    Unfortunately, I keep getting this error:

    Error in ifelse(rem == 0, day = "Thursday", ifelse(rem == 1, day = "Friday",  : 
    unused argument(s) (day = "Thursday")
    Calls: dayFinder -> ifelse
    Execution halted
    

    I haven't been able to figure out how to get around it - it looks like it's simply ignoring the then part of the ifelse statement. I've tried feeding it a variety of sample data sets or data points and haven't been able to fix the error.

    Here is my code - thanks in advance.

    dayFinder <- function(x){
    #Assuming that '0' refers to January 1 1970
    #Store given number
    start <- x
    #Initialize variable
    day="Halloween"
    #Divide x by 7 and store remainder
    rem <- x%%7
    #Determine the day
    ifelse(rem==0, day="Thursday", 
        ifelse (rem==1, day="Friday", 
            ifelse (rem==2, day="Saturday", 
                ifelse (rem==3, day="Sunday", 
                    ifelse (rem==4, day="Monday", 
                        ifelse(rem==5, day="Tuesday", 
                            if (rem==6)
                                {
                                    day="Wednesday"
                                    }))))))
    return(day)
    }
    
    q = seq(7,50,1)
    z = dayFinder(q)
    z
    
    • Backlin
      Backlin over 11 years
      Seen this? Find the day of a week in R. Should be simple to convert your number-of-days-values to a true date values that can be fed into weekdays. Generally, if there is a base function that satisfies your need it is probably much more carefully written than what you or I would write.
    • Carl Witthoft
      Carl Witthoft over 11 years
      As they write over and over again at thedailywtf, if you want to perform some obvious task, it's a certainty someone else has already done so. Thus the relatively easy search for date/time functions in common R packages :-)