Basic functions in R language

I have written about R installation in my previous post R language, R studio – Installation. Let’s do something more in this post.

Basic commands

> 1+2
[1] 3
> log(4)
[1] 1.386294
> tan(45)
[1] 1.619775
> atan(5)
[1] 1.373401
> #addition
> 10+15
[1] 25
> #Subtraction
> 450-300
[1] 150
> #Multiplication
> 3 * 4;
[1] 12
> #Division
> 5/2
[1] 2.5
> #Expressions
> 1+(4/2)/3
[1] 1.666667
> #Exponentiation
> 3^2
[1] 9
> #Square root
> sqrt(25)
[1] 5
> #Constants
> pi
[1] 3.141593
> oth<-1:100
> oth
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
 [20]  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38
 [39]  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57
 [58]  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76
 [77]  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95
 [96]  96  97  98  99 100
> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t"
[21] "u" "v" "w" "x" "y" "z"
> letters[1:10]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> letters[26:1]
 [1] "z" "y" "x" "w" "v" "u" "t" "s" "r" "q" "p" "o" "n" "m" "l" "k" "j" "i" "h" "g"
[21] "f" "e" "d" "c" "b" "a"
> LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
[21] "U" "V" "W" "X" "Y" "Z"

() – Function bracket

[] – data set contains row and columns [row, column]

{} – user defined functions

> matrix(1:30)
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9
[10,]   10
[11,]   11
[12,]   12
[13,]   13
[14,]   14
[15,]   15
[16,]   16
[17,]   17
[18,]   18
[19,]   19
[20,]   20
[21,]   21
[22,]   22
[23,]   23
[24,]   24
[25,]   25
[26,]   26
[27,]   27
[28,]   28
[29,]   29
[30,]   30
> matrix(1:30, nrow=3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    4    7   10   13   16   19   22   25    28
[2,]    2    5    8   11   14   17   20   23   26    29
[3,]    3    6    9   12   15   18   21   24   27    30
> matrix(1:30, ncol=3)
      [,1] [,2] [,3]
 [1,]    1   11   21
 [2,]    2   12   22
 [3,]    3   13   23
 [4,]    4   14   24
 [5,]    5   15   25
 [6,]    6   16   26
 [7,]    7   17   27
 [8,]    8   18   28
 [9,]    9   19   29
[10,]   10   20   30

Store the results to environment

All the results shown above are not stored. So they are cleared from memory. So we’ll store the results to the R environment and retrieve it.

> result = 1+2+3+4+5
> result2 = 6+7+8+9+10
> final = result + result2
> avg = mean(result, result2)

The assignment may also use <- arrow.

> tamil<-80
> tamil
[1] 80

Everything is object in R. tamil is on object, so as avg, final, result, result2.

We shall use : operator to mention a series as given below

> 1:10
 [1]  1  2  3  4  5  6  7  8  9 10
> one_to_ten<-1:10
> one_to_ten
 [1]  1  2  3  4  5  6  7  8  9 10

Got it? If you are using R Studio, you would have seen that, the values are being stored.

R studio 2

We shall print it on the console like the following.

> result
[1] 15
> result2
[1] 40
> final
[1] 55
> avg
[1] 15

Object management

This returns all the user-defined variables in memory

> #view all objects in use
> ls()
[1] "avg"     "final"   "result"  "result2" "tamil"  
> objects()
[1] "avg"     "final"   "result"  "result2" "tamil"

Lets see how to remove those objects from memory.

> #remove one object
> rm(avg)
> remove(tamil)
> ls()
[1] "final"   "result"  "result2"
> #remove multiple objects
> rm(final, result)
> ls()
[1] "result2"
> rm(list=ls())
> ls()
character(0)

I’ll see you in another post with interesting subject.

 

Advertisements

One thought on “Basic functions in R language

  1. Pingback: Working with data types of R | JavaShine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s