官术网_书友最值得收藏!

Writing if else statements in R

We often use if statements in MS Excel, but we can also write a small code to perform simple tasks in R.

How to do it…

The logic for if else statements is very simple and is as follows:

if(x>3){
  print("greater value")
}else {
  print("lesser value")
}

We can copy and paste the preceding statement in the R console or write a function that makes use of the if else logic.

How it works…

The logic behind if else statements is very simple. The following lines clearly state the logic:

if(condition){
#perform some action
}else {
  #perform some other action
}

The preceding code will check whether x is greater than or less than 3, and simply print it. In order to get the value, we type the following in the R command window:

x = 2
主站蜘蛛池模板: 铜川市| 临西县| 福建省| 徐水县| 迭部县| 兰州市| 彰化市| 庆安县| 镇赉县| 平陆县| 新巴尔虎左旗| 雅江县| 雅江县| 高密市| 颍上县| 宁安市| 礼泉县| 江源县| 翼城县| 沧州市| 三都| 武城县| 新营市| 安福县| 阿勒泰市| 化隆| 濮阳县| 夏河县| 桐庐县| 渭南市| 忻州市| 马鞍山市| 罗源县| 永胜县| 南皮县| 金湖县| 南雄市| 洛浦县| 集安市| 萝北县| 伊金霍洛旗|