【问题标题】:Creating a function to calculate slope m=(y2-y1)/(x2-x1) in R创建一个函数来计算 R 中的斜率 m=(y2-y1)/(x2-x1)
【发布时间】:2019-11-02 16:16:26
【问题描述】:

我是任何类型的编程的新学生,并且无法在 R 中创建一个计算斜率的函数。这些错误意味着什么会阻止函数工作?

slope <- function(x1,y1,x2,y2) {

missingvars=c(missing(x1),missing(y1),missing(x2),missing(y2))

if(sum(missingvars)>0){
stop(paste(“Values were not provided (denoted by TRUE): x1 -”,missingvars[1],“, y1 -”,missingvars[2],“, x2 -”,missingvars[3],“, 
Y2 -”,missingvars[4],sep=“”))
}

if(x2-x1==0) {
stop(“Both X values are the same, slope cannot be calculated when X are the same.”)
}

m = (y2-y1)/(x2-x1)
return(m)

}
Error: unexpected '}' in "}"
> slope <- function(x1,y1,x2,y2) {
+ 
+ missingvars=c(missing(x1),missing(y1),missing(x2),missing(y2))
+ 
+ if(sum(missingvars)>0){
+ stop(paste(“Values were not provided (denoted by TRUE): x1 -“,missingvars[1],”, y1 -“,missingvars[2],”, x2 -“,missingvars[3],”, 
Error: unexpected input in:
"if(sum(missingvars)>0){
stop(paste(�"
> Y2 -“,missingvars[4],sep=“”))
Error: unexpected input in "Y2 -�"
> }
Error: unexpected '}' in "}"

【问题讨论】:

    标签: r function package


    【解决方案1】:

    我怀疑您的文本编辑器写错了引号。在代码中,您有引号“和”(看起来像 66 和 99)但 R 只能解释“(看起来像 ||)。如果将“和”替换为“,则代码不会抛出任何错误我。

    使用像记事本 (Windows)、Gedit (Linux) 或 TextEdit (MacOS) 这样的纯文本编辑器或像 RStudio 这样的 IDE 来编写代码应该可以防止将来发生这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2020-12-08
      相关资源
      最近更新 更多