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

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.

主站蜘蛛池模板: 鱼台县| 象州县| 汤阴县| 玉林市| 青海省| 宜黄县| 荆州市| 米林县| 固安县| 苏尼特右旗| 大渡口区| 大足县| 清水河县| 潢川县| 高唐县| 开原市| 鄯善县| 文昌市| 虞城县| 济南市| 毕节市| 涿鹿县| 阳城县| 米易县| 南部县| 梁河县| 启东市| 梁平县| 瑞丽市| 荥经县| 玉树县| 勐海县| 昭觉县| 印江| 文成县| 兴山县| 西乡县| 宜城市| 综艺| 枝江市| 鞍山市|