{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Introduction to Jupyter and R\n", "\n", "This is a [Jupyter](https://jupyter.org/) notebook.\n", "Here is a [Jupyter tutorial](https://youtu.be/Rc4JQWowG5I).\n", "\n", "This _cell_ is a [Markdown](http://daringfireball.net/projects/markdown/syntax) cell: it contains text rather than executable code.\n", "\n", "You can also style text in a Markdown cell using html\n", "\n", "Markdown cells in Jupyter support [MathJax](https://www.mathjax.org/), so you can write lovely typeset mathematics, e.g., $ e^{i\\pi} = -1$.\n", "\n", "$$\n", " e^x \\equiv \\sum_{j=1}^\\infty \\frac{x^j}{j!}.\n", "$$\n", "\n", "Here is a [demo of Markdown and MathJax in Jupyter](https://youtu.be/-F4WS8o-G2A)\n", "\n", "This particular Jupyter notebook is an R notebook (there are also Python and Julia notebooks, among programming languages).\n", "This notebook is communicating with an R kernel and can execute R commands.\n", "R is a high-level statistics package with a versatile programming language.\n", "\n", "Here is [an introduction to R](http://cran.r-project.org/doc/manuals/R-intro.pdf). \n", "\n", "For a more thorough introduction to R, see http://statistics.berkeley.edu/computing/r-bootcamp \n", "https://github.com/berkeley-scf/r-bootcamp-2014/blob/master/schedule/schedule.pdf\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Jupyter\n", "\n", "Most of Jupyter's functionality is clear from its drop-down menus: commands to insert or delete cells, to execute cells, to clear output, etc.\n", "\n", "One of the most useful features of Jupyter is its help functions and tab completion.\n", "\n", "For instance, typing \"tab\" while you are typing the name of a function will give you a list of functions that start with the letters you have typed so far.\n", "\n", "Please click \"help\" and take the User Interface Tour.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## R\n", "\n", "The rest of this notebook is a brief introduction to R (within Jupyter). \n", "We will see more of R in later sections of the course, as we encounter particular topics linear algebra, least squares, optimization, random number generation, the Bootstrap, etc." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Hello world!\"\n" ] } ], "source": [ "# This is a code cell (but this line is a comment, because it starts with '#')\n", "# This is an R notebook, so you can type R commands into this cell, for example:\n", "print('Hello world!')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "7" ], "text/latex": [ "7" ], "text/markdown": [ "7" ], "text/plain": [ "[1] 7" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "25" ], "text/latex": [ "25" ], "text/markdown": [ "25" ], "text/plain": [ "[1] 25" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "2.5" ], "text/latex": [ "2.5" ], "text/markdown": [ "2.5" ], "text/plain": [ "[1] 2.5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "2.5" ], "text/latex": [ "2.5" ], "text/markdown": [ "2.5" ], "text/plain": [ "[1] 2.5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "Warning message:\n", "In sqrt(-1): NaNs produced" ] }, { "data": { "text/html": [ "NaN" ], "text/latex": [ "NaN" ], "text/markdown": [ "NaN" ], "text/plain": [ "[1] NaN" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "[1] 0+1i" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# more R\n", "\n", "# arithmetic\n", "5+2 \n", "5^2\n", "5/2\n", "5/2; # lines ending with semicolon don't produce printed output\n", "\n", "sqrt(-1) # no answer among the real numbers\n", "sqrt(-1 + 0i) # R understands complex numbers, though" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "5" ], "text/latex": [ "5" ], "text/markdown": [ "5" ], "text/plain": [ "[1] 5" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "6" ], "text/latex": [ "6" ], "text/markdown": [ "6" ], "text/plain": [ "[1] 6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "25" ], "text/latex": [ "25" ], "text/markdown": [ "25" ], "text/plain": [ "[1] 25" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "2.23606797749979" ], "text/latex": [ "2.23606797749979" ], "text/markdown": [ "2.23606797749979" ], "text/plain": [ "[1] 2.236068" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# variables\n", "x <- 5; # assignment uses an right arrow, <-\n", "x\n", "6 -> y; # can assign in the other direction, too\n", "y\n", "x^2\n", "sqrt(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "3.14159265358979" ], "text/latex": [ "3.14159265358979" ], "text/markdown": [ "3.14159265358979" ], "text/plain": [ "[1] 3.141593" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# R has some pre-defined values\n", "pi" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1] NA" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "FALSE" ], "text/latex": [ "FALSE" ], "text/markdown": [ "FALSE" ], "text/plain": [ "[1] FALSE" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# R represents \"not a number\" by \"NA\" (it also represents missing values by \"NA,\" so be careful)\n", "x <- NA;\n", "x + 1 # arithmetic with \"NA\" gives \"NA\"\n", "\n", "is.na(x) # check whether x is not a number\n", "is.na(pi)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
x | y |
---|---|
1 | 6 |
2 | 7 |
3 | 8 |
4 | 9 |
5 | 10 |
x | y | |
---|---|---|
1 | 6 | 1 |
2 | 7 | 4 |
3 | 8 | 9 |
4 | 9 | 16 |
5 | 10 | 25 |
var1 | time | val | |
---|---|---|---|
1 | 1 | 3 | 0.9480642 |
2 | 2 | 4 | 0.7340699 |
3 | 3 | 5 | 0.9362691 |
4 | 4 | 6 | 0.7923703 |
5 | 5 | 7 | 0.1712906 |
var1 | time | val | |
---|---|---|---|
1 | 1 | 3 | 0.9480642 |
2 | 2 | 4 | 0.7340699 |
3 | 3 | 5 | 0.9362691 |
4 | 4 | 6 | 0.7923703 |
5 | 5 | 7 | 0.1712906 |
6 | 6 | 7 | 8 |
7 | 1.791759 | 1.94591 | 2.079442 |
1 | 5 | 9 | 13 | 17 |
2 | 6 | 10 | 14 | 18 |
3 | 7 | 11 | 15 | 19 |
4 | 8 | 12 | 16 | 20 |
1 | 6 | 11 | 16 |
2 | 7 | 12 | 17 |
3 | 8 | 13 | 18 |
4 | 9 | 14 | 19 |
5 | 10 | 15 | 20 |
1 | 5 | 9 | 13 | 17 |
2 | 6 | 10 | 14 | 18 |
3 | 7 | 11 | 15 | 19 |
4 | 8 | 12 | 16 | 20 |
1 | 1 | 1 | 1 |
2 | 2 | 2 | 2 |
3 | 3 | 3 | 3 |
4 | 4 | 4 | 4 |
5 | 5 | 5 | 5 |
1 | 5 | 4 | 3 | 2 |
2 | 1 | 5 | 4 | 3 |
3 | 2 | 1 | 5 | 4 |
4 | 3 | 2 | 1 | 5 |
c {base} | R Documentation |
This is a generic function which combines its arguments.\n", "
\n", "The default method combines its arguments to form a vector.\n", "All arguments are coerced to a common type which is the type\n", "of the returned value, and all attributes except names are removed.\n", "
\n", "\n", "\n", "\n", "c(..., recursive = FALSE)\n", "\n", "\n", "\n", "
... | \n",
"\n",
" objects to be concatenated. \n", " |
recursive | \n",
"\n",
" logical. If |
The output type is determined from the highest type of the components\n",
"in the hierarchy NULL < raw < logical < integer < double < complex < character\n",
"< list < expression. Pairlists are treated as lists, but non-vector\n",
"components (such names and calls) are treated as one-element lists\n",
"which cannot be unlisted even if recursive = TRUE
.\n",
"
c
is sometimes used for its side effect of removing attributes\n",
"except names, for example to turn an array into a vector.\n",
"as.vector
is a more intuitive way to do this, but also drops\n",
"names. Note too that methods other than the default are not required\n",
"to do this (and they will almost certainly preserve a class attribute).\n",
"
This is a primitive function.\n", "
\n", "\n", "\n", "NULL
or an expression or a vector of an appropriate mode.\n",
"(With no arguments the value is NULL
.)\n",
"
This function is S4 generic, but with argument list\n",
"(x, ..., recursive = FALSE)
.\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", "unlist
and as.vector
to produce\n",
"attribute-free vectors.\n",
"
\n", "c(1,7:9)\n", "c(1:5, 10.5, \"next\")\n", "\n", "## uses with a single argument to drop attributes\n", "x <- 1:4\n", "names(x) <- letters[1:4]\n", "x\n", "c(x) # has names\n", "as.vector(x) # no names\n", "dim(x) <- c(2,2)\n", "x\n", "c(x)\n", "as.vector(x)\n", "\n", "## append to a list:\n", "ll <- list(A = 1, c = \"C\")\n", "## do *not* use\n", "c(ll, d = 1:3) # which is == c(ll, as.list(c(d = 1:3))\n", "## but rather\n", "c(ll, d = list(1:3)) # c() combining two lists\n", "\n", "c(list(A = c(B = 1)), recursive = TRUE)\n", "\n", "c(options(), recursive = TRUE)\n", "c(list(A = c(B = 1, C = 2), B = c(E = 7)), recursive = TRUE)\n", "\n", "\n", "