{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises: Session 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "suppressPackageStartupMessages(library(tidyverse))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1**. We will work with the `Puromycin` data set in this exercise.\n", "\n", "1. Use `help` to find out more about of the `Puromycin` data set\n", "2. Use `class` to find out the class of the data set\n", "3. How many rows and columns are there?\n", "4. What is the type of each column?\n", "5. Show all unique values for the `state` column\n", "6. Show the first 5 rows\n", "7. Show the last 5 rows" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 2\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#4\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#4\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#5\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#6\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#7\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2**. Using the `Puromycin` data set,\n", "\n", "1. Show the first 20 rows using **piping**\n", "2. Show the last 10 rows using **piping**\n", "3. Show rows 11 to 20 using **piping**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3**. Using the `Puromycin` data set,togehter with **piping** and **filter**\n", "\n", "1. Show only rows where the `state` is `untreated`\n", "2. Show only rows where the `conc` is 0.11\n", "3. Show only rows where the `conc` is less than 0.1\n", "4. Show only rows where the `state` is `treated` and the rate is more than 100\n", "5. Show only rows where the `conc` is less than 0.1 or the rate is more than 200" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#4\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#5\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4**. Using the `Puromycin` data set, together with **piping**, **head** and **select**, **select_if** and **select_all**\n", "\n", "1. Show only the `conc` and `rate` columns\n", "2. Show only the columns whose type is numeric\n", "3. Show only the columns whose names end with the letter `e`\n", "4. Convert all column names to UPPERCASE\n", "5. Rearrange the columns in the order `state`, `conc`, `rate`\n", "6. Drop the `state` column\n", "\n", "Limit to only the first 3 rows in each case." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#4\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#5\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#6\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**5**. Using the `Puromycin` data set, together with **mutate** or **transmutate** and any other operation necessary\n", "\n", "1. Create a new column `rate2` that is the square of rate\n", "2. Create a new data frame that only has the 3 columns with `conc`, `conc^2` and `conc^3` values. Name them `conc`, `conc2` and `conc3`\n", "3. Replace each value of all numeric columns with the square root of the value\n", "\n", "Show only the first 5 rows in each case" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**6**. Using the `Puromycin` data set, together with **arrange** and any other operation necessary\n", "\n", "1. Sort in ascending `rate` order\n", "2. Sort in descending `rate` order\n", "3. Sort first on `conc` i ascending order, then `rate` in ascending order\n", "4. Sort in ascending order of the number of characters in the `state` column\n", "\n", "In each case show only the first 5 rows." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#4 \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**7**. Using the `Puromycin` data set, together with **summarize** and any other operation necessary\n", "\n", "- Find the mean value of numeric columns\n", "- Find the mean length of the `state` column\n", "- Find the min, median and max of the `rate` column" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**8**. Using the `Puromycin` data set, together with **group_by** and any other operation necessary\n", "\n", "1. Find the average rate for each `state`\n", "2. Find the number of treated and untreated states in a new column `count`\n", "3. Find the number of rows with the same `conc` and `state` in a new column `count` and only show rows where the count is an even number.\n", "4. Find the mean and standard deviation of rate for each `state` and `conc`. Remove any rows with an NA value for the rate standard deviation.\n", "\n", "Hint: `group_by` is often combined with `summarize`, and `n()` returns the count." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#2\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#4\n" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.4.0" } }, "nbformat": 4, "nbformat_minor": 2 }