简体   繁体   English

R中的ifelse'意外符号'错误

[英]ifelse 'unexpected symbol' error in R

Its just a basic ifelse but its returning the error: "unexpected symbol" and points to the first character of the last line. 它只是一个基本的ifelse,但返回错误:“意外符号”,并指向最后一行的第一个字符。 any ideas? 有任何想法吗?

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

You should use the standard if and else construct in R, as defined by the R language definition . 您应使用R语言定义中定义的标准ifelse在R中构造。

When the if statement is not in a block the else, if present, must appear on the same line as the end of statement2. 如果if语句不在块中,则else(如果存在)必须与statement2的末尾出现在同一行。 Otherwise the new line at the end of statement2 completes the if and yields a syntactically complete statement that is evaluated. 否则,statement2末尾的新行将完成if并产生一个语法完整的语句,该语句将被评估。 A simple solution is to use a compound statement wrapped in braces, putting the else on the same line as the closing brace that marks the end of the statement 一个简单的解决方案是使用用大括号括起来的复合语句,将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)
}

The command ifelse takes vectors and returns vectors. ifelse命令获取向量并返回向量。 See ?ifelse for examples. 有关示例,请参见?ifelse

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM