Finally i can write in my blog again after a several months stop writing for reasons that cannot be mentioned. In this section i would like to share about data types in R. We are going to cover five main data types in R which also called
atomic data types in R. So, let’s get started!
I am going to start by giving variable name, it’s going to be
x then create an arrow
<-and in R this is an
assignment operator and i want to assign
x the value of
2 in order to make sure this this is going to be an integer i have to put a capital
L, and it will be this
> x <-2L
It have been executed as new variable in global environtment. Now we are going to check what actual type does
x have, in order to check the type only we have to do is to type
typeof() and in brackets we put the
x variable what we want to check, and let’s execute that.
> x <-2L > typeof(x)  "integer"
Now whe can se that it is an integer!
Now we are going to create a duble variable, let’s call it
y and we will put assignment operator and give it a value
2.5 , and let’s execute to set it a avariable and it will be like this
> y <- 2.5
Now let’s have a look at type of
y and execute it.
> y <- 2.5 > typeof(y)  "double"
And what console returns was
double. So now we have two variables, one is an integer and one is a double!
Okay, now let discuss about
L symbol in
x varibale, why we couldn’t say just
x <- 2 ? Well. the reason is because by default R will decide for us hot it store the data, all operation in R, all arithmatic operator in R always conducted on a double level because make sense right if we going to add like a double variable something has a deciman point and something doesn’t have then result of the operation may actually have decimal point. So, it’s logical that any operation conducted with number have decimal point, that’s why it’s better to store anything as a double, if we dont put
x variable and rerun it, we will se that the type will be double as below:
> x <- 2 > typeof(x)  "double"
We can se double there, so basically R by default will store your integer variable (the single
x) as a double just because is it anticipating that you will be require an aritmathic operation in future and because it will run in double mode anyway, it just easier for R to store variable like that right a way. If you now for instance that we are not to be running arithmatical operation for example the
x variable used for category or giving sequences to something or you never going to add them or running arithmatical operation then we can put ‘L’ in X variable to tell R it’s definetely an integer and we want to store it as integer.
The next one is Complex, we won’t be stopping on complex too long, let’s write
> z <- 3 + 2i
If you have done math, like a bit sophisticated math then you know what complex number are! If you don’t don’t really worry about it we won’t be dealing with them, I’m just going to show you how they works here, and as usual let’s check type of z and we will see that indeed complex!
> z <- 3 + 2i > typeof(z)  "complex"
Once again we won’t be stopping that for too long, so let’s move on reamaining two. Remain two are character and boolean.
We are going to create
a variable and inside the variable we are going to put a letter
h, to put a letter in a variable we need to put quotaion mark
" and wrap it insite letter or word.
> a <- "h" > typeof(a)  "character"
Finally we are coming to the last data type, an it is logical. In order to create a logical variable, let’s first create a name, let say
q1. Logical variable is basically either
false and here we are going to assign true, for true we just letter
T as below
> q1 <- T > typeof(q1)  "logical"
As you can see it’s logical!
By the way there other way we can create logical variable, for instance we could say
q2 is not true, we can type
F or we can type the whole world
FALSE but has to be in caps.
> q2 <- FALSE > typeof(q2)  "logical"
To see all variable that we created we can check it by calling
ls() it will call all variables in global environtment!
> ls()  "a" "q1" "q2" "x" "y" "z"
Alright that’s all, see you next time an happy coding!