简体   繁体   English

错误的 If, Else 语句

[英]Faulty If, Else Statement

I always get the the Error: unexpected 'else' in " else" in my If, Else Statement.在我的 If, Else 语句中,我总是在“else”中收到错误:意外的“else”。

Pls help I've been sitting with this Error since two days and need to finish my paper till tomorrow.请帮助我两天以来一直坐在这个错误上,需要完成我的论文直到明天。

Isflorida <- function(data) {
  data <- filter(data,is.na(State)== FALSE)
  data$Isflorida <- vector(mode = "logical", length = nrow(data)) 
  for (i in 1:nrow(data)) { 
    if (data[i,]$State == "Florida") {
      data[i,]$Isflorida <- TRUE }
    else {data[i,]$Isflorida <- FALSE}}data}  

这还不够吗?

data$IsFlorida <- ifelse(data$State=="Florida", TRUE, FALSE)

Do not add data right after } , if really want to, add a semi-colon.不要在}之后添加data ,如果真的想要,添加一个分号。

Isflorida <- function(data) {
  data <- filter(data,is.na(State)== FALSE)
  data$Isflorida <- vector(mode = "logical", length = nrow(data)) 
  for (i in 1:nrow(data)) { 
    if (data[i,]$State == "Florida") {
      data[i,]$Isflorida <- TRUE } 
    else {data[i,]$Isflorida <- FALSE}}; data}

I would think that most R users will use more lines, like so:我认为大多数 R 用户会使用更多行,如下所示:

Isflorida <- function(data) {
  data <- filter(data,is.na(State)== FALSE)
  data$Isflorida <- vector(mode = "logical", length = nrow(data)) 
  for (i in 1:nrow(data)) { 
    if (data[i,]$State == "Florida") {
      data[i,]$Isflorida <- TRUE 
    } else {data[i,]$Isflorida <- FALSE}
  }
  data
}  

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

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