{ "cells": [ { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading tidyverse: ggplot2\n", "Loading tidyverse: tibble\n", "Loading tidyverse: tidyr\n", "Loading tidyverse: readr\n", "Loading tidyverse: purrr\n", "Loading tidyverse: dplyr\n", "Conflicts with tidy packages ---------------------------------------------------\n", "filter(): dplyr, stats\n", "lag(): dplyr, stats\n" ] } ], "source": [ "library(tidyverse)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to R\n", "\n", "## R as simple calculator" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "245" ], "text/latex": [ "245" ], "text/markdown": [ "245" ], "text/plain": [ "[1] 245" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "2 + 3^5 " ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "2" ], "text/latex": [ "2" ], "text/markdown": [ "2" ], "text/plain": [ "[1] 2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "7 %/% 3" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "1" ], "text/latex": [ "1" ], "text/markdown": [ "1" ], "text/plain": [ "[1] 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sin(0.5*pi)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "1230" ], "text/latex": [ "1230" ], "text/markdown": [ "1230" ], "text/plain": [ "[1] 1230" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "1.23e3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Printing results\n", "\n", "By default, the result on the last line is displayed in the Jupyter notebook. To see other lines, use the `print` function." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 24\n", "[1] FALSE\n" ] }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(1 * 2 * 3 *4)\n", "print(11 %% 3 == 1)\n", "10 > 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting help\n", "\n", "Type something and hit the tab key - this will either complete the R command for you (if unique) or present a list of opitons.\n", "\n", "Use ?topic to get a description of `topic`, or more verbosely, you can also use `help(topic)`. " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "?seq" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use example to see usage examples." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "seq> seq(0, 1, length.out = 11)\n", " [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0\n", "\n", "seq> seq(stats::rnorm(20)) # effectively 'along'\n", " [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n", "\n", "seq> seq(1, 9, by = 2) # matches 'end'\n", "[1] 1 3 5 7 9\n", "\n", "seq> seq(1, 9, by = pi) # stays below 'end'\n", "[1] 1.000000 4.141593 7.283185\n", "\n", "seq> seq(1, 6, by = 3)\n", "[1] 1 4\n", "\n", "seq> seq(1.575, 5.125, by = 0.05)\n", " [1] 1.575 1.625 1.675 1.725 1.775 1.825 1.875 1.925 1.975 2.025 2.075 2.125\n", "[13] 2.175 2.225 2.275 2.325 2.375 2.425 2.475 2.525 2.575 2.625 2.675 2.725\n", "[25] 2.775 2.825 2.875 2.925 2.975 3.025 3.075 3.125 3.175 3.225 3.275 3.325\n", "[37] 3.375 3.425 3.475 3.525 3.575 3.625 3.675 3.725 3.775 3.825 3.875 3.925\n", "[49] 3.975 4.025 4.075 4.125 4.175 4.225 4.275 4.325 4.375 4.425 4.475 4.525\n", "[61] 4.575 4.625 4.675 4.725 4.775 4.825 4.875 4.925 4.975 5.025 5.075 5.125\n", "\n", "seq> seq(17) # same as 1:17, or even better seq_len(17)\n", " [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n" ] } ], "source": [ "example(seq)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To search for all functions with the phrase `topic`, use `apropos(topic)`." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "