ifelse 在 R 中'unexpected symbol'错误



它只是一个基本的ifelse,但它返回错误:"意外符号"并指向最后一行的第一个字符。有什么想法吗?

input1<-readline("using C:/Users/HouseGroup/Desktop/betterformat.csv as the target csv file.
  Is this correct?  Y/N" )
ifelse (input1=="Y",
theData <- read.csv("C:/Users/HouseGroup/Desktop/betterformat.csv"),
rightDir<- readline("please input the proper file path") 
theData<-  read.csv(rightDir))

应使用标准if,并在 R 中else构造,如 R 语言定义所定义。

当 if 语句不在块中时,else 如果存在,则必须 与语句 2 的末尾出现在同一行上。否则新的 语句 2 末尾的行完成 if 并生成 计算的语法完整语句。一个简单的解决方案 是使用用大括号括起来的复合语句,将 else 放在 与标记语句结尾的右大括号相同的行

if (input1=="Y") {
  theData <- read.csv("C:/Users/HouseGroup/Desktop/betterformat.csv")
} else {
  rightDir <- readline("please input the proper file path") 
  theData <- read.csv(rightDir)
}

命令ifelse接受向量并返回向量。有关示例,请参阅?ifelse

x <- c(6:-4)
sqrt(x)  #- gives warning
sqrt(ifelse(x >= 0, x, NA))  # no warning

最新更新