R:如果条件后跟几个命令

时间:2012-07-03 21:19:25

标签: r if-statement command

我有一个if条件,后跟几个命令。但是,似乎存在语法错误,但它无法正常工作。我想这很容易。我四处寻找正确的文档,但没有找到解决方案。谢谢你的帮助!

#data
supply_by_gas <- rep(0,365)
supply_by_gas[] <- 32069547/365
overall_gas_prod <- 51224298

#here the condition which should be true
if (overall_gas_prod > sum(supply_by_gas))
#followed by two commands
{peak_use_gas <- (overall_gas_prod - sum(supply_by_gas))
overall_gas_prod <- sum(supply_by_gas)-1 }
else
{peak_use_gas <- 0}

这是我收到的错误:

Unexpected 'else' in "else" 
> {peak_use_gas <- 0}

1 个答案:

答案 0 :(得分:3)

if (overall_gas_prod > sum(supply_by_gas)) {
    peak_use_gas <- (overall_gas_prod - sum(supply_by_gas))
overall_gas_prod <- sum(supply_by_gas)-1 
} else  {
peak_use_gas <- 0
}