Topics covered-

1. 6 Data structures in R,

2. Vectors part 1:

2.1 Six data types (classes) of atomic vectors.

2.2Type conversion of atomic vectors

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.

R Has basically 6 structures of data.

  • Vectors
  • Matrices
  • Arrays
  • Lists
  • Factors
  • Dataframes

Vectors

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.

There are six data types of atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors.

  • Numeric
  • Integer
  • character
  • Logical
  • Complex
  • Raw

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.

Note: class is a property assigned to an object that determines how to operate with it.

  1. Numeric
a<-3
b<-3.5
class(a)
## [1] "numeric"
class(b)
## [1] "numeric"
  1. Integer
a<-3L
class(a)
## [1] "integer"

3.Character

a<-"Vishnu Institute of Technology"
class(a)
## [1] "character"
  1. Logical
a<-(5<4)
class(a)
## [1] "logical"
  1. complex
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

TYPE CONVERSION IN R

1. Internal type conversion

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.

2. Vector coercion (Explicit type conversion)

2.1 Converting other datatype to integer

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

2.2 Converting other datatypes to numeric

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

2.3 Converting other datatypes to logical

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

2.4 Converting other datatypes to character

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"