Generally, while doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that, when you create a variable you reserve some space in memory. You may like to store information of various data types like character, wide character, integer, floating point, double floating point, Boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
In contrast to other programming languages like C and java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable.
The elements of a vector must all have the same data type. You can have a vector consisting of three character strings or three integer elements , but not a vector with one integer element and two character string elements.
Note: There is nothing like scalar in R. The numbers are one-element vectors or atomic vectors.
Let’s create an variable object x and assign a value 8 to it.
x<-8
print(x)
## [1] 8
Note: “<-” and “=” are both assignment operators in R. But using “=” is discouraged because it dosen’t work in some situations. (Generally, “=” is used in function arguments).
print function is used to print the R objects.Note Everything that exists in R is an object in the sense that it is a kind of data structure that can be manipulated.
a<-3
b<-3.5
class(a)
## [1] "numeric"
class(b)
## [1] "numeric"
a<-3L
class(a)
## [1] "integer"
3.Character
a<-"Vishnu Institute of Technology"
class(a)
## [1] "character"
a<-(5<4)
class(a)
## [1] "logical"
a<-2+3i
class(a)
## [1] "complex"
6.Raw ( Converts the characters to hex) link
a<-charToRaw("Vishnu")
a
## [1] 56 69 73 68 6e 75
Try to create a vector with 3 members (2,“hi”,TRUE) and assign it to a variable v. What will be the value of v? and what is the datatype of v?
v<-c(2,"hi",TRUE)
v
## [1] "2" "hi" "TRUE"
class(v)
## [1] "character"
Here, numeric 2 and logical TRUE is converted into charcters.
as.integer(3.5) # converting numeric to integer data type
## [1] 3
as.integer("4.5") # converting character to integer
## [1] 4
as.integer("2nd CSE") # Unable to convert character to integer
## Warning: NAs introduced by coercion
## [1] NA
as.integer(TRUE) # True is 1
## [1] 1
as.integer(FALSE) # False is 0
## [1] 0
as.numeric("4.5") # converting character to numeric
## [1] 4.5
as.numeric(TRUE) # Converting logical to numeric
## [1] 1
as.numeric("3rd CSE") # Converting character to mumeric
## Warning: NAs introduced by coercion
## [1] NA
as.logical(1) # 1 is true
## [1] TRUE
as.logical(0) # 0 is False
## [1] FALSE
as.logical(100) # Any non-zero is TRUE
## [1] TRUE
as.logical("hi") # Unable to convert character to logical
## [1] NA
as.character(100) # converting numeric to character
## [1] "100"
as.character(100L) # converting integer to character
## [1] "100"
as.character(TRUE) # converting logical to character
## [1] "TRUE"