2. Basic Programming Using R | Environmental Data Modeling - Asia B Technology

2. Basic Programming Using R | Environmental Data Modeling

programming in r

In this chapter, the author wants to invite the reader to be more familiar with the syntax or commands in R which will help the reader to do programming in R. Readers will investigate the use of operators in performing data processing operations in R, the type of data in R, to how we make the decision making process using R. Table of Contents programming r

Arithmetic Operator

Arithmetic Function

Relationship Operator

Logic Operator

Enter a Value Into a Variable

Data Type

Vector

Matrix

Factor

Data Frames

List

Loop

Loop Using Apply Family Function

Decision Making

Function 2.1 Arithmetic Operator

The calculation process will be handled by a special function. R will know the order validly. Unless we explicitly decide on something else. As a model run the following syntax:2+4*two

## [1] 10

Compare using the following syntax:(2+4)*2

## [1] 12

R can be used as a calculator

Based on the two outputs, it can be concluded that when we do not set the order of calculation using parentheses indications, R will automatically calculate in advance multiplication or blocking.

The arithmetic operators provided by R are as follows:

Table 1 Arithmetic Operator R + Addition, create addition operation – Substraction, create subtraction operation * Multiplication, create division / Division operation, create division operation ^ Expoentiation, create %% Modulus lifting operation,To find the remaining division of %/% Integer, To find integers of the distribution output only & without the rest of the division

To better understand it, here is the operator implementation syntax model earlier.# Addition five+3

## [1] 8

# Substraction 5-3

## [1] 2

# Multiplication five*3

## [1] 15

# Division five/3

## [1] 1.666667

# Exponent 5^3

## [1] 125

# Modulus five%%three

## [1] 2

# Integer 5%/%3

## [1] 1

Note:On R omen # serves to add a message to reveal a syntax in R. two.two Arithmetic function

In addition to the arithmetic operator function, in R there are also other arithmetic functions such as logarithmic, exponential, trigonometric, etc.Logarithms and exponentials

For logarithmic & exponential function models run the following syntax:log2(8) # logarithm base 2 create 8

## [1] 3

log10(8) # logarithm base 10 make 8

## [1] 0.90309

exp(8) # exponential 8

## [1] 2980.958

Trigonometric functions

trigonometric functions displayed e.g. sin, cos, tan, etc.cos(x) # cos x sin(x) # Sin x tan(x) # Tan x acos(x) # arc-cos x salted(x) # arc-sin x atan(x) #arc-tan x

Note:x in trigonometric functions has radian units

Here is one of the models of its use:cos(pi)

## [1] -1

Other mathematical functions

Other functions that can be used are absolute functions, square roots, etc. Here is a syntax model of the use of absolute functions & square roots.abs(-two) # absolute value -2

## [1] two

sqrt(4) # square root 4

## [1] twotwo.three Relationship Operators

Associate operators are used to compare one object using another object. The operator provided by R is presented in Table two.

Table 2 Relation Operator R “>” Greater than “<” Smaller based on “==” Same using “>=” Larger equal using “<=” Smaller equal using “!=” Not the same using “!=” Not the same using

Here are the operator deployments in the table:x <- 34 y <- 35 # Operator > x > y

## [1] FALSE

# Operator < x < y

## [1] TRUE

# operator == x == y

## [1] FALSE

# Operator > = x > = y

## [1] FALSE

# Operator <= x <= y

## [1] TRUE

# Operator != x != y

## [1] TRUEtwo.4 Logic Operator

Resource operators only apply to vectors with logical, numeric, or complex types. All numbers worth 1 will be believed to be true. The operator of the sense provided by R can be observed in Table three.

Table three Logic operator R & Operator reason AND ! Opeartor akal NOT & Operator reason AND element wise Operator reason OR element wise

Its application is found in the following syntax:v <- c(TRUE,TRUE, FALSE) t <- c(FALSE,FALSE,FALSE) # Operator && print(vdan print(vdanamp;t)

## [1] FALSE FALSE FALSE

Operator and will check the sense of each element in the vector in a way (in order from left to right).

Operator %% & from left to right on the first observation. For example, when using & if the first observation is TRUE then the first observation in another vector will be checked, but if the first observation is FALSE then the process will be stopped immediately and form FALSE. two.five Enter values into variables.

Variables in R can be used to store values. For example run the following syntax:# The price of a lemon is 500 rupiah lemon <- 500 # Or 500-> lemon # can also use the indication “=” lemon = 500

R allows the use of <-,->, or = to be a variable value filler command

R is case-sensitive. The meaning is the variable Lemon nir the same using lemon (Large small alphabetinfluence)

To find out the value of the lemon object we can use the print() function or type the name of the object personally.# Using the print() print(lemon) function

## [1] 500

# Or lemon

## [1] 500

R will store the lemon variable as an object on memory. So that we can perform operations on the object such as multiplying it or adding it up with other sapta. For example run the following syntax:# Multiplication operation against lemon object five*lemon

## [1] 2500

We can also renew the value of the lemon object by inputting a new value against the same object. R will automatically match the previous value. To better understand it run the following syntax:lemon <- 1000 # Print lemon print(lemon)

## [1] 1000

To better understand it, here is the syntax to calculate the volume of an object.# Dimensions of < length objects- 10 < widths- five < heights- five # Calculate volume volume <- length*width*height # Print print volume object(volume)

## [1] 250

To find out what objects we have made throughout this article we can use the function ls().ls()

## [1] “wide””lemon””long” “t””high””v””volume” ## [8] “x””y”

A collection of objects already stored on memory is claimed to be workspace

close
Asi B Technology

Silahkan Subscribe untuk Mendapatkan Update Terbaru Sekitar Technology

We don’t spam! Read our privacy policy for more info.