嵌套ifelse语句中未使用的参数错误 [英] Unused argument error in nested ifelse statements

查看:126
本文介绍了嵌套ifelse语句中未使用的参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个输出星期几的函数,给出自1970年1月1日以来的一些天。如果它是一个的链,如果那么语句,该函数工作正常,但我想在向量上使用该函数,所以我需要构建这个看起来很傻的链 ifelse 语句。

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

我一直无法弄清楚如何绕过它 - 看起来它只是忽略然后部分 ifelse 声明。我已尝试为其提供各种样本数据集或数据点,但无法修复错误。

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


推荐答案

ifelse 链存在一些问题,但我想首先提一下写这个的方法更具可读性的选择器。

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"



现在......关于使用 ifelse 未使用的参数错误 ...

首先,请记住 ifelse ()是一个函数,因此当你写一个像
... ifelse(rem == 0,day =Thursday,... ,R将解释 day =...部分,就像传递 命名参数一样 到函数。

此外,一般来说,你应该避免使用 = [大部分时间],你可能意味着使用< -

无论如何,纠正ifelse链应该看起来像'喜欢


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")
               )
             )
           )
         )
       )

这篇关于嵌套ifelse语句中未使用的参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆