Data types in R


Generally for any technology to understand or work as a beginner we need to understand the types/input predefined instruction of the language platform we start working. In this we will discuss the type with calling and code snippet for R
Data types in R which includes inbuilt primitives and structured
  • vector
  • list
  • matrix
  • data frame
  • factors (we will avoid these, but they have their uses)
  • tables
  • logical
  • integer
  • numeric
  • complex
  • character

Vector
A vector can be a vector of characters, logical, integers or numeric. Creating object for Vector using vector() or c().

# Empty
Command::  v<-vector()
Print::          print(v)
Output  ::     logical(0)

# Predefined Length
 v <- vector(length = 10)
 print(v)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

#
 x <- c(1, 2, 3)
 print(x)
[1] 1 2 3

#
m <- c(1L, 2L, 3L)
 print(m)
[1] 1 2 3
#
y <- c(TRUE, TRUE, FALSE, FALSE)
> print(y)
[1]  TRUE  TRUE FALSE FALSE

#
> z <- c("Andrew", "Zach", "Mike")
> print(z)
[1] "Andrew" "Zach"   "Mike"

List
A list is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it. Lists are sometimes called recursive vectors, because a list can contain other lists. This makes them fundamentally different from atomic vectors.

# Empty
Command::  x <- list(10, "ax", TRUE, 1+4i)
Print::          print(x)
Output  ::    

 [[1]]
[1] 10

[[2]]
[1] "ax"

[[3]]
[1] TRUE

[[4]]
[1] 1+4i

#Length of List
Command:: length(x)
[1] 4


Matrix
Matrices are a special vector in R which holds row and column attributes. It can be created using a vector input to the matrix function.

# Empty
Command::  m <- matrix(1:6, nrow=2, ncol =3)

Print::          print(m)
Output::

[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6


We can also bind rows /column wise using the command cbind() and rbind()

 x <- 5:7
 y <- 6:8

 rbind(x,y)
  [,1] [,2] [,3]
x    5    6    7
y    6    7    8


 cbind(x,y)
     x y
[1,] 5 6
[2,] 6 7
[3,] 7 8



Array
Arrays can be of any number of dimensions opposed to matrix which is limited to  two dimensional. The array function takes a dim attribute which creates the required number of dimension.

)

# Command Prototype
Command:: a <- array(c('vegetable','Fruits'),dim = c(3,3,2))
> print(a)

, , 1

     [,1]        [,2]        [,3]      
[1,] "vegetable" "Fruits"    "vegetable"
[2,] "Fruits"    "vegetable" "Fruits"  
[3,] "vegetable" "Fruits"    "vegetable"

, , 2

     [,1]        [,2]        [,3]      
[1,] "Fruits"    "vegetable" "Fruits"  
[2,] "vegetable" "Fruits"    "vegetable"
[3,] "Fruits"    "vegetable" "Fruits"

Factor
Factor are a special vector in R. It can be ordered or unordered and are important when for modeling functions such as lm() and glm() and also in plot methods. Factors has to be pre-defined values.
.
# Empty
Command::  factor()

#Example

spectrum <- c('Red','green','yellow','Voilet','Cyan','Magenta','Orange')
factor_spectrum<-factor(spectrum)

print(factor_spectrum)

[1] Red     green   yellow  Voilet  Cyan    Magenta Orange
Levels: Cyan green Magenta Orange Red Voilet yellow

print(nlevels(factor_spectrum))
[1] 7

DataFrames
Data frames are data structure for most tabular data which we use for statistics in R. It has attributes rownames()which is used to annotate  the entity. Data frame is a special type of list where every element of a list has same length. Few additional built in function:-

·         data.matrix()
·         nrow(df) 
·         ncol(df)
·         cbind(df)
·         head()
·         tail()
·         Data frame is a special type of list where every element of a list has same length

# Empty
Command::  data.frame()


#Example
dFrame <- data.frame(id = letters[2:5], x = 2:5, y = rnorm(4))
 print(dFrame)
          id       x        y
1         b       2        0.3129572
2         c       3        1.6263950
3        d        4        0.8441991
4        e        5        1.0301756

cbind(dFrame, data.frame(z = 4))
  id x         y z
1  b 2 0.3129572 4
2  c 3 1.6263950 4
3  d 4 0.8441991 4
4  e 5 1.0301756 4


 head(dFrame,2)
          id       x         y
1        b        2        0.3129572
2        c        3        1.6263950

 tail(dFrame,2)
          id       x         y
3        d        4        0.8441991
4        e        5        1.0301756
Data types in R Reviewed by Rupesh on 23:35 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.