Explain any three data types used in R with suitable function/ examples.

Example 1: r data types

# Short answer:
# In R there are 6 main data types: character, numeric (real or decimal),
# integer, logical, complex, and raw. Use class(object) to determine the
# data type you are working with.

# Example data types:
Data Type		Examples	
Logical		TRUE, FALSE
Numeric		12.3, 5, 999 # In R, "ints" and "floats" are type numeric
Integer		2L, 34L, 0L	# Add an L after the number to specify type int
Character	'a', "good", "TRUE", '23.4'	
Complex		3 + 2i 	# This type isn't used much
Raw			"Hello" is stored as 48 65 6c 6c 6f	# This type isn't used much

Example 2: r data structures

# R’s base data structures can be organised by their dimensionality 
# (1D, 2D, or ND) and whether they’re homogeneous (all contents must be 
# of the same type) or heterogeneous (the contents can be of different 
# types). This gives rise to the five data types most often used in data 
# analysis:

Dimension	Homogeneous		Heterogeneous
1D			Atomic vector	List
2D			Matrix			Data frame
ND			Array

# Note, atomic vectors are always flat (i.e. they can't be nested), they 
#	are usually of type logical, integer, double (often called 
#	numeric), or character, and other R objects are built on atomic 
#	vectors.
# Note, because atomic vectors cannot contain mixed types, if you 
#	combine types, they will be coerced to the most flexible type of 
#	the types you used. In order of increasing flexibility, the types
#	are: logical -> integer -> double -> character

Example 3: data types in r

# Create a matrix.
M = matrix( c('a','a','b','c','b',1), nrow = 2, ncol = 3, byrow = TRUE)

print(M)

Tags:

R Example