{ "cells": [ { "cell_type": "code", "execution_count": 208, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. 1
2. \n", "\t
3. 3
4. \n", "\t
5. 2
6. \n", "\t
7. 5
8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 3\n", "\\item 2\n", "\\item 5\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 2\n", "4. 5\n", "\n", "\n" ], "text/plain": [ "[1] 1 3 2 5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Chapter 2 Lab: Introduction to R\n", "\n", "# Basic Commands\n", "\n", "x <- c(1,3,2,5)\n", "x" ] }, { "cell_type": "code", "execution_count": 209, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. 1
2. \n", "\t
3. 6
4. \n", "\t
5. 2
6. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 6\n", "\\item 2\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 6\n", "3. 2\n", "\n", "\n" ], "text/plain": [ "[1] 1 6 2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x = c(1,6,2)\n", "x" ] }, { "cell_type": "code", "execution_count": 210, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "3" ], "text/latex": [ "3" ], "text/markdown": [ "3" ], "text/plain": [ "[1] 3" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "3" ], "text/latex": [ "3" ], "text/markdown": [ "3" ], "text/plain": [ "[1] 3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "y = c(1,4,3)\n", "length(x)\n", "length(y)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. 2
2. \n", "\t
3. 10
4. \n", "\t
5. 5
6. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 2\n", "\\item 10\n", "\\item 5\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 2\n", "2. 10\n", "3. 5\n", "\n", "\n" ], "text/plain": [ "[1] 2 10 5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x+y" ] }, { "cell_type": "code", "execution_count": 212, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. 'A'
2. \n", "\t
3. 'Auto'
4. \n", "\t
5. 'cylinders'
6. \n", "\t
7. 'df'
8. \n", "\t
9. 'f'
10. \n", "\t
11. 'fa'
12. \n", "\t
13. 'q'
14. \n", "\t
15. 'quit'
16. \n", "\t
17. 'x'
18. \n", "\t
19. 'y'
20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'A'\n", "\\item 'Auto'\n", "\\item 'cylinders'\n", "\\item 'df'\n", "\\item 'f'\n", "\\item 'fa'\n", "\\item 'q'\n", "\\item 'quit'\n", "\\item 'x'\n", "\\item 'y'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'A'\n", "2. 'Auto'\n", "3. 'cylinders'\n", "4. 'df'\n", "5. 'f'\n", "6. 'fa'\n", "7. 'q'\n", "8. 'quit'\n", "9. 'x'\n", "10. 'y'\n", "\n", "\n" ], "text/plain": [ " [1] \"A\" \"Auto\" \"cylinders\" \"df\" \"f\" \"fa\" \n", " [7] \"q\" \"quit\" \"x\" \"y\" " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ls()" ] }, { "cell_type": "code", "execution_count": 213, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. 'A'
2. \n", "\t
3. 'Auto'
4. \n", "\t
5. 'cylinders'
6. \n", "\t
7. 'df'
8. \n", "\t
9. 'f'
10. \n", "\t
11. 'fa'
12. \n", "\t
13. 'q'
14. \n", "\t
15. 'quit'
16. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'A'\n", "\\item 'Auto'\n", "\\item 'cylinders'\n", "\\item 'df'\n", "\\item 'f'\n", "\\item 'fa'\n", "\\item 'q'\n", "\\item 'quit'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'A'\n", "2. 'Auto'\n", "3. 'cylinders'\n", "4. 'df'\n", "5. 'f'\n", "6. 'fa'\n", "7. 'q'\n", "8. 'quit'\n", "\n", "\n" ], "text/plain": [ "[1] \"A\" \"Auto\" \"cylinders\" \"df\" \"f\" \"fa\" \n", "[7] \"q\" \"quit\" " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rm(x,y)\n", "ls()" ] }, { "cell_type": "code", "execution_count": 214, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm(list=ls())" ] }, { "cell_type": "code", "execution_count": 215, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
 matrix {base} R Documentation
\n", "\n", "

## Matrices

\n", "\n", "

### Description

\n", "\n", "

matrix creates a matrix from the given set of values.\n", "

\n", "

as.matrix attempts to turn its argument into a matrix.\n", "

\n", "

is.matrix tests if its argument is a (strict) matrix.\n", "

\n", "\n", "\n", "

### Usage

\n", "\n", "
\n",
"matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,\n",
"       dimnames = NULL)\n",
"\n",
"as.matrix(x, ...)\n",
"## S3 method for class 'data.frame'\n",
"as.matrix(x, rownames.force = NA, ...)\n",
"\n",
"is.matrix(x)\n",
"
\n", "\n", "\n", "

### Arguments

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
 data \n", "an optional data vector (including a list or\n", "expression vector). Non-atomic classed R objects are\n", "coerced by as.vector and all attributes discarded.\n", " nrow \n", "the desired number of rows.\n", " ncol \n", "the desired number of columns.\n", " byrow \n", "logical. If FALSE (the default) the matrix is\n", "filled by columns, otherwise the matrix is filled by rows.\n", " dimnames \n", "A dimnames attribute for the matrix:\n", "NULL or a list of length 2 giving the row and column\n", "names respectively. An empty list is treated as NULL, and a\n", "list of length one as row names. The list can be named, and the\n", "list names will be used as names for the dimensions.\n", " x \n", "an R object.\n", " ... \n", "additional arguments to be passed to or from methods.\n", " rownames.force \n", "logical indicating if the resulting matrix\n", "should have character (rather than NULL)\n", "rownames. The default, NA, uses NULL\n", "rownames if the data frame has ‘automatic’ row.names or for a\n", "zero-row data frame.\n", "
\n", "\n", "\n", "

### Details

\n", "\n", "

If one of nrow or ncol is not given, an attempt is\n", "made to infer it from the length of data and the other\n", "parameter. If neither is given, a one-column matrix is returned.\n", "

\n", "

If there are too few elements in data to fill the matrix,\n", "then the elements in data are recycled. If data has\n", "length zero, NA of an appropriate type is used for atomic\n", "vectors (0 for raw vectors) and NULL for lists.\n", "

\n", "

is.matrix returns TRUE if x is a vector and has a\n", "\"dim\" attribute of length 2) and FALSE otherwise.\n", "Note that a data.frame is not a matrix by this\n", "test. The function is generic: you can write methods to handle\n", "specific classes of objects, see InternalMethods.\n", "

\n", "

as.matrix is a generic function. The method for data frames\n", "will return a character matrix if there is only atomic columns and any\n", "non-(numeric/logical/complex) column, applying as.vector\n", "to factors and format to other non-character columns.\n", "Otherwise, the usual coercion hierarchy (logical < integer < double <\n", "complex) will be used, e.g., all-logical data frames will be coerced\n", "to a logical matrix, mixed logical-integer will give a integer matrix,\n", "etc.\n", "

\n", "

The default method for as.matrix calls as.vector(x), and\n", "hence e.g. coerces factors to character vectors.\n", "

\n", "

When coercing a vector, it produces a one-column matrix, and\n", "promotes the names (if any) of the vector to the rownames of the matrix.\n", "

\n", "

is.matrix is a primitive function.\n", "

\n", "

The print method for a matrix gives a rectangular layout with\n", "dimnames or indices. For a list matrix, the entries of length not\n", "one are printed in the form integer,7 indicating the type\n", "and length.\n", "

\n", "\n", "\n", "

### Note

\n", "\n", "

If you just want to convert a vector to a matrix, something like\n", "

\n", "
  dim(x) <- c(nx, ny)\n",
"  dimnames(x) <- list(row_names, col_names)\n",
"
\n", "

will avoid duplicating x.\n", "

\n", "\n", "\n", "

### References

\n", "\n", "

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\n", "The New S Language.\n", "Wadsworth & Brooks/Cole.\n", "

\n", "\n", "\n", "

\n", "\n", "

data.matrix, which attempts to convert to a numeric\n", "matrix.\n", "

\n", "

A matrix is the special case of a two-dimensional array.\n", "

\n", "\n", "\n", "

### Examples

\n", "\n", "
\n",
"is.matrix(as.matrix(1:10))\n",
"!is.matrix(warpbreaks)  # data.frame, NOT matrix!\n",
"warpbreaks[1:10,]\n",
"as.matrix(warpbreaks[1:10,])  # using as.matrix.data.frame(.) method\n",
"\n",
"## Example of setting row and column names\n",
"mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE,\n",
"               dimnames = list(c(\"row1\", \"row2\"),\n",
"                               c(\"C.1\", \"C.2\", \"C.3\")))\n",
"mdat\n",
"
\n", "\n", "
[Package base version 3.3.1 ]
 1 3 2 4
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t 1 & 3\\\\\n", "\t 2 & 4\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "\n", "\n" ], "text/plain": [ " [,1] [,2]\n", "[1,] 1 3\n", "[2,] 2 4" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x=matrix(data=c(1,2,3,4), nrow=2, ncol=2)\n", "x" ] }, { "cell_type": "code", "execution_count": 217, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\n", "
 1 2 3 4
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t 1 & 2\\\\\n", "\t 3 & 4\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 2\n", "4. 4\n", "\n", "\n" ], "text/plain": [ " [,1] [,2]\n", "[1,] 1 2\n", "[2,] 3 4" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\n", "
 1 1.73205 1.41421 2
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t 1.000000 & 1.732051\\\\\n", "\t 1.414214 & 2.000000\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 1.4142135623731\n", "3. 1.73205080756888\n", "4. 2\n", "\n", "\n" ], "text/plain": [ " [,1] [,2]\n", "[1,] 1.000000 1.732051\n", "[2,] 1.414214 2.000000" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\n", "
 1 9 4 16
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t 1 & 9\\\\\n", "\t 4 & 16\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 4\n", "3. 9\n", "4. 16\n", "\n", "\n" ], "text/plain": [ " [,1] [,2]\n", "[1,] 1 9\n", "[2,] 4 16" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x=matrix(c(1,2,3,4),2,2)\n", "matrix(c(1,2,3,4),2,2,byrow=TRUE)\n", "sqrt(x)\n", "x^2" ] }, { "cell_type": "code", "execution_count": 218, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "0.995230520811311" ], "text/latex": [ "0.995230520811311" ], "text/markdown": [ "0.995230520811311" ], "text/plain": [ "[1] 0.9952305" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x=rnorm(50)\n", "y=x+rnorm(50,mean=50,sd=.1)\n", "cor(x,y)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. -1.14397631447974
2. \n", "\t
3. 1.34212936561501
4. \n", "\t
5. 2.18539047574276
6. \n", "\t
7. 0.536392517923731
8. \n", "\t
9. 0.0631929664685468
10. \n", "\t
11. 0.502234482468979
12. \n", "\t
13. -0.000416724686432643
14. \n", "\t
15. 0.565819840539162
16. \n", "\t
17. -0.572522688962623
18. \n", "\t
19. -1.11022500727696
20. \n", "\t
21. -0.0486871233624514
22. \n", "\t
23. -0.695656217619366
24. \n", "\t
25. 0.828917480303335
26. \n", "\t
27. 0.206652855081802
28. \n", "\t
29. -0.235674509102427
30. \n", "\t
31. -0.556310491381104
32. \n", "\t
33. -0.364754357080585
34. \n", "\t
35. 0.862355034263622
36. \n", "\t
37. -0.63077153536771
38. \n", "\t
39. 0.313602125215739
40. \n", "\t
41. -0.931495317661393
42. \n", "\t
43. 0.823867618473952
44. \n", "\t
45. 0.523370702077482
46. \n", "\t
47. 0.706921411979056
48. \n", "\t
49. 0.420204325601679
50. \n", "\t
51. -0.269052154682033
52. \n", "\t
53. -1.51031729990999
54. \n", "\t
55. -0.69021247657504
56. \n", "\t
57. -0.143471952443572
58. \n", "\t
59. -1.0135274099044
60. \n", "\t
61. 1.57327373614751
62. \n", "\t
63. 0.0127465054882014
64. \n", "\t
65. 0.872647049887217
66. \n", "\t
67. 0.422066190530336
68. \n", "\t
69. -0.0188157916578866
70. \n", "\t
71. 2.61574896890584
72. \n", "\t
73. -0.693140174826871
74. \n", "\t
75. -0.266321780991085
76. \n", "\t
77. -0.720636441231524
78. \n", "\t
79. 1.36773420645149
80. \n", "\t
81. 0.264007332160512
82. \n", "\t
83. 0.632186807367191
84. \n", "\t
85. -1.33065098578719
86. \n", "\t
87. 0.0268888182209596
88. \n", "\t
89. 1.0406363207788
90. \n", "\t
91. 1.31202379854711
92. \n", "\t
93. -0.0300020766733214
94. \n", "\t
95. -0.250025712488174
96. \n", "\t
97. 0.0234144856913592
98. \n", "\t
99. 1.65987065574227
100. \n", "
\n", "\t
1. 1
2. \n", "\t
3. 2
4. \n", "\t
5. 3
6. \n", "\t
7. 4
8. \n", "\t
9. 5
10. \n", "\t
11. 6
12. \n", "\t
13. 7
14. \n", "\t
15. 8
16. \n", "\t
17. 9
18. \n", "\t
19. 10
20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 4\n", "\\item 5\n", "\\item 6\n", "\\item 7\n", "\\item 8\n", "\\item 9\n", "\\item 10\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "5. 5\n", "6. 6\n", "7. 7\n", "8. 8\n", "9. 9\n", "10. 10\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 4 5 6 7 8 9 10" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x=seq(1,10)\n", "x" ] }, { "cell_type": "code", "execution_count": 226, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
1. 1
2. \n", "\t
3. 2
4. \n", "\t
5. 3
6. \n", "\t
7. 4
8. \n", "\t
9. 5
10. \n", "\t
11. 6
12. \n", "\t
13. 7
14. \n", "\t
15. 8
16. \n", "\t
17. 9
18. \n", "\t
19. 10
20. \n", "