Saturday, November 21, 2020

Keywords in R Programming

 In programming, a keyword is a word which is reserved by a program because it has a special meaning. A keyword can be a command or a parameter. Like in C, C++, Java, there is also a set of keywords in R. A keyword can't be used as a variable name. Keywords are also called as "reserved names."

There are the following keywords as per ?reserved or help(reserved) command:

ifelserepeat
whilefunctionfor
nextbreakTRUE
FALSENULLInf
NaNNANA_integer_
NA_real_NA_complex_NA_character_
R Programming Keywords

1) if

The if statement consists of a Boolean expression which is followed by one or more statements. In R, if statement is the simplest conditional statement which is used to decide whether a block of the statement will be executed or not.

Example:

  1. a<-11  
  2. if(a<15)  
  3. print("I am lesser than 15")  

Output:

R Programming Keywords

2) else

The R else statement is associated with if statement. When the if statement's condition is false only then else block will be executed. Let see an example to make it clear:

Example:

  1. a<-22  
  2. if(a<20){  
  3.     cat("I am lesser than 20")  
  4. }else{   
  5.     cat("I am larger than 20")  
  6. }  

Output:

R Programming Keywords

3) repeat

The repeat keyword is used to iterate over a block of code multiple numbers of times. In R, repeat is a loop, and in this loop statement, there is no condition to exit from the loop. For exiting the loop, we will use the break statement.

Example:

  1. x <- 1  
  2. repeat {  
  3.     cat(x)  
  4.     x = x+1  
  5. if (x == 6){  
  6.         break  
  7.     }  
  8. }  

Output:

R Programming Keywords

4) while

A while keyword is used as a loop. The while loop is executed until the given condition is true. This is also used to make an infinite loop.

Example:

  1. a <- 20  
  2. while(a!=0){  
  3.       cat(a)  
  4.       a = a-2  
  5. }     

Output:

R Programming Keywords

5) function

A function is an object in R programming. The keyword function is used to create a user-define function in R. R has some pre-defined functions also, such as seq, mean, and sum.

Example:

  1. new.function<- function(n) {  
  2.    for(i in 1:n) {  
  3.       a <- i^2  
  4.       print(a)  
  5.    }  
  6. }  
  7. new.function(6)  

Output:

R Programming Keywords

6) for

The for is a keyword which is used for looping or iterating over a sequence (dictionary, string, list, set or tuple).

We can execute a set of a statement once for each item in the iterator (list, set, tuple, etc.) with the help of for loop.

Example:

  1. v <- LETTERS[1:4]  
  2. for ( i in v) {  
  3.    print(i)  
  4. }  

Output:

R Programming Keywords

7) next

The next keyword skips the current iteration of a loop without terminating it. When R parser found next, it skips further evaluation and starts the new iteration of the loop.

Example:

  1. v <- LETTERS[1:6]  
  2. for ( i in v) {  
  3. if (i == "D") {  
  4.         next  
  5. }  
  6.     print(i)  
  7. }  

Output:

R Programming Keywords

8) break

The break keyword is used to terminate the loop if the condition is true. The control of the program firstly passes to the outer statement then passes to the body of the break statement.

Example:

  1. n<-1  
  2. while(n<10){  
  3. if(n==3)  
  4.         break  
  5. n=n+1  
  6. cat(n,"\n")  
  7. }  
  8. cat("End of the program")  

Output:

R Programming Keywords

9) TRUE/FALSE

The TRUE and FALSE keywords are used to represent a Boolean true and Boolean false. If the given statement is true, then the interpreter returns true else the interpreter returns false.

R Programming Keywords

10) NULL

In R, NULL represents the null object. NULL is used to represent missing and undefined values. NULL is the logical representation of a statement which is neither TRUE nor FALSE.

Example:

  1. as.null(list(a = 1, b = "c"))  

Output:

R Programming Keywords

11) Inf and NaN

The is.finite and is.infinite function returns a vector of the same length indicating which elements are finite or infinite.

Inf and -Inf are positive and negative infinity. NaN stands for 'Not a Number.' NaN applies on numeric values and real and imaginary parts of complex values, but it will not apply to the values of integer vectors.

Usage

  1. is.finite(x)  
  2. is.infinite(x)  
  3. is.nan(x)  
  4.   
  5. Inf  
  6. NaN  

12) NA

NA is a logical constant of length 1 that contains a missing value indicator. It can be coerced to any other vector type except raw. There are other types of constant also, such as NA_Integer_, NA_real_, NA_complex_, and NA_character. These constants are of the other atomic vector type which supports missing values.

Usage

  1. NA  
  2. is.na(x)  
  3. anyNA(x, recursive = FALSE)  
  4.   
  5. ## S3 method for class 'data.frame'  
  6. is.na(x)  
  7.   
  8. is.na(x) <- value  

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...