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

How to write code expressions

In this section, we will discuss how to write various basic expressions which are the core elements of writing a program. Later, we will discuss how to create user-defined functions.

Expressions

R code consists of one or more expressions. An expression is an instruction to perform a particular task.

For example, the addition of two numbers is given by the following expression:

>4+5 

It gives the following output:

[1] 9 

If there is more than one expression in a program, they get executed one by one, in the sequence they appear.

Now we will discuss basic types of expressions.

Constant expression

The simplest form of expression are constant values, which may be character or numeric values.

For example, 100 is a numeric value expression of a constant value.

Hello World is a character form expression of a constant expression.

Arithmetic expression

The R language has standard arithmetic operators and using these, arithmetic expressions can be written.

R has the following arithmetic operators:

Using these arithmetic operations, one can generate arithmetic expressions; for example:

4+5 
4-5 
4*5 

R follows the BODMAS rule. One can use parentheses to avoid ambiguity in creating any arithmetic expression.

Conditional expression

A conditional expression compares two values and returns a logical value in the form of True or False.

R has standard operators for comparing values and operators for combining conditions:

For example:

10>5, when executed, returns True.

5>10, when executed, returns False.

Functional call expression

The most common and useful type of R expression is calling functions. There are a lot of built-in functions in R, and users can built their own functions. In this section, we will see the basic structure of calling a function.

A function call consists of a function name followed by parentheses. Within the parentheses, arguments are present, separated by commas. Arguments are expressions that provide the necessary information to the functions to perform the required tasks. An example will be provided when we discuss how to construct user-defined functions.

Symbols and assignments

R code consists of keywords and symbols.

A symbol is the label for an object stored in RAM, and it gets the stored value from the memory when the program gets executed.

R also stores many predefined values for predefined symbols, which is used in the program as required and gets automatically downloaded.

For example, the date() function produces today's date when executed.

The result of an expression can be assigned to a symbol, and it is assigned by using the assignment operator <-.

For example, the expression value <-4+6 assigns the symbol value with value 10 and is stored in memory.

Keywords

Some symbols are used to represent special values and cannot be reassigned:

  • NA: This is used to define missing or unknown values
  • Inf: This is used to represent infinity. For example, 1/0 produces the result infinity
  • NaN: This is used to define the result of arithmetic expression which is undefined. For example, 0/0 produces NaN
  • NULL: This is used to represent empty result
  • TRUE and FALSE: These are logical values and are generally generated when values are compared

Naming variables

When writing R code, we need to store various pieces of information under many symbols. So we need to name these symbols meaningfully as that will make the code easy to understand. Symbols should be self-explanatory. Writing short symbol name will make the code tougher to understand.

For example, if we represent date of birth information by DateOfBirth or DOB, then the first option is better as it is self-explanatory.

主站蜘蛛池模板: 池州市| 班戈县| 鄂托克前旗| 汤原县| 青神县| 天等县| 泰顺县| 浦北县| 泾阳县| 东乡| 西林县| 青州市| 来安县| 张家口市| 和田市| 阿坝| 渝中区| 论坛| 岳普湖县| 旬阳县| 革吉县| 政和县| 岗巴县| 连江县| 个旧市| 大名县| 鹿邑县| 永昌县| 达日县| 大渡口区| 沐川县| 玛沁县| 麻阳| 舟曲县| 建昌县| 大兴区| 隆尧县| 荥经县| 九龙城区| 朝阳市| 宁晋县|