<TeXmacs|1.0.3.2>

<style|generic>

<\body>
  <section|Lecture 2>

  <subsection|Matrices and arrays>

  A matrix is just like a vector. (In fact, a matrix is a vector!)

  It is easy to create a matrix:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      matrix(1:12,3,4)
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2] [,3] [,4]

      [1,] \ \ \ 1 \ \ \ 4 \ \ \ 7 \ \ 10

      [2,] \ \ \ 2 \ \ \ 5 \ \ \ 8 \ \ 11

      [3,] \ \ \ 3 \ \ \ 6 \ \ \ 9 \ \ 12
    </output>

    <\textput>
      Notice the order in which the numbers go into the matrix: down the
      first column, then down the second, and so on.
    </textput>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=matrix(1:12,3,4)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[2,2]
    </input>

    <\output>
      [1] 5
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Just like with a vector, it is easy to address submatrices of matrices:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[1:2,2:3]
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2]

      [1,] \ \ \ 4 \ \ \ 7

      [2,] \ \ \ 5 \ \ \ 8
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[,1]
    </input>

    <\output>
      [1] 1 2 3
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[1,]
    </input>

    <\output>
      [1] \ 1 \ 4 \ 7 10
    </output>
  </session>>

  If we leave one of the indexes out, it is assume that we mean all the
  dimensions.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[1:2,1:2]=0
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2] [,3] [,4]

      [1,] \ \ \ 0 \ \ \ 0 \ \ \ 7 \ \ 10

      [2,] \ \ \ 0 \ \ \ 0 \ \ \ 8 \ \ 11

      [3,] \ \ \ 3 \ \ \ 6 \ \ \ 9 \ \ 12
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a\<gtr\>8
    </input>

    <\output>
      \ \ \ \ \ \ [,1] \ [,2] \ [,3] [,4]

      [1,] FALSE FALSE FALSE TRUE

      [2,] FALSE FALSE FALSE TRUE

      [3,] FALSE FALSE \ TRUE TRUE
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[a\<gtr\>8]=a[a\<gtr\>8]*3
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2] [,3] [,4]

      [1,] \ \ \ 0 \ \ \ 0 \ \ \ 7 \ \ 30

      [2,] \ \ \ 0 \ \ \ 0 \ \ \ 8 \ \ 33

      [3,] \ \ \ 3 \ \ \ 6 \ \ 27 \ \ 36
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  In the exercise we saw the distribution of adding up two numbers - it was
  humped, but not really normal. Let us see this again:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      x=sample(10,50000,rep=T);y=sample(10,50000,rep=T)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      hist(x+y,breaks=0.5:20.5);v()
    </input>

    <\output>
      <postscript|<tuple|<raw-data|%!PS-Adobe-3.0
      EPSF-3.0\n%%DocumentNeededResources: font Helvetica\n%%+ font
      Helvetica-Bold\n%%+ font Helvetica-Oblique\n%%+ font
      Helvetica-BoldOblique\n%%+ font Symbol\n%%Title: R Graphics
      Output\n%%Creator: R Software\n%%Pages: (atend)\n%%BoundingBox: 154 277
      442 565\n%%EndComments\n%%BeginProlog\n/bp \ { gs gs } def\n% begin
      .ps.prolog\n/gs \ { gsave } def\n/gr \ { grestore } def\n/ep \ {
      showpage gr gr } def\n/m \ \ { moveto } def\n/l \ { rlineto } def\n/np
      \ { newpath } def\n/cp \ { closepath } def\n/f \ \ { fill } def\n/o
      \ \ { stroke } def\n/c \ \ { newpath 0 360 arc } def\n/r \ \ { 4 2 roll
      moveto 1 copy 3 -1 roll exch 0 exch rlineto 0 rlineto -1 mul 0 exch
      rlineto closepath } def\n/p1 \ { stroke } def\n/p2 \ { gsave bg
      setrgbcolor fill grestore newpath } def\n/p3 \ { gsave bg setrgbcolor
      fill grestore stroke } def\n/t \ \ { 6 -2 roll moveto gsave rotate\n
      \ \ \ \ \ \ ps mul neg 0 2 1 roll rmoveto\n \ \ \ \ \ \ 1 index
      stringwidth pop\n \ \ \ \ \ \ mul neg 0 rmoveto show grestore }
      def\n/cl \ { grestore gsave newpath 3 index 3 index moveto 1 index\n
      \ \ \ \ \ \ 4 -1 roll lineto \ exch 1 index lineto lineto\n
      \ \ \ \ \ \ closepath clip newpath } def\n/rgb { setrgbcolor } def\n/s
      \ \ { scalefont setfont } def\n/R \ \ { /Font1 findfont } def\n/B \ \ {
      /Font2 findfont } def\n/I \ \ { /Font3 findfont } def\n/BI \ { /Font4
      findfont } def\n/S \ \ { /Font5 findfont } def\n1 setlinecap 1
      setlinejoin\n% end \ \ .ps.prolog\n%%IncludeResource: font
      Helvetica\n/Helvetica findfont\ndup length dict begin\n \ {1 index /FID
      ne {def} {pop pop} ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n
      \ currentdict\n \ end\n/Font1 exch definefont pop\n%%IncludeResource:
      font Helvetica-Bold\n/Helvetica-Bold findfont\ndup length dict begin\n
      \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font2 exch definefont
      pop\n%%IncludeResource: font Helvetica-Oblique\n/Helvetica-Oblique
      findfont\ndup length dict begin\n \ {1 index /FID ne {def} {pop pop}
      ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n \ currentdict\n
      \ end\n/Font3 exch definefont pop\n%%IncludeResource: font
      Helvetica-BoldOblique\n/Helvetica-BoldOblique findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font4 exch definefont
      pop\n%%IncludeResource: font Symbol\n/Symbol findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n
      \ currentdict\n \ end\n/Font5 exch definefont pop\n%%EndProlog\n%%Page:
      2 2\nbp\n202.84 338.14 416.44 515.74 cl\n153.64 276.94 441.64 564.94
      cl\n/ps 14 def B 14 s\n0 0 0 rgb\n309.64 535.32 (Histogram of x + y) .5
      0 0 t\n/ps 12 def R 12 s\n309.64 292.54 (x + y) .5 0 0 t\n164.44 426.94
      (Frequency) .5 0 90 t\n153.64 276.94 441.64 564.94 cl\n0 0 0 rgb\n0.75
      setlinewidth\n[] 0 setdash\nnp\n205.80 338.14 m\n197.78 -0.00
      l\no\nnp\n205.80 338.14 m\n0.00 -6.00 l\no\nnp\n255.25 338.14 m\n0
      -6.00 l\no\nnp\n304.69 338.14 m\n-0.00 -6.00 l\no\nnp\n354.14 338.14
      m\n-0.00 -6.00 l\no\nnp\n403.58 338.14 m\n-0.00 -6.00 l\no\n/ps 12 def
      R 12 s\n205.80 316.54 (0) .5 0 0 t\n255.25 316.54 (5) .5 0 0 t\n304.69
      316.54 (10) .5 0 0 t\n354.14 316.54 (15) .5 0 0 t\n403.58 316.54 (20)
      .5 0 0 t\nnp\n202.84 344.72 m\n0.00 166.34 l\no\nnp\n202.84 344.72
      m\n-6.00 0.00 l\no\nnp\n202.84 377.99 m\n-6.00 0.00 l\no\nnp\n202.84
      411.26 m\n-6.00 -0.00 l\no\nnp\n202.84 444.53 m\n-6.00 -0.00
      l\no\nnp\n202.84 477.80 m\n-6.00 0.00 l\no\nnp\n202.84 511.06 m\n-6.00
      0.00 l\no\n188.44 344.72 (0) .5 0 90 t\n188.44 377.99 (1000) .5 0 90
      t\n188.44 444.53 (3000) .5 0 90 t\n188.44 511.06 (5000) .5 0 90
      t\n202.84 338.14 416.44 515.74 cl\n0 0 0 rgb\n0.75 setlinewidth\n[] 0
      setdash\n210.75 344.72 9.89 0.00 r p1\n220.64 344.72 9.89 16.60 r
      p1\n230.53 344.72 9.89 32.77 r p1\n240.42 344.72 9.89 53.89 r
      p1\n250.30 344.72 9.89 64.87 r p1\n260.19 344.72 9.89 83.04 r
      p1\n270.08 344.72 9.89 99.44 r p1\n279.97 344.72 9.89 117.04 r
      p1\n289.86 344.72 9.89 133.14 r p1\n299.75 344.72 9.89 148.84 r
      p1\n309.64 344.72 9.89 164.44 r p1\n319.53 344.72 9.89 150.94 r
      p1\n329.42 344.72 9.89 134.84 r p1\n339.30 344.72 9.89 114.41 r
      p1\n349.19 344.72 9.89 98.91 r p1\n359.08 344.72 9.89 84.37 r
      p1\n368.97 344.72 9.89 66.27 r p1\n378.86 344.72 9.89 51.27 r
      p1\n388.75 344.72 9.89 32.74 r p1\n398.64 344.72 9.89 15.60 r
      p1\nep\n%%Trailer\n%%Pages: 2\n%%EOF\n>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Very triangular! What happens if we add 3 numbers?

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      z=sample(10,50000,rep=T)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      hist(x+y+z,breaks=0.5:30.5);v()
    </input>

    <\output>
      <postscript|<tuple|<raw-data|%!PS-Adobe-3.0
      EPSF-3.0\n%%DocumentNeededResources: font Helvetica\n%%+ font
      Helvetica-Bold\n%%+ font Helvetica-Oblique\n%%+ font
      Helvetica-BoldOblique\n%%+ font Symbol\n%%Title: R Graphics
      Output\n%%Creator: R Software\n%%Pages: (atend)\n%%BoundingBox: 154 277
      442 565\n%%EndComments\n%%BeginProlog\n/bp \ { gs gs } def\n% begin
      .ps.prolog\n/gs \ { gsave } def\n/gr \ { grestore } def\n/ep \ {
      showpage gr gr } def\n/m \ \ { moveto } def\n/l \ { rlineto } def\n/np
      \ { newpath } def\n/cp \ { closepath } def\n/f \ \ { fill } def\n/o
      \ \ { stroke } def\n/c \ \ { newpath 0 360 arc } def\n/r \ \ { 4 2 roll
      moveto 1 copy 3 -1 roll exch 0 exch rlineto 0 rlineto -1 mul 0 exch
      rlineto closepath } def\n/p1 \ { stroke } def\n/p2 \ { gsave bg
      setrgbcolor fill grestore newpath } def\n/p3 \ { gsave bg setrgbcolor
      fill grestore stroke } def\n/t \ \ { 6 -2 roll moveto gsave rotate\n
      \ \ \ \ \ \ ps mul neg 0 2 1 roll rmoveto\n \ \ \ \ \ \ 1 index
      stringwidth pop\n \ \ \ \ \ \ mul neg 0 rmoveto show grestore }
      def\n/cl \ { grestore gsave newpath 3 index 3 index moveto 1 index\n
      \ \ \ \ \ \ 4 -1 roll lineto \ exch 1 index lineto lineto\n
      \ \ \ \ \ \ closepath clip newpath } def\n/rgb { setrgbcolor } def\n/s
      \ \ { scalefont setfont } def\n/R \ \ { /Font1 findfont } def\n/B \ \ {
      /Font2 findfont } def\n/I \ \ { /Font3 findfont } def\n/BI \ { /Font4
      findfont } def\n/S \ \ { /Font5 findfont } def\n1 setlinecap 1
      setlinejoin\n% end \ \ .ps.prolog\n%%IncludeResource: font
      Helvetica\n/Helvetica findfont\ndup length dict begin\n \ {1 index /FID
      ne {def} {pop pop} ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n
      \ currentdict\n \ end\n/Font1 exch definefont pop\n%%IncludeResource:
      font Helvetica-Bold\n/Helvetica-Bold findfont\ndup length dict begin\n
      \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font2 exch definefont
      pop\n%%IncludeResource: font Helvetica-Oblique\n/Helvetica-Oblique
      findfont\ndup length dict begin\n \ {1 index /FID ne {def} {pop pop}
      ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n \ currentdict\n
      \ end\n/Font3 exch definefont pop\n%%IncludeResource: font
      Helvetica-BoldOblique\n/Helvetica-BoldOblique findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font4 exch definefont
      pop\n%%IncludeResource: font Symbol\n/Symbol findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n
      \ currentdict\n \ end\n/Font5 exch definefont pop\n%%EndProlog\n%%Page:
      2 2\nbp\n202.84 338.14 416.44 515.74 cl\n153.64 276.94 441.64 564.94
      cl\n/ps 14 def B 14 s\n0 0 0 rgb\n309.64 535.32 (Histogram of x + y +
      z) .5 0 0 t\n/ps 12 def R 12 s\n309.64 292.54 (x + y + z) .5 0 0
      t\n164.44 426.94 (Frequency) .5 0 90 t\n153.64 276.94 441.64 564.94
      cl\n0 0 0 rgb\n0.75 setlinewidth\n[] 0 setdash\nnp\n207.45 338.14
      m\n197.78 -0.00 l\no\nnp\n207.45 338.14 m\n-0.00 -6.00 l\no\nnp\n240.42
      338.14 m\n-0.00 -6.00 l\no\nnp\n273.38 338.14 m\n-0.00 -6.00
      l\no\nnp\n306.34 338.14 m\n-0.00 -6.00 l\no\nnp\n339.30 338.14 m\n0.00
      -6.00 l\no\nnp\n372.27 338.14 m\n-0.00 -6.00 l\no\nnp\n405.23 338.14
      m\n0.00 -6.00 l\no\n/ps 12 def R 12 s\n207.45 316.54 (0) .5 0 0
      t\n240.42 316.54 (5) .5 0 0 t\n273.38 316.54 (10) .5 0 0 t\n306.34
      316.54 (15) .5 0 0 t\n339.30 316.54 (20) .5 0 0 t\n372.27 316.54 (25)
      .5 0 0 t\n405.23 316.54 (30) .5 0 0 t\nnp\n202.84 344.72 m\n0.00 131.03
      l\no\nnp\n202.84 344.72 m\n-6.00 0.00 l\no\nnp\n202.84 388.40 m\n-6.00
      -0.00 l\no\nnp\n202.84 432.08 m\n-6.00 -0.00 l\no\nnp\n202.84 475.75
      m\n-6.00 0 l\no\n188.44 344.72 (0) .5 0 90 t\n188.44 388.40 (1000) .5 0
      90 t\n188.44 432.08 (2000) .5 0 90 t\n188.44 475.75 (3000) .5 0 90
      t\n202.84 338.14 416.44 515.74 cl\n0 0 0 rgb\n0.75 setlinewidth\n[] 0
      setdash\n210.75 344.72 6.59 0.00 r p1\n217.34 344.72 6.59 0.00 r
      p1\n223.93 344.72 6.59 2.18 r p1\n230.53 344.72 6.59 6.33 r p1\n237.12
      344.72 6.59 13.41 r p1\n243.71 344.72 6.59 22.28 r p1\n250.30 344.72
      6.59 34.11 r p1\n256.90 344.72 6.59 47.04 r p1\n263.49 344.72 6.59
      61.06 r p1\n270.08 344.72 6.59 78.31 r p1\n276.67 344.72 6.59 99.37 r
      p1\n283.27 344.72 6.59 116.44 r p1\n289.86 344.72 6.59 138.54 r
      p1\n296.45 344.72 6.59 151.56 r p1\n303.05 344.72 6.59 156.15 r
      p1\n309.64 344.72 6.59 164.44 r p1\n316.23 344.72 6.59 164.31 r
      p1\n322.82 344.72 6.59 160.51 r p1\n329.42 344.72 6.59 151.30 r
      p1\n336.01 344.72 6.59 137.76 r p1\n342.60 344.72 6.59 119.81 r
      p1\n349.19 344.72 6.59 98.06 r p1\n355.79 344.72 6.59 79.19 r
      p1\n362.38 344.72 6.59 59.93 r p1\n368.97 344.72 6.59 46.04 r
      p1\n375.56 344.72 6.59 33.89 r p1\n382.16 344.72 6.59 20.48 r
      p1\n388.75 344.72 6.59 12.97 r p1\n395.34 344.72 6.59 6.42 r p1\n401.93
      344.72 6.59 1.97 r p1\nep\n%%Trailer\n%%Pages: 2\n%%EOF\n>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Somewhat more bell-shaped. What happens when we look at more numbers?

  There is an easier way to do this than to redefine variables and add them
  up:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      x=matrix( sample(1:10,50000*10,rep=T), 50000,10 )
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      y=apply(x,1,sum)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      length(y)
    </input>

    <\output>
      [1] 50000
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      hist(y,n=30);v()
    </input>

    <\output>
      <postscript|<tuple|<raw-data|%!PS-Adobe-3.0
      EPSF-3.0\n%%DocumentNeededResources: font Helvetica\n%%+ font
      Helvetica-Bold\n%%+ font Helvetica-Oblique\n%%+ font
      Helvetica-BoldOblique\n%%+ font Symbol\n%%Title: R Graphics
      Output\n%%Creator: R Software\n%%Pages: (atend)\n%%BoundingBox: 154 277
      442 565\n%%EndComments\n%%BeginProlog\n/bp \ { gs gs } def\n% begin
      .ps.prolog\n/gs \ { gsave } def\n/gr \ { grestore } def\n/ep \ {
      showpage gr gr } def\n/m \ \ { moveto } def\n/l \ { rlineto } def\n/np
      \ { newpath } def\n/cp \ { closepath } def\n/f \ \ { fill } def\n/o
      \ \ { stroke } def\n/c \ \ { newpath 0 360 arc } def\n/r \ \ { 4 2 roll
      moveto 1 copy 3 -1 roll exch 0 exch rlineto 0 rlineto -1 mul 0 exch
      rlineto closepath } def\n/p1 \ { stroke } def\n/p2 \ { gsave bg
      setrgbcolor fill grestore newpath } def\n/p3 \ { gsave bg setrgbcolor
      fill grestore stroke } def\n/t \ \ { 6 -2 roll moveto gsave rotate\n
      \ \ \ \ \ \ ps mul neg 0 2 1 roll rmoveto\n \ \ \ \ \ \ 1 index
      stringwidth pop\n \ \ \ \ \ \ mul neg 0 rmoveto show grestore }
      def\n/cl \ { grestore gsave newpath 3 index 3 index moveto 1 index\n
      \ \ \ \ \ \ 4 -1 roll lineto \ exch 1 index lineto lineto\n
      \ \ \ \ \ \ closepath clip newpath } def\n/rgb { setrgbcolor } def\n/s
      \ \ { scalefont setfont } def\n/R \ \ { /Font1 findfont } def\n/B \ \ {
      /Font2 findfont } def\n/I \ \ { /Font3 findfont } def\n/BI \ { /Font4
      findfont } def\n/S \ \ { /Font5 findfont } def\n1 setlinecap 1
      setlinejoin\n% end \ \ .ps.prolog\n%%IncludeResource: font
      Helvetica\n/Helvetica findfont\ndup length dict begin\n \ {1 index /FID
      ne {def} {pop pop} ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n
      \ currentdict\n \ end\n/Font1 exch definefont pop\n%%IncludeResource:
      font Helvetica-Bold\n/Helvetica-Bold findfont\ndup length dict begin\n
      \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font2 exch definefont
      pop\n%%IncludeResource: font Helvetica-Oblique\n/Helvetica-Oblique
      findfont\ndup length dict begin\n \ {1 index /FID ne {def} {pop pop}
      ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n \ currentdict\n
      \ end\n/Font3 exch definefont pop\n%%IncludeResource: font
      Helvetica-BoldOblique\n/Helvetica-BoldOblique findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font4 exch definefont
      pop\n%%IncludeResource: font Symbol\n/Symbol findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n
      \ currentdict\n \ end\n/Font5 exch definefont pop\n%%EndProlog\n%%Page:
      2 2\nbp\n202.84 338.14 416.44 515.74 cl\n153.64 276.94 441.64 564.94
      cl\n/ps 14 def B 14 s\n0 0 0 rgb\n309.64 535.32 (Histogram of y) .5 0 0
      t\n/ps 12 def R 12 s\n309.64 292.54 (y) .5 0 0 t\n164.44 426.94
      (Frequency) .5 0 90 t\n153.64 276.94 441.64 564.94 cl\n0 0 0 rgb\n0.75
      setlinewidth\n[] 0 setdash\nnp\n204.76 338.14 m\n209.76 -0.00
      l\no\nnp\n204.76 338.14 m\n-0.00 -6.00 l\no\nnp\n234.72 338.14 m\n-0.00
      -6.00 l\no\nnp\n264.69 338.14 m\n-0.00 -6.00 l\no\nnp\n294.65 338.14
      m\n-0.00 -6.00 l\no\nnp\n324.62 338.14 m\n0.00 -6.00 l\no\nnp\n354.59
      338.14 m\n-0.00 -6.00 l\no\nnp\n384.55 338.14 m\n0.00 -6.00
      l\no\nnp\n414.52 338.14 m\n-0.00 -6.00 l\no\n/ps 12 def R 12 s\n204.76
      316.54 (20) .5 0 0 t\n234.72 316.54 (30) .5 0 0 t\n264.69 316.54 (40)
      .5 0 0 t\n294.65 316.54 (50) .5 0 0 t\n324.62 316.54 (60) .5 0 0
      t\n354.59 316.54 (70) .5 0 0 t\n384.55 316.54 (80) .5 0 0 t\n414.52
      316.54 (90) .5 0 0 t\nnp\n202.84 344.72 m\n0.00 150.32 l\no\nnp\n202.84
      344.72 m\n-6.00 0.00 l\no\nnp\n202.84 382.30 m\n-6.00 0.00
      l\no\nnp\n202.84 419.88 m\n-6.00 -0.00 l\no\nnp\n202.84 457.46 m\n-6.00
      -0.00 l\no\nnp\n202.84 495.04 m\n-6.00 0.00 l\no\n188.44 344.72 (0) .5
      0 90 t\n188.44 382.30 (1000) .5 0 90 t\n188.44 419.88 (2000) .5 0 90
      t\n188.44 457.46 (3000) .5 0 90 t\n188.44 495.04 (4000) .5 0 90
      t\n202.84 338.14 416.44 515.74 cl\n0 0 0 rgb\n0.75 setlinewidth\n[] 0
      setdash\n210.75 344.72 5.99 0.30 r p1\n216.74 344.72 5.99 0.75 r
      p1\n222.74 344.72 5.99 1.24 r p1\n228.73 344.72 5.99 3.83 r p1\n234.72
      344.72 5.99 5.75 r p1\n240.72 344.72 5.99 9.36 r p1\n246.71 344.72 5.99
      16.95 r p1\n252.70 344.72 5.99 26.64 r p1\n258.70 344.72 5.99 40.62 r
      p1\n264.69 344.72 5.99 56.74 r p1\n270.68 344.72 5.99 76.62 r
      p1\n276.67 344.72 5.99 95.79 r p1\n282.67 344.72 5.99 118.37 r
      p1\n288.66 344.72 5.99 135.13 r p1\n294.65 344.72 5.99 150.80 r
      p1\n300.65 344.72 5.99 159.71 r p1\n306.64 344.72 5.99 164.44 r
      p1\n312.63 344.72 5.99 154.07 r p1\n318.63 344.72 5.99 147.72 r
      p1\n324.62 344.72 5.99 128.14 r p1\n330.61 344.72 5.99 107.44 r
      p1\n336.61 344.72 5.99 86.47 r p1\n342.60 344.72 5.99 64.22 r
      p1\n348.59 344.72 5.99 48.44 r p1\n354.59 344.72 5.99 31.60 r
      p1\n360.58 344.72 5.99 21.27 r p1\n366.57 344.72 5.99 11.99 r
      p1\n372.57 344.72 5.99 6.80 r p1\n378.56 344.72 5.99 3.83 r p1\n384.55
      344.72 5.99 1.95 r p1\n390.55 344.72 5.99 1.13 r p1\n396.54 344.72 5.99
      0.49 r p1\n402.53 344.72 5.99 0.30 r p1\nep\n%%Trailer\n%%Pages:
      2\n%%EOF\n>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  So, adding 10 numbers gives something that has normal distribution. This is
  actually called the central limit theorem: if we sample numbers
  (independently), and add them up, the distribution of the sum will approach
  a normal as the number of numbers goes up.

  This is pretty nice: the average of the numbers is just the sum devided by
  n, so that means that no matter what distribution we take, the average is
  normaly distributed.

  (The distribution itself is not normal, but the mean of a sample is normaly
  distributed.)

  But let us go back to what we did:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=matrix(1:3,3,3)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2] [,3]

      [1,] \ \ \ 1 \ \ \ 1 \ \ \ 1

      [2,] \ \ \ 2 \ \ \ 2 \ \ \ 2

      [3,] \ \ \ 3 \ \ \ 3 \ \ \ 3
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,1,sum)
    </input>

    <\output>
      [1] 3 6 9
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,2,sum)
    </input>

    <\output>
      [1] 6 6 6
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      c(sum(a[1,]),sum(a[2,]),sum(a[3,]))
    </input>

    <\output>
      [1] 3 6 9
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  \ <verbatim|apply()> takes a matrix, and applies a function to each row,
  \ or each column. We can use any function we want, as long as it can be
  applied to a vector.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,1,mean)
    </input>

    <\output>
      [1] 1 2 3
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,2,min)
    </input>

    <\output>
      [1] 1 1 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,1,cumsum)
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2] [,3]

      [1,] \ \ \ 1 \ \ \ 2 \ \ \ 3

      [2,] \ \ \ 2 \ \ \ 4 \ \ \ 6

      [3,] \ \ \ 3 \ \ \ 6 \ \ \ 9
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a
    </input>

    <\output>
      \ \ \ \ \ [,1] [,2] [,3]

      [1,] \ \ \ 1 \ \ \ 1 \ \ \ 1

      [2,] \ \ \ 2 \ \ \ 2 \ \ \ 2

      [3,] \ \ \ 3 \ \ \ 3 \ \ \ 3
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  The last example shows that the result of the function can be a vector,
  instead of simply a number. In that case, we get a matrix back.

  So, to calculate a sum of 10 numbers 50000 times, we created a matrix of
  50000*10 random numbers, and then summed each 10 up.\ 

  Back to the central limit theorem. Let us try a really strange
  distribution. How about one that has a small hump very far away?

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      x=c( rnorm(1000), rnorm(100,mean=10) )
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      hist(x,breaks=30);v()
    </input>

    <\output>
      <postscript|<tuple|<raw-data|%!PS-Adobe-3.0
      EPSF-3.0\n%%DocumentNeededResources: font Helvetica\n%%+ font
      Helvetica-Bold\n%%+ font Helvetica-Oblique\n%%+ font
      Helvetica-BoldOblique\n%%+ font Symbol\n%%Title: R Graphics
      Output\n%%Creator: R Software\n%%Pages: (atend)\n%%BoundingBox: 154 277
      442 565\n%%EndComments\n%%BeginProlog\n/bp \ { gs gs } def\n% begin
      .ps.prolog\n/gs \ { gsave } def\n/gr \ { grestore } def\n/ep \ {
      showpage gr gr } def\n/m \ \ { moveto } def\n/l \ { rlineto } def\n/np
      \ { newpath } def\n/cp \ { closepath } def\n/f \ \ { fill } def\n/o
      \ \ { stroke } def\n/c \ \ { newpath 0 360 arc } def\n/r \ \ { 4 2 roll
      moveto 1 copy 3 -1 roll exch 0 exch rlineto 0 rlineto -1 mul 0 exch
      rlineto closepath } def\n/p1 \ { stroke } def\n/p2 \ { gsave bg
      setrgbcolor fill grestore newpath } def\n/p3 \ { gsave bg setrgbcolor
      fill grestore stroke } def\n/t \ \ { 6 -2 roll moveto gsave rotate\n
      \ \ \ \ \ \ ps mul neg 0 2 1 roll rmoveto\n \ \ \ \ \ \ 1 index
      stringwidth pop\n \ \ \ \ \ \ mul neg 0 rmoveto show grestore }
      def\n/cl \ { grestore gsave newpath 3 index 3 index moveto 1 index\n
      \ \ \ \ \ \ 4 -1 roll lineto \ exch 1 index lineto lineto\n
      \ \ \ \ \ \ closepath clip newpath } def\n/rgb { setrgbcolor } def\n/s
      \ \ { scalefont setfont } def\n/R \ \ { /Font1 findfont } def\n/B \ \ {
      /Font2 findfont } def\n/I \ \ { /Font3 findfont } def\n/BI \ { /Font4
      findfont } def\n/S \ \ { /Font5 findfont } def\n1 setlinecap 1
      setlinejoin\n% end \ \ .ps.prolog\n%%IncludeResource: font
      Helvetica\n/Helvetica findfont\ndup length dict begin\n \ {1 index /FID
      ne {def} {pop pop} ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n
      \ currentdict\n \ end\n/Font1 exch definefont pop\n%%IncludeResource:
      font Helvetica-Bold\n/Helvetica-Bold findfont\ndup length dict begin\n
      \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font2 exch definefont
      pop\n%%IncludeResource: font Helvetica-Oblique\n/Helvetica-Oblique
      findfont\ndup length dict begin\n \ {1 index /FID ne {def} {pop pop}
      ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n \ currentdict\n
      \ end\n/Font3 exch definefont pop\n%%IncludeResource: font
      Helvetica-BoldOblique\n/Helvetica-BoldOblique findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font4 exch definefont
      pop\n%%IncludeResource: font Symbol\n/Symbol findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n
      \ currentdict\n \ end\n/Font5 exch definefont pop\n%%EndProlog\n%%Page:
      2 2\nbp\n202.84 338.14 416.44 515.74 cl\n153.64 276.94 441.64 564.94
      cl\n/ps 14 def B 14 s\n0 0 0 rgb\n309.64 535.32 (Histogram of x) .5 0 0
      t\n/ps 12 def R 12 s\n309.64 292.54 (x) .5 0 0 t\n164.44 426.94
      (Frequency) .5 0 90 t\n153.64 276.94 441.64 564.94 cl\n0 0 0 rgb\n0.75
      setlinewidth\n[] 0 setdash\nnp\n254.01 338.14 m\n123.61 -0.00
      l\no\nnp\n254.01 338.14 m\n-0.00 -6.00 l\no\nnp\n315.82 338.14 m\n-0.00
      -6.00 l\no\nnp\n377.62 338.14 m\n0.00 -6.00 l\no\n/ps 12 def R 12
      s\n254.01 316.54 (0) .5 0 0 t\n315.82 316.54 (5) .5 0 0 t\n377.62
      316.54 (10) .5 0 0 t\nnp\n202.84 344.72 m\n0.00 167.80 l\no\nnp\n202.84
      344.72 m\n-6.00 0.00 l\no\nnp\n202.84 386.67 m\n-6.00 0.00
      l\no\nnp\n202.84 428.62 m\n-6.00 0.00 l\no\nnp\n202.84 470.57 m\n-6.00
      -0.00 l\no\nnp\n202.84 512.52 m\n-6.00 -0.00 l\no\n188.44 344.72 (0) .5
      0 90 t\n188.44 386.67 (50) .5 0 90 t\n188.44 428.62 (100) .5 0 90
      t\n188.44 470.57 (150) .5 0 90 t\n188.44 512.52 (200) .5 0 90 t\n202.84
      338.14 416.44 515.74 cl\n0 0 0 rgb\n0.75 setlinewidth\n[] 0
      setdash\n210.75 344.72 6.18 1.68 r p1\n216.93 344.72 6.18 3.36 r
      p1\n223.11 344.72 6.18 15.10 r p1\n229.29 344.72 6.18 40.27 r
      p1\n235.47 344.72 6.18 72.99 r p1\n241.65 344.72 6.18 119.98 r
      p1\n247.83 344.72 6.18 164.44 r p1\n254.01 344.72 6.18 140.11 r
      p1\n260.19 344.72 6.18 134.24 r p1\n266.37 344.72 6.18 78.87 r
      p1\n272.55 344.72 6.18 48.66 r p1\n278.74 344.72 6.18 17.62 r
      p1\n284.92 344.72 6.18 0.00 r p1\n291.10 344.72 6.18 1.68 r p1\n297.28
      344.72 6.18 0.00 r p1\n303.46 344.72 6.18 0.00 r p1\n309.64 344.72 6.18
      0.00 r p1\n315.82 344.72 6.18 0.00 r p1\n322.00 344.72 6.18 0.00 r
      p1\n328.18 344.72 6.18 0.00 r p1\n334.36 344.72 6.18 0.00 r p1\n340.54
      344.72 6.18 0.00 r p1\n346.72 344.72 6.18 1.68 r p1\n352.90 344.72 6.18
      1.68 r p1\n359.08 344.72 6.18 10.07 r p1\n365.26 344.72 6.18 17.62 r
      p1\n371.44 344.72 6.18 10.91 r p1\n377.62 344.72 6.18 16.78 r
      p1\n383.80 344.72 6.18 10.91 r p1\n389.99 344.72 6.18 6.71 r p1\n396.17
      344.72 6.18 5.03 r p1\n402.35 344.72 6.18 2.52 r
      p1\nep\n%%Trailer\n%%Pages: 2\n%%EOF\n>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  What will the distribution of the mean of 10 numbers drawn from this
  distribution look like?

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=matrix( sample(x,50000*10,rep=T), 50000, 10)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      y=apply(a,1,mean)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      hist(y,br=30);v()
    </input>

    <\output>
      <postscript|<tuple|<raw-data|%!PS-Adobe-3.0
      EPSF-3.0\n%%DocumentNeededResources: font Helvetica\n%%+ font
      Helvetica-Bold\n%%+ font Helvetica-Oblique\n%%+ font
      Helvetica-BoldOblique\n%%+ font Symbol\n%%Title: R Graphics
      Output\n%%Creator: R Software\n%%Pages: (atend)\n%%BoundingBox: 154 277
      442 565\n%%EndComments\n%%BeginProlog\n/bp \ { gs gs } def\n% begin
      .ps.prolog\n/gs \ { gsave } def\n/gr \ { grestore } def\n/ep \ {
      showpage gr gr } def\n/m \ \ { moveto } def\n/l \ { rlineto } def\n/np
      \ { newpath } def\n/cp \ { closepath } def\n/f \ \ { fill } def\n/o
      \ \ { stroke } def\n/c \ \ { newpath 0 360 arc } def\n/r \ \ { 4 2 roll
      moveto 1 copy 3 -1 roll exch 0 exch rlineto 0 rlineto -1 mul 0 exch
      rlineto closepath } def\n/p1 \ { stroke } def\n/p2 \ { gsave bg
      setrgbcolor fill grestore newpath } def\n/p3 \ { gsave bg setrgbcolor
      fill grestore stroke } def\n/t \ \ { 6 -2 roll moveto gsave rotate\n
      \ \ \ \ \ \ ps mul neg 0 2 1 roll rmoveto\n \ \ \ \ \ \ 1 index
      stringwidth pop\n \ \ \ \ \ \ mul neg 0 rmoveto show grestore }
      def\n/cl \ { grestore gsave newpath 3 index 3 index moveto 1 index\n
      \ \ \ \ \ \ 4 -1 roll lineto \ exch 1 index lineto lineto\n
      \ \ \ \ \ \ closepath clip newpath } def\n/rgb { setrgbcolor } def\n/s
      \ \ { scalefont setfont } def\n/R \ \ { /Font1 findfont } def\n/B \ \ {
      /Font2 findfont } def\n/I \ \ { /Font3 findfont } def\n/BI \ { /Font4
      findfont } def\n/S \ \ { /Font5 findfont } def\n1 setlinecap 1
      setlinejoin\n% end \ \ .ps.prolog\n%%IncludeResource: font
      Helvetica\n/Helvetica findfont\ndup length dict begin\n \ {1 index /FID
      ne {def} {pop pop} ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n
      \ currentdict\n \ end\n/Font1 exch definefont pop\n%%IncludeResource:
      font Helvetica-Bold\n/Helvetica-Bold findfont\ndup length dict begin\n
      \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font2 exch definefont
      pop\n%%IncludeResource: font Helvetica-Oblique\n/Helvetica-Oblique
      findfont\ndup length dict begin\n \ {1 index /FID ne {def} {pop pop}
      ifelse} forall\n \ /Encoding ISOLatin1Encoding def\n \ currentdict\n
      \ end\n/Font3 exch definefont pop\n%%IncludeResource: font
      Helvetica-BoldOblique\n/Helvetica-BoldOblique findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n \ /Encoding
      ISOLatin1Encoding def\n \ currentdict\n \ end\n/Font4 exch definefont
      pop\n%%IncludeResource: font Symbol\n/Symbol findfont\ndup length dict
      begin\n \ {1 index /FID ne {def} {pop pop} ifelse} forall\n
      \ currentdict\n \ end\n/Font5 exch definefont pop\n%%EndProlog\n%%Page:
      2 2\nbp\n202.84 338.14 416.44 515.74 cl\n153.64 276.94 441.64 564.94
      cl\n/ps 14 def B 14 s\n0 0 0 rgb\n309.64 535.32 (Histogram of y) .5 0 0
      t\n/ps 12 def R 12 s\n309.64 292.54 (y) .5 0 0 t\n164.44 426.94
      (Frequency) .5 0 90 t\n153.64 276.94 441.64 564.94 cl\n0 0 0 rgb\n0.75
      setlinewidth\n[] 0 setdash\nnp\n250.30 338.14 m\n148.34 -0.00
      l\no\nnp\n250.30 338.14 m\n0.00 -6.00 l\no\nnp\n299.75 338.14 m\n0
      -6.00 l\no\nnp\n349.19 338.14 m\n-0.00 -6.00 l\no\nnp\n398.64 338.14
      m\n-0.00 -6.00 l\no\n/ps 12 def R 12 s\n250.30 316.54 (0) .5 0 0
      t\n299.75 316.54 (2) .5 0 0 t\n349.19 316.54 (4) .5 0 0 t\n398.64
      316.54 (6) .5 0 0 t\nnp\n202.84 344.72 m\n0.00 141.25 l\no\nnp\n202.84
      344.72 m\n-6.00 0.00 l\no\nnp\n202.84 380.03 m\n-6.00 -0.00
      l\no\nnp\n202.84 415.35 m\n-6.00 0.00 l\no\nnp\n202.84 450.66 m\n-6.00
      0.00 l\no\nnp\n202.84 485.97 m\n-6.00 0.00 l\no\n188.44 344.72 (0) .5 0
      90 t\n188.44 380.03 (1000) .5 0 90 t\n188.44 450.66 (3000) .5 0 90
      t\n202.84 338.14 416.44 515.74 cl\n0 0 0 rgb\n0.75 setlinewidth\n[] 0
      setdash\n210.75 344.72 4.94 0.04 r p1\n215.69 344.72 4.94 0.07 r
      p1\n220.64 344.72 4.94 0.60 r p1\n225.58 344.72 4.94 3.04 r p1\n230.53
      344.72 4.94 14.83 r p1\n235.47 344.72 4.94 48.73 r p1\n240.42 344.72
      4.94 102.51 r p1\n245.36 344.72 4.94 154.35 r p1\n250.30 344.72 4.94
      163.24 r p1\n255.25 344.72 4.94 131.68 r p1\n260.19 344.72 4.94 102.69
      r p1\n265.14 344.72 4.94 125.25 r p1\n270.08 344.72 4.94 161.02 r
      p1\n275.03 344.72 4.94 164.44 r p1\n279.97 344.72 4.94 116.21 r
      p1\n284.92 344.72 4.94 80.02 r p1\n289.86 344.72 4.94 65.89 r
      p1\n294.80 344.72 4.94 77.26 r p1\n299.75 344.72 4.94 71.40 r
      p1\n304.69 344.72 4.94 52.08 r p1\n309.64 344.72 4.94 28.78 r
      p1\n314.58 344.72 4.94 21.47 r p1\n319.53 344.72 4.94 20.90 r
      p1\n324.47 344.72 4.94 21.68 r p1\n329.42 344.72 4.94 12.82 r
      p1\n334.36 344.72 4.94 7.24 r p1\n339.30 344.72 4.94 4.17 r p1\n344.25
      344.72 4.94 4.13 r p1\n349.19 344.72 4.94 3.04 r p1\n354.14 344.72 4.94
      2.72 r p1\n359.08 344.72 4.94 1.06 r p1\n364.03 344.72 4.94 0.46 r
      p1\n368.97 344.72 4.94 0.42 r p1\n373.92 344.72 4.94 0.53 r p1\n378.86
      344.72 4.94 0.39 r p1\n383.80 344.72 4.94 0.18 r p1\n388.75 344.72 4.94
      0.04 r p1\n393.69 344.72 4.94 0.11 r p1\n398.64 344.72 4.94 0.00 r
      p1\n403.58 344.72 4.94 0.11 r p1\nep\n%%Trailer\n%%Pages:
      2\n%%EOF\n>|ps>||||||>
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Not very normal, yet. How many numbers does one need to add up to get a
  normal distribution?

  <subsection|Functions>

  How is it possible to apply something more complicated than a predefined
  function?

  Let us try to solve the following problem: throwing 10 die, what is the
  chance to get 2 equal in a row?

  First let us throw 10 die once:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      x=sample(1:6,10,rep=T);x
    </input>

    <\output>
      \ [1] 2 3 5 6 5 4 1 5 2 5
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  How would we check is two in a row are equal?

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      x[-1]==x[-10]
    </input>

    <\output>
      [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      sum(x[-1]==x[-10])
    </input>

    <\output>
      [1] 0
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  So, given x, the above expression is 0 if none are equal, and bigger than 0
  if some are equal.

  To apply it on an array we do the following:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=matrix( sample(1:6, 10*1000, rep=T), 1000, 10)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      x=apply(a,1, function(x) { sum(x[-1]==x[-10]) } )
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      table(x)
    </input>

    <\output>
      x

      \ \ 0 \ \ 1 \ \ 2 \ \ 3 \ \ 4 \ \ 5 \ \ 6\ 

      215 342 257 144 \ 35 \ \ 6 \ \ 1\ 
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  So, the form is '<verbatim|function(x) {> an expression on x that gives the
  desired result <verbatim|}>'.

  <subsection|Reading data>

  Let us look at data that I got from Philipp, saved in csv format:

  <with|prog-language|shell|prog-session|default|<\session>
    <\output>
      Shell session inside TeXmacs
    </output>

    <\input|shell] >
      cat Example1.csv
    </input>

    <\output>
      id,C_det,c1cCEL,c2cCEL,c3cCEL,h1cCEL,h2cCEL,h3cCEL,c,chan

      38396_at,38396_at,7.16081254,7.290660424,7.392737297,6.952853711,7.292026313,7.412756193,0.003868,y

      49409_i_at,49409_i_at,10.13546109,10.62638901,10.25252325,10.25231658,10.18956957,10.30671591,0.007848,n

      53038_at,53038_at,7.702094201,7.618807349,7.67603345,7.515808309,7.596549958,7.604193007,0.008735,n

      53434_at,,2.876379498,2.824038624,2.827237258,2.885637592,2.815227189,3.026897002,,n

      59043_at,59043_at,7.966912097,7.96113163,8.126213606,7.871402099,7.966892224,7.937320193,0.008627,y

      59455_at,59455_at,4.366778851,4.225955724,4.543832778,3.980990847,4.310665357,4.236237567,0.041165,n

      61679_at,61679_at,5.049982992,5.374783109,5.268838032,5.948013417,6.596032038,5.979160843,0.889628,n

      61971_at,61971_at,4.142098062,4.121471299,4.302961649,4.125880936,4.156195764,4.050269331,0.006094,n

      68376_at,,4.595836702,4.650914887,4.665653487,4.606630406,4.520145401,4.541359268,,n

      80627_at,80627_at,4.911620543,4.939508403,5.216403763,4.976140892,4.98236699,5.042316751,0.000494,y

      81035_i_at,81035_i_at,5.906465854,5.576539595,5.462219328,4.813760076,5.008206281,5.866770711,0.175418,n

      85462_at,85462_at,4.5502712,4.529353758,4.666960935,4.738686195,4.548837862,4.561258314,0.00116,n

      88170_at,88170_at,5.678974331,5.433884455,5.604304954,5.45987842,5.427371795,5.654569802,0.003416,n

      89639_at,89639_at,8.379944752,8.350986947,8.380638937,8.424241477,8.385903768,8.301314198,0,n

      91383_at,91383_at,2.507656878,2.612207403,2.619023484,3.533030418,3.805764366,3.669470322,1.187648,n

      32436_at,32436_at,8.739317645,9.187020063,8.777907612,9.126521153,9.008978032,9.307131573,0.060579,y

      49937_at,,5.586517151,6.146771071,5.498159076,6.136033905,6.008704221,6.140507136,,n

      81837_at,,6.141651244,6.200506461,6.159321513,5.490323476,5.224234723,5.639440434,,y

      85075_at,,6.479857121,6.593030444,6.545424085,6.432517662,6.854616025,6.613686102,,y

      34099_f_at,34099_f_at,4.558208637,4.80513002,4.505734211,5.251655915,5.200594346,5.289451867,0.389638,n

      35376_f_at,,3.713291278,3.634191811,3.712004514,3.613298263,3.691586806,3.912949963,,n

      49888_f_at,49888_f_at,11.10310726,11.07483456,11.14825469,10.97350419,10.91908758,10.80596038,0.043771,n

      <with|color|red|Interrupted TeXmacs shell>
    </output>

    <\input|shell] >
      \;
    </input>

    \;
  </session>>

  It has a header, and the values are comma-separated. The first column is
  the name of the gene, and the second column is a string.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil=read.table("Example1.csv",sep=",",head=T,row.names=1,as.is=2)
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil[1:5,]
    </input>

    <\output>
      \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ C.det \ \ \ c1cCEL \ \ \ c2cCEL
      \ \ \ c3cCEL \ \ \ h1cCEL \ \ \ h2cCEL

      38396_at \ \ \ \ 38396_at \ 7.160813 \ 7.290660 \ 7.392737 \ 6.952854
      \ 7.292026

      49409_i_at 49409_i_at 10.135461 10.626389 10.252523 10.252317 10.189570

      53038_at \ \ \ \ 53038_at \ 7.702094 \ 7.618807 \ 7.676033 \ 7.515808
      \ 7.596550

      53434_at \ \ \ \ \ \ \ \ \ \ \ \ \ \ 2.876379 \ 2.824039 \ 2.827237
      \ 2.885638 \ 2.815227

      59043_at \ \ \ \ 59043_at \ 7.966912 \ 7.961132 \ 8.126214 \ 7.871402
      \ 7.966892

      \ \ \ \ \ \ \ \ \ \ \ \ \ \ h3cCEL \ \ \ \ \ \ \ c chan

      38396_at \ \ \ 7.412756 0.003868 \ \ \ y

      49409_i_at 10.306716 0.007848 \ \ \ n

      53038_at \ \ \ 7.604193 0.008735 \ \ \ n

      53434_at \ \ \ 3.026897 \ \ \ \ \ \ NA \ \ \ n

      59043_at \ \ \ 7.937320 0.008627 \ \ \ y
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  Here are the parameters we used:

  <\itemize-dot>
    <item><verbatim|sep='',''> - The seperator between fields. Use slash-t
    for tab.

    <item>head=T - Use the first row as a header for the table

    <item>row.names=1 - Use the 1st column as a name for the rows

    <item>as.is=2 - Leave the 2nd column as is - it contains strings.
  </itemize-dot>

  What is the NA on the 4th row in column c? Not Available. In some cases
  data is not available. R enables us to deal with these cases.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=1:10
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a[3]=NA
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a
    </input>

    <\output>
      \ [1] \ 1 \ 2 NA \ 4 \ 5 \ 6 \ 7 \ 8 \ 9 10
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      min(a)
    </input>

    <\output>
      [1] NA
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      min(a,na.rm=T)
    </input>

    <\output>
      [1] 1
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      is.na(a)
    </input>

    <\output>
      \ [1] FALSE FALSE \ TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  What did we get when we read the file? A data.frame

  data.frame are very much like arrays. (Later we'll see that they are
  actually lists)

  Arrays, like vectors, contain only one data type - number, string, boolean,
  categorical.

  A data frame can contain several types of vectors, in a table. All have to
  have the same length.

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil$chan
    </input>

    <\output>
      \ [1] y n n n y n n n n y n n n n n y n y y n n n n

      Levels: n y
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  This is categorical data. It is like a string, but is stored as integers.\ 

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil[,1]
    </input>

    <\output>
      \ [1] "38396_at" \ \ "49409_i_at" "53038_at" \ \ ""
      \ \ \ \ \ \ \ \ \ \ "59043_at" \ 

      \ [6] "59455_at" \ \ "61679_at" \ \ "61971_at" \ \ ""
      \ \ \ \ \ \ \ \ \ \ "80627_at" \ 

      [11] "81035_i_at" "85462_at" \ \ "88170_at" \ \ "89639_at"
      \ \ "91383_at" \ 

      [16] "32436_at" \ \ "" \ \ \ \ \ \ \ \ \ \ "" \ \ \ \ \ \ \ \ \ \ ""
      \ \ \ \ \ \ \ \ \ \ "34099_f_at"

      [21] "" \ \ \ \ \ \ \ \ \ \ "49888_f_at" "87506_at" \ 
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  This is string data.

  You can see that data in a data.frame can be addressed like data in a
  table:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil[1:3,2:4]
    </input>

    <\output>
      \ \ \ \ \ \ \ \ \ \ \ \ \ \ c1cCEL \ \ \ c2cCEL \ \ \ c3cCEL

      38396_at \ \ \ 7.160813 \ 7.290660 \ 7.392737

      49409_i_at 10.135461 10.626389 10.252523

      53038_at \ \ \ 7.702094 \ 7.618807 \ 7.676033
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      colnames(phil)
    </input>

    <\output>
      [1] "C.det" \ "c1cCEL" "c2cCEL" "c3cCEL" "h1cCEL" "h2cCEL" "h3cCEL" "c"
      \ \ \ \ 

      [9] "chan" \ 
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      rownames(phil)
    </input>

    <\output>
      \ [1] "38396_at" \ \ "49409_i_at" "53038_at" \ \ "53434_at"
      \ \ "59043_at" \ 

      \ [6] "59455_at" \ \ "61679_at" \ \ "61971_at" \ \ "68376_at"
      \ \ "80627_at" \ 

      [11] "81035_i_at" "85462_at" \ \ "88170_at" \ \ "89639_at"
      \ \ "91383_at" \ 

      [16] "32436_at" \ \ "49937_at" \ \ "81837_at" \ \ "85075_at"
      \ \ "34099_f_at"

      [21] "35376_f_at" "49888_f_at" "87506_at" \ 
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  It is possible to add data to the data.frame:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil$c3c
    </input>

    <\output>
      \ [1] \ 7.392737 10.252523 \ 7.676033 \ 2.827237 \ 8.126214 \ 4.543833
      \ 5.268838

      \ [8] \ 4.302962 \ 4.665653 \ 5.216404 \ 5.462219 \ 4.666961 \ 5.604305
      \ 8.380639

      [15] \ 2.619023 \ 8.777908 \ 5.498159 \ 6.159322 \ 6.545424 \ 4.505734
      \ 3.712005

      [22] 11.148255 \ 9.341933
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil$big = phil$c3c \<gtr\> 5
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      phil[1:3,]
    </input>

    <\output>
      \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ C.det \ \ \ c1cCEL \ \ \ c2cCEL
      \ \ \ c3cCEL \ \ \ h1cCEL \ \ \ h2cCEL

      38396_at \ \ \ \ 38396_at \ 7.160813 \ 7.290660 \ 7.392737 \ 6.952854
      \ 7.292026

      49409_i_at 49409_i_at 10.135461 10.626389 10.252523 10.252317 10.189570

      53038_at \ \ \ \ 53038_at \ 7.702094 \ 7.618807 \ 7.676033 \ 7.515808
      \ 7.596550

      \ \ \ \ \ \ \ \ \ \ \ \ \ \ h3cCEL \ \ \ \ \ \ \ c chan \ big

      38396_at \ \ \ 7.412756 0.003868 \ \ \ y TRUE

      49409_i_at 10.306716 0.007848 \ \ \ n TRUE

      53038_at \ \ \ 7.604193 0.008735 \ \ \ n TRUE
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  We can also make a matrix out of part of a data.frame:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=phil[,2:4]
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a
    </input>

    <\output>
      \ \ \ \ \ \ \ \ \ \ \ \ \ \ c1cCEL \ \ \ c2cCEL \ \ \ c3cCEL

      38396_at \ \ \ 7.160813 \ 7.290660 \ 7.392737

      49409_i_at 10.135461 10.626389 10.252523

      53038_at \ \ \ 7.702094 \ 7.618807 \ 7.676033

      53434_at \ \ \ 2.876379 \ 2.824039 \ 2.827237

      59043_at \ \ \ 7.966912 \ 7.961132 \ 8.126214

      59455_at \ \ \ 4.366779 \ 4.225956 \ 4.543833

      61679_at \ \ \ 5.049983 \ 5.374783 \ 5.268838

      61971_at \ \ \ 4.142098 \ 4.121471 \ 4.302962

      68376_at \ \ \ 4.595837 \ 4.650915 \ 4.665653

      80627_at \ \ \ 4.911621 \ 4.939508 \ 5.216404

      81035_i_at \ 5.906466 \ 5.576540 \ 5.462219

      85462_at \ \ \ 4.550271 \ 4.529354 \ 4.666961

      88170_at \ \ \ 5.678974 \ 5.433884 \ 5.604305

      89639_at \ \ \ 8.379945 \ 8.350987 \ 8.380639

      91383_at \ \ \ 2.507657 \ 2.612207 \ 2.619023

      32436_at \ \ \ 8.739318 \ 9.187020 \ 8.777908

      49937_at \ \ \ 5.586517 \ 6.146771 \ 5.498159

      81837_at \ \ \ 6.141651 \ 6.200506 \ 6.159322

      85075_at \ \ \ 6.479857 \ 6.593030 \ 6.545424

      34099_f_at \ 4.558209 \ 4.805130 \ 4.505734

      35376_f_at \ 3.713291 \ 3.634192 \ 3.712005

      49888_f_at 11.103107 11.074835 11.148255

      87506_at \ \ \ 9.545659 \ 9.573302 \ 9.341933
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,1,mean)
    </input>

    <\output>
      \ \ 38396_at 49409_i_at \ \ 53038_at \ \ 53434_at \ \ 59043_at
      \ \ 59455_at\ 

      \ \ 7.281403 \ 10.338124 \ \ 7.665645 \ \ 2.842552 \ \ 8.018086
      \ \ 4.378856\ 

      \ \ 61679_at \ \ 61971_at \ \ 68376_at \ \ 80627_at 81035_i_at
      \ \ 85462_at\ 

      \ \ 5.231201 \ \ 4.188844 \ \ 4.637468 \ \ 5.022511 \ \ 5.648408
      \ \ 4.582195\ 

      \ \ 88170_at \ \ 89639_at \ \ 91383_at \ \ 32436_at \ \ 49937_at
      \ \ 81837_at\ 

      \ \ 5.572388 \ \ 8.370524 \ \ 2.579629 \ \ 8.901415 \ \ 5.743816
      \ \ 6.167160\ 

      \ \ 85075_at 34099_f_at 35376_f_at 49888_f_at \ \ 87506_at\ 

      \ \ 6.539437 \ \ 4.623024 \ \ 3.686496 \ 11.108732 \ \ 9.486965\ 
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      apply(a,2,mean)
    </input>

    <\output>
      \ \ c1cCEL \ \ c2cCEL \ \ c3cCEL\ 

      6.165170 6.232670 6.204101\ 
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  We can not <verbatim|apply()> on the whole frame, because it would first be
  converted all to strings.

  We can easily make a data.frame:

  <with|prog-language|r|prog-session|default|<\session>
    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      a=11:20
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      b=a^2
    </input>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      data.frame(a,b)
    </input>

    <\output>
      \ \ \ \ a \ \ b

      1 \ 11 121

      2 \ 12 144

      3 \ 13 169

      4 \ 14 196

      5 \ 15 225

      6 \ 16 256

      7 \ 17 289

      8 \ 18 324

      9 \ 19 361

      10 20 400
    </output>

    <\input|<with|color|red|\<gtr\> <with|color|black|>>>
      \;
    </input>
  </session>>

  \;
</body>

<\initial>
  <\collection>
    <associate|page-even|30mm>
    <associate|page-reduce-bot|15mm>
    <associate|page-reduce-right|25mm>
    <associate|page-reduce-left|25mm>
    <associate|page-type|a4>
    <associate|page-top|30mm>
    <associate|page-right|30mm>
    <associate|par-width|150mm>
    <associate|page-odd|30mm>
    <associate|page-bot|30mm>
    <associate|language|english>
    <associate|page-reduce-top|15mm>
  </collection>
</initial>

<\references>
  <\collection>
    <associate|toc-5|<tuple|<uninit>|?>>
    <associate|toc-1|<tuple|<uninit>|?>>
    <associate|toc-2|<tuple|<uninit>|?>>
    <associate|toc-3|<tuple|<uninit>|?>>
    <associate|toc-4|<tuple|<uninit>|?>>
  </collection>
</references>

<\auxiliary>
  <\collection>
    <\associate|toc>
      Lecture 2<value|toc-dots><pageref|toc-1>

      <with|par-left|<quote|1.5fn>|Cutting
      data<value|toc-dots><pageref|toc-2>>
    </associate>
  </collection>
</auxiliary>
