41
www.utm.my innova-ve entrepreneurial global 1 Workshop: Ge,ng Started with R. UiTM 12 July 2017 .© Dr. Norhaiza Ahmad Getting Started with R for newbies PART C: DATA OBJECT STRUCTURE IN R 12 July 2017 Dr. NorhaizaAhmad Department of Mathematical Sciences Faculty of Science Universiti Teknologi Malaysia http://science.utm.my/norhaiza/

Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   1  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

12 July 2017 "Dr. Norhaiza Ahmad"

Department of Mathematical Sciences"Faculty of Science"

Universiti Teknologi Malaysia"

Getting Started with R!for newbies!

!PART C: !

DATA OBJECT STRUCTURE IN R!12 July 2017 "

Dr. Norhaiza Ahmad"Department of Mathematical Sciences"

Faculty of Science"Universiti Teknologi Malaysia"

http://science.utm.my/norhaiza/!

Page 2: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   2  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Outline  

PART  C:  DATA  OBJECT  STRUCTURE  IN  R  1.  Types  of  Data  object  structure  

 Scalars    Vectors    Matrices    Other  structures:  Factors,  Lists,  Data  Frames  

2.  Checking  and  Changing  Data  Object  Structure  

 

Page 3: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   3  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Example  Iris  Dataset:  Flowers  with  Measurements    Sepal.Length  Sepal.Width  Petal.Length  Petal.Width  5.1                    3.5                    1.4                  0.2  4.9                    3.0                    1.4                  0.2  4.7                    3.2                    1.3                  0.2  4.6                    3.1                    1.5                  0.2  5.0                    3.6                    1.4                  0.2  5.4                  3.9                    1.7                  0.4  4.6                  3.4                    1.4                  0.3  5.0                  3.4                    1.5                  0.2  4.4                  2.9                    1.4                  0.2  4.9                  3.1                    1.5                  0.1  

Sepal  Length  

Sepal  Width  

Petal  Length  

Petal  Width  

1   5.1   3.5   1.4   0.2  2   4.9   3   1.4   0.2  3   4.7   3.2   1.3   0.2  4   4.6   3.1   1.5   0.2  5   5   3.6   1.4   0.2  6   5.4   3.9   1.7   0.4  7   4.6   3.4   1.4   0.3  8   5   3.4   1.5   0.2  9   4.4   2.9   1.4   0.2  10   4.9   3.1   1.5   0.1  

Scalar  

Vector  

Matrix  

Computa-onal  in  R  :  manipulaton  of  data  structure  –  Linear  Algebra.  In  this  sec-on  we  learn  to  understand  and  manipulate  data  in  R      

Page 4: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   4  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Example  Iris  Dataset:  Flowers  with  Measurements    Sepal.Length  Sepal.Width  Petal.Length  Petal.Width  5.1                    3.5                    1.4                  0.2  4.9                    3.0                    1.4                  0.2  4.7                    3.2                    1.3                  0.2  4.6                    3.1                    1.5                  0.2  5.0                    3.6                    1.4                  0.2  5.4                  3.9                    1.7                  0.4  4.6                  3.4                    1.4                  0.3  5.0                  3.4                    1.5                  0.2  4.4                  2.9                    1.4                  0.2  4.9                  3.1                    1.5                  0.1  

Sepal  Length  

Sepal  Width  

Petal  Length  

Petal  Width  

1   5.1   3.5   1.4   0.2  2   4.9   3   1.4   0.2  3   4.7   3.2   1.3   0.2  4   4.6   3.1   1.5   0.2  5   5   3.6   1.4   0.2  6   5.4   3.9   1.7   0.4  7   4.6   3.4   1.4   0.3  8   5   3.4   1.5   0.2  9   4.4   2.9   1.4   0.2  10   4.9   3.1   1.5   0.1   Iris  data  in  matrix  form  

x1,1 x1,2 x1,3 x1,4x2,1 x2,2 x2,3 x2,4 x j1 x j2 x jk x jp x10,1 x10,2 x10,3 x10,4

!

"

########

$

%

&&&&&&&&

Mul-variate  data  consists  of  mul-ple  vectors  and  scalars.    Computa-onally  we  are  free  to  work  with  our  data  as  separate  variables  

Page 5: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   5  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Intro:  Data  Structure  in  R  

Recall  •  In  Part  A-­‐  you  have  experimented  with  object  

assignment  using  R     > x = 2; x > len = 2; len x=2; len=2;x+2

These  are  examples  of  data  objects  in  R  

i.e    assigned  a  variable/name  to  a  value  

Page 6: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   6  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

       

amat =

x11 x12 x1k x1px21 x22 x2k x2 p x j1 x j2 x jk x jp xn1 xn2 xnk xnp

!

"

########

$

%

&&&&&&&&

x =

x1x2xn

!

"

######

$

%

&&&&&&

IntuiQvely  We  can  assign  data  objects  to  the  entries  in  a  set  of  data  

   

Page 7: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   7  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Outline  

PART  C:  DATA  STRUCTURE  IN  R  1.   Types  of  Data  structure  

 Scalars    Vectors    Matrices  &  Arrays    Other  structures:  Factors,  Lists,  Data  Frames  

2.  Checking  and  Changing  Data  Object  Structure  

   

Page 8: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   8  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Intro:  Data  Object  Structure  in  R  

•  There  are  6  Types  of  data  object  structure  in  R:  

•  Scalars  •  Vectors  •  Factors  •  Matrices  &  Arrays  •  Lists  •  Data  Frames  

NUMERIC  LOGICAL  MODE  (True/False)  STRINGS  

6  DATA  STRUCTURE  MODES  CONSIST  

OF  VALUES  

IMPORTANT  TO  KNOW  STRUCTURE  OF  DATA  as  Different  R  func-ons  might  use  a  par-cular  data  structure  

   

Page 9: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   9  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Types  of  Data  Object  Structure:  Scalar                

•  What  are  other  func-ons  in  R?  

•  Where  to  find  other  func-ons?  

•  simplest  type  of  data  object  structure  is  a  scalar.  •  Scalar  is  a  data  object  with  one  value  > x = 5 #create scalar data object!!

> y = 2 !!

> x*y+2!!> ”Apa khabar?”!!> 3 < 4!!!!!!

!!!![1] 12!![1]"Apa khabar?”!![1] TRUE!!!!!!

Page 10: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   10  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Handy  Tip  

To  check  the  mode  of  data  object:    

> x=5;x!> mode(x)!> mode(3<4)!> mode("Apa khabar?")!!

[1] 5![1] "numeric”![1] "logical”![1] "character”!

Page 11: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   11  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Outline  

PART  C:  DATA  STRUCTURE  IN  R  

1.   Types  of  Data  structure    Scalars    Vectors    Matrices    Other  structures:  Factors,  Lists,  Data  Frames  

2.  Checking  and  Changing  Data  Object  Structure    

 

Page 12: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   12  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Types  of  Data  Object  Structure:  Vectors                

•  What  are  other  func-ons  in  R?  

•  Where  to  find  other  func-ons?  

•  A  vector's  values  can  be  numbers,  strings,  logical  values,  or  any  other  type,  as  long  as  they're  all  the  same  type.  

(3)  A  vector's  values  can  be  numbers   > c(2,4,5) [1] 2 4 5!

(5)  A  vector's  values  can  be  strings   > c(‘a’,’b’) [1] "a" "b"!

(4)  Assign  a  vector  to  an  R  object   > x=c(2,4,5);x [1] 2 4 5!

(1)  This  is  a  scalar   > 5 [1] 5!

(2)  Assign  the  scalar  to  an  R  object   > x=5;x [1] 5!

•  The  c()  func-on  (c  is  short  for  Combine/Concatenate)  creates  a  new  vector  by  combining  a  list  of  values.  

Page 13: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   13  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Vector  Arithme-c  

Vectors  can  be  used  on  the  usual  arithme-c  opera-ons  in  R.    

If  the  vectors  are  of  different  length,  the  vector  with  the  shorter  length  is  repeated  to  the  length  of  the  longer  vector:    

TIP: Use length() to measure the length of a vector

> x1=c(1,2,3) > x2=c(4,5,6) > y=x1+x2;y

[1] 5 7 9

> x=1:10;x > y=c(1,3);y > x*y

[1] 1 2 3 4 5 6 7 8 9 10 [1] 1 3 [1] 1 6 3 12 5 18 7 24 9 30

Page 14: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   14  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Sequence  Vectors  in  R  

To  generate  a  sequence  of  real  numbers  use  the  func-on  seq()    

(2)  Other  op-ons  in  seq!(increment of 0.5)!

> seq(1,3,0.5) [1] 1.0 1.5 2.0 2.5 3.0!

(3)  vector  with  integers  from    3  down  to  1:  

> 3:1 [1] 2 4 5!

(1)  A  vector  from  1  to  3   > 1:3 [1] 1 2 3!

         alterna-vely..   > seq(1,3) [1] 1 2 3!

[1] 3 2 1!

         Seek/Link  help  document  for  seq!

Page 15: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   15  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Repeat  Sequence  Vectors  in  R  

         Seek/Link  help  document  for  rep()!

To  generate  a  sequence  of  repea-ng  sequences  use  rep()

> x1=rep(1:2,3);x1 [1] 1 2 1 2 1 2!

Page 16: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   16  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

TASK:  Generate  vector  sequence  

1.  Generate  the  following  sequences:     [1]8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0

2.  Generate  the  following  sequences:     [1]10.0 9.8 9.6 9.4 9.2 9.0 8.8 8.6 8.4 8.2 8.0 3.Write  the  command  to  produce 3 5 1 3 5 1 using R command ,rep()!

Page 17: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   17  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Generate  vector  (Random  Sequence)    

R  can  generate  a  random  sequence  from  a  number  of  probability  density  func-ons.  The  general  format  for  genera-ng  such  sequences  is:  rdensity(num,p1,p2,...)  where  density  is  the  probability  density  func-on,  num  is  the  number  of  values  to  generate    and  p1,  p2,  ...  are  the  values  needed  to  generate  from  the  density  func-on.    

Page 18: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   18  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

No   Task   R  Code   Output  

1   Generate  5    values  from  a  Normal  distribuQon  with  mean  5    and  standard  deviaQon  1.5  

> rnorm(10,5,1.5)!!!>r1=rnorm(5,5,1.5)!

[1] 2.597671 4.021968 5.820759![4] 4.130905 2.859552!!> r1![1] 4.600297 4.410904 3.577039![4] 4.812063 6.642143!!

2   Random  genera-on  for  the  Poisson  distribu-on  with  parameter  lambda.  package  <stats>  

> rpois(30,2)! [1] 1 2 2 1 2 4 1 0 3 1 2 3 3 2 2![16] 2 2 4 1 0 0 2 1 0 1 1 0 5 1 4!

!

         Seek  help  document  for  the  related  R  func-ons!

[NOTE: What is the difference between the two commands in Task 1?

Generate  vector  (Random  Sequence)    

Page 19: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   19  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Task:  Generate  vector  (Random  Sequence)  

 1.   Generate  100  values  from  a  binomial  distribuQon  of  size  23  and  

probability  0.25.        2.  Generate  80  values  from  a  standard  normal  distribuQon,  stored  in  an  R  object  called  r8.  Then  find  the  mean,  standard  deviaQon  of  this  dataset.          [NOTE:  use  summary()  to  your  set  of  data  in  quesQon  2.  What  is  the  output  for  this?]  

Page 20: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   20  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

ExtracQng  &  Assigning  elements  from  vectors  

> x=c(1:10)*2 > x!

No   Task   R  Code   Output  

1   Extract  the  6th  element  in  x   > x[6]! [1] 12!

2   Extract  the  2nd  to  6th  element   > x[2:6] ! [1] 4 6 8 10 12!!

3   Extract  the  1st,  3rd  and  5th  element  in  x  

> x[c(1,3,5)]! [1] 2 6 10!!

4   Extract  reverse  order   > x[3:1] ![1] 6 4 2!

We  can  EXTRACT  an  element  from  the  vector  or  a  subset  of  the  vector  by  indica-ng  the  INDEX  of  THE  ELEMENTS  using  square  brackets  [ ].    

[1] 2 4 6 8 10 12 14 16 18 20!

Page 21: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   21  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

We  can  EXTRACT  and  assign  subsets  to  a  vector  that  we  have  extracted  

No   Task   R  Code   Output  

5   Extract  Dis-nct  ranges    

> x[c(1:3,5:6)]!!> x[c(1:3,7,10)]!

[1] 2 4 6 10 12!![1] 2 4 6 14 20  

6   Extract  Repeated  index   > x[rep(c(9,10),2)]! [1] 18 20 18 20!  

7   Extract  and  assign  subset  to  a  vector  

>ab=x[c(1:3,7,10)]! > ab![1] 2 4 6 14 20!  

8   Extract  logical  vector   > x>10!!> x[x>10]!

[1]  FALSE  FALSE  FALSE  FALSE  FALSE    TRUE    [7]    TRUE    TRUE    TRUE    TRUE    [1]  12  14  16  18  20  

ExtracQng  &  Assigning  elements  from  vectors  

Page 22: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   22  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Excluding  elements  from  vectors  

We  can  EXCLUDE  elements  from  a  vector  by  indica-ng  the  NEGATIVE  index  of  the  element(s)    using  square  brackets  [ ].    

No   Task   R  Code   Output  9   Exclude  the  

6th  element  in  x  

> x[-6]! [1] 2 4 6 8 10 14 16 18 20!

10   Exclude  the  2nd  to  6th  element  

> x[-(2:6)] ![1] 2 14 16 18 20!!

11   Exclude  the  1st,  3rd  and  5th  element  in  x  

> x[-c(1,3,5)]! [1] 4 8 12 14 16 18 20!

Page 23: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   23  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Other  useful  funcQons  

We  can  EXTRACT  an  element  from  the  vector  or  a  subset  of  the  vector  using  square  brackets  [ ].    

> x=6:15!> length(x) #Number of elements in x![1] 10!> max(x) #Largest value in x![1] 15!> min(x) #Least value in x![1] 6!> sum(x) #Sum of all values in x![1] 105!> prod(x) #Product of all values in x![1] 10897286400!> mean(x) #Average of all values in x![1] 10.5!> range(x) #Range of vector x![1] 6 15!> var(x) #Variance of x![1] 9.166667!> sd(x) #Standard deviation of x![1] 3.02765!> sqrt(var(x)) #Square root of variance=sd of x![1] 3.02765!

Page 24: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   24  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Outline  

PART  C:  DATA  STRUCTURE  IN  R  

1.   Types  of  Data  structure    Scalars    Vectors  

 Matrices      Other  structures:  Factors,  Lists,  Data  Frames  

2.  Checking  and  Changing  Data  Object  Structure    

 

Page 25: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   25  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Types  of  Data  Object  Structure:  Matrices                

•  R  stores  data  elements  in  a  2-­‐dimensional  matrix  using  the  func-on  matrix()!

•  Computa-onally  efficient  -­‐  Manipulate  data  as  matrices    

Array  is  a  matrix  with  more  than  2-­‐dimension  

Page 26: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   26  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Matrices  in  R   A=        2      5                    5      6                    1      8  

> x.mat=matrix(c(2,5,1,5,6,8),nrow=3,ncol=2)!

> x.mat=matrix(c(2,5,1,5,6,8),ncol=2)!

[,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8!

[,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8!

> x.mat=matrix(c(2,5,1,5,6,8),3,2)! [,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8!

Page 27: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   27  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

ExtracQng  element(s)from  a  matrix  

No   Task   R  Code   Output  

1   Extract  element  in  3rd  row  of  1st  column  

> x.mat[3,1]! [1] 1!

2   Extract  all  observa-ons  in  the  2nd  column  

> x.mat[ ,2] ![1] 5 6 8!!

3   Extract  t  all  observa-ons  in  the  3rd  row   ?! [1] 1 8!

!

4   Extract  submatrices   > x.mat[1:2,]! !

[,1] [,2]![1,] 2 5![2,] 5 6!

Just  like  vectors  elements  are  indicated  by  the  labels  in  the  matrices  > x.mat! [,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8!

[ROW,COLUMN]  

Page 28: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   28  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Combining  matrix  or  vectors(matrix  form)  to  a  matrix  

No   Task   R  Code   Output  1   Combining  

a  vector  > y.mat=matrix(1:3,3,1)! [,1]!

[1,] 1![2,] 2![3,] 3!

> cbind(x.mat,y.mat)! > cbind(x.mat,y.mat)! [,1] [,2] [,3]![1,] 2 5 1![2,] 5 6 2![3,] 1 8 3!

Provided  the  length  is  appropriate  > x.mat! [,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8!

Combine matrices by columns!cbind()  

Page 29: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   29  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Combining  matrix  or  vectors(matrix  form)    to  a  matrix  

No   Task   R  Code   Output  1   Create  matrix  

z.mat  in  R   ?!  

z.mat! [,1] [,2]![1,] 1 4![2,] 2 5!

2   Combine  matrix  x.mat  and  z.mat  by  rows  and  assign  the  new  matrix  as  A.mat  

?! A.mat! [,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8![4,] 1 4![5,] 2 5!!

Combine matrices by row rbind()  

Page 30: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   30  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Transposing  a  matrix:  t()  

No  

Task   R  Code   Output  

1   Transpose  a  matrix  

> t(x.mat)!!

[,1] [,2] [,3]![1,] 2 5 1![2,] 5 6 8!

Page 31: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   31  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Matrix  ArithmeQc  OperaQons:  No   Task   R  Code   Output  

1   Check  dimension  of  matrix  

> dim(x.mat)!> dim(y.mat)!!

[1] 3 2![1] 3 1!#clearly these two matrices cannot be multiplied!

2   Matrix  mul-plica-on  

> dim(t(x.mat))!> t(x.mat)%*%y.mat! !

[1] 2 3! [,1]![1,] 15![2,] 41!

3   Inverse  of  a  matrix  #solve() func-on.    The  matrix  must  be  square  and  not  singular.      

>A=matrix(sample(4),2,2)!!# any matrix!

! !

[,1] [,2]![1,] 2 3![2,] 4 1!

> solve(A)!!#inverse of A!#TEST that the AA-1=I!

[,1] [,2]![1,] -0.1 0.3![2,] 0.4 -0.2!

Matrix  mul-plica-ons  

%*%!Ensure  appropriate  dimension  

Page 32: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   32  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Outline  

PART  C:  DATA  STRUCTURE  IN  R  1.   Types  of  Data  structure  

 Scalars    Vectors    Matrices    Other  structures:  Factors,  Lists,  Data  Frames  

2.  Checking  and  Changing  Data  Object  Structure  

   

Page 33: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   33  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Other  Types  of  Data  Object  Structure:                

Factor  •   type  of  character/string  vector  •  Typically  used  to  describe  the  data-­‐  not  for  calcula-ons  •  Labels  for  qualita-ve  variables  

> quality=c("High","Medium","Low")!> quality=factor(quality)!

> quality![1] High Medium Low !Levels: High Low Medium!

factor()  

Page 34: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   34  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Other  Types  of  Data  Object  Structure:                

Lists  •   data  objects  containing  ‘every’  elements  •  Contains  element  of  miscellaneous  modes  •  Useful  for  organising  informa-on  > mylist= list(5,6,c(1,2,3),c("blue","red"),x.mat)!

> mylist![[1]]![1] 5!![[2]]![1] 6!!

[[3]]![1] 1 2 3!![[4]]![1] "blue" "red" !![[5]]! [,1] [,2]![1,] 2 5![2,] 5 6![3,] 1 8!

list()  

> mylist[[5]]!

Elements  on  a  list  can  be  extracted  

Page 35: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   35  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Other  Types  of  Data  Object  Structure:                

Data  Frames  •  similar  to  a  spreadsheet  •  Each  column  is  a  vector.  Elements  in  each  vector  has  the  same  mode.  Different  vectors  can  have  different  modes.  

•  All  vectors  in  the  data  frame  must  be  the  same  length  

data.frame()  

> x=1:2!> y=c(”a”,”b”)!> z=c(100,200)!> A.df=data.frame(x,y,z);A.df!!

> A.df! x y z!1 1 a 100!2 2 b 200!

Page 36: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   36  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Other  Types  of  Data  Object  Structure:                

Data  Frames  •  If  the  elements  are  defined  within  a  dataframe,  •  Use  anach(name  of  data  frame)  to  read  the  elements  

data.frame()  

> A.df=data.frame(x1=1:2,y1=c("a","b"),c1=c(100,200))!!!!> x1!!> attach(A.df)!> x1!!!> # or use $ sign!>A.df$x1!!

> A.df!x1 y1 c1!1 1 a 100!2   2 b 200!

Error: object 'x1' not found!

> x1![1] 1 2!

> A.df$x1![1] 1 2!

Page 37: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   37  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Handy  Tip  ●  To  call  a  variable  in  a  dataframe.  We  could  use  $  sign  or    anach(<dataframe>)  then  call  the  variable.  ●  However,  if  the  name  of  the  variable  in  the  dataframe  has  already  been  

defined  earlier  as  a  data  object  outside  the  dataframe  (Global  environment),  Thus-­‐Calling  the  variable  in  a  dataframe  arer  anach()  might  fail,  as  the  variable  in  the  dataframe  could  be  ‘masked’  by  the  data  object.  

●  Say      >x1=c(“here”,”there”)  #defined  as  a  data  object  .  Then  > A.df=data.frame(x1=1:2,y1=c("a","b"),c1=c(100,200))!Thus, if > attach(A.df);x1!Then x1 is displayed as “here”,”there” instead of 1:2!!●  One  way  is  to  remove  the  data  object  x1  

> rm(x1)!Then attach again!!

Page 38: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   38  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Outline  

PART  C:  DATA  STRUCTURE  IN  R  1.  Types  of  Data  structure  

 Scalars    Vectors    Matrices    Other  structures:  Factors,  Lists,  Data  Frames  

2.   Checking  and  Changing  Data  Object  Structure  

   

Page 39: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   39  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Checking  Data  Object  Structure  •  Iden-fy  types  of  Data  structure:  vector,  matrix,  list  etc  

is.<what>  Eg. is.vector();is.matrix();is.numeric; is.character  

> x<-c(1,2,3,4) !> #check data object type !> is.vector(x) !!> is.data.frame(x) !!> #check data mode !> is.character(x) !!> is.numeric(x) !!

!![1] TRUE !![1] FALSE !!![1] FALSE !!![1] TRUE !

Page 40: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   40  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

Change  Data  Object  Structure  •  Forcing  a  structure  to  another  

as.<what>  Eg. as.vector();as.matrix();as.numeric; as.character  

> x<-c(1,2,3,4) !!!!> x1=as.matrix(x)!!!!!

> x![1] 1 2 3 4!!!> x1! [,1]![1,] 1![2,] 2![3,] 3![4,] 4!

Page 41: Getting Started with R - Universiti Teknologi Malaysia€¦ · Workshop:)Ge,ng)Started)with)R.)UiTM)12)July)2017).©)Dr.)Norhaiza)Ahmad) 12 July 2017 "Dr. Norhaiza Ahmad" Department

www.utm.my     innova-ve  ●  entrepreneurial  ●  global   41  

Workshop:  Ge,ng  Started  with  R.  UiTM  12  July  2017  .©  Dr.  Norhaiza  Ahmad  

NEXT  

● PART  D:  Reading  Data