Basics of R Programming - Part 2
Basics of R Programming - Part 2
Here, 65 (ASCII value of character ‘A’) is compared with 5 and that’s why the output is TRUE.
Variables: Variables are nothing but reserved memory locations to store values. This implies that
when you create a variable you reserve some space in memory. Such as: B= True, x = 15 are the
variables occupying the memory locations.
A basic concept in (statistical) programming is called a variable. A variable allows you to store a
value (e.g. 42) or an object (e.g. a function description) in R. You can then later use this
variable's name to easily access the value or the object that is stored within this variable. You can
assign a value 42 to a variable x with the command.
Code & Output
# Assign the value 42 to x
> x <- 42
> # Print out the value of the variable x
>x
[1] 42
Suppose you have a fruit basket with five apples. As a data analyst in training, you want to store
the number of apples in a variable with the name my_apples.
Code & Output
> # Assign the value 5 to the variable my_apples
> my_apples <- 5
> # Print out the value of the variable my_apples
> my_apples
[1] 5
Every tasty fruit basket needs oranges, so you decide to add six oranges. As a data analyst, your
reflex is to immediately create the variable my_oranges and assign the value 6 to it.
Code & Output
> # Assign the value 6 to the variable my_oranges
> my_oranges <- 6
> # Print out the value of the variable my_oranges
> my_oranges
[1] 6
Next, you want to calculate how many pieces of fruit you have in total. Since you have given
meaningful names to these values, you can now code this in a clear way:
Code & Output
> # Add these two variables together
> my_apples + my_oranges
[1] 11
Here you can create a variable my_fruit and store the result there.
Code & Output
> # Create the variable my_fruit
> my_fruit = my_apples + my_oranges
> my_fruit
[1] 11
There are several R-objects, for instance, vectors, matrices, arrays, data frames, lists, factors. In
R, the basic data types like logical, numeric, character etc. come under vectors. These basic data
types are referred to as ‘Atomic Vectors’. There are six basic data types, such as, logical,
numeric, integer, complex, character and raw. We shall gradually explore by following examples.
In the following example, we shall come across a special type of function, combining function
c() .
Codes & Outputs
> #vectors
> #Logical
> vtr1 = c(TRUE, FALSE)
> class(vtr1)
[1] "logical"
> #Numeric
> vtr2 = c(15, 85.6, 9999)
> class(vtr2)
[1] "numeric"
> #Integer
> vtr3 = c(35L, 58L, 146L)
> class(vtr3)
[1] "integer"
> #Complex
> vtr4 = c(4+3i, 8+7i)
> class(vtr4)
[1] "complex"
> #Character
> vtr5 = c("A")
> class(vtr5)
[1] "character"
> #Raw
> vtr6 = charToRaw("X")
> class(vtr6)
[1] "raw"
Now, if a vector has a collection of different data types, they will be converted into a common
data type like numerical by considering ‘TRUE’ as 1 in the following example.
Code & Output
> vtr = c(TRUE, 35L, 3.14)
> class(vtr)
[1]"numeric"
While dealing with combinations of characters, logical, and integers; all entries will be converted
into characters.
Code & Output
> v = c("hello", "FALSE", 65L)
> class(v)
[1] "character"
There is a function like typeof() in R to check the data types. Let’s discuss some following
examples.