4

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
seancarmody
  • 6,182
  • 2
  • 34
  • 31
user1807967
  • 137
  • 1
  • 1
  • 6
  • 3
    Seen this? [Find the day of a week in R](http://stackoverflow.com/questions/9216138/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. – Backlin Nov 09 '12 at 09:13
  • 1
    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 :-) – Carl Witthoft Nov 09 '12 at 12:39

3 Answers3

10

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")
               )
             )
           )
         )
       )
mjv
  • 73,152
  • 14
  • 113
  • 156
9

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"
    )
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
phathamster
  • 101
  • 1
  • 2
3
dayFinder <- function(x) weekdays(as.Date("1970/1/1") + x)
dayFinder(21)
# [1] "Thursday"
dayFinder(c(21, 101))
# [1] "Thursday" "Sunday"  
Echilon
  • 10,064
  • 33
  • 131
  • 217
seancarmody
  • 6,182
  • 2
  • 34
  • 31