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

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
主站蜘蛛池模板: 嘉义市| 合水县| 汶上县| 开阳县| 滁州市| 雅安市| 郑州市| 龙南县| 威宁| 兰州市| 宁晋县| 满洲里市| 额敏县| 太谷县| 牙克石市| 临猗县| 神农架林区| 伊金霍洛旗| 鄂托克旗| 黄龙县| 长泰县| 凯里市| 淮阳县| 北川| 绵竹市| 怀化市| 吉林市| 古丈县| 滦平县| 乐山市| 盘锦市| 新兴县| 通渭县| 桓台县| 台东县| 安吉县| 盘山县| 达尔| 枝江市| 界首市| 新野县|