Command Patterns in R

Week 3

Command chains

Your commands will be written as chains.

An example command chain

Princes <- 
  BabyNames %>%
  filter(grepl("Prince",name)) %>%
  group_by(year) %>%
  summarise(total = sum(count))

Syntax and semantics

There are two distinct aspects involved in reading or writing a command chain.

  1. Syntax: the grammar of the command
  2. Semantics: the meaning of the command

The focus today is on syntax.

Parts of Speech

From the dictionarty

part of speech noun

plural noun: parts of speech a category to which a word is assigned in accordance with its syntactic functions. In English the main parts of speech are noun, pronoun, adjective, determiner, verb, adverb, preposition, conjunction, and interjection.

Parts of Speech in R

  1. Data tables
  2. Functions
  3. Arguments
  4. Variables
  5. Constants
  6. Assignment
  7. Formulas (mostly left to future statistics classes)

Data tables

Functions

Arguments

The things that go inside a function’s parentheses are called arguments.

summarise(total = sum(count))

You can also consider the data table passed along by %>% as an argument to a function that immediately follows.

Variables

Variables are the components of data tables.

Constants

Constants are single values, most commonly a number or a character string.

Discussion Problem

Consider this command chain:

Princes <- 
  BabyNames %>%
  filter(grepl("Prince",name)) %>%
  group_by(year) %>%
  summarise(total = sum(count))

Just from the syntax, you should be able to tell which of the five different kinds of object each of these things is: Princes, BabyNames, filter, grepl, "Prince", name, group_by, year, summarise, total, sum, count.

Explain your reasoning.

R Markdown

R Markdown

Creating an Rmd File

Use the “DCF Work” or “DataComputing simple” template file for Rmd:

The good people at RStudio have developed a number of “Cheat Sheets” to get people off and running with these tools. Here’s a link to several of them, including RMarkdown, RStudio, and other topics we’ll hit in this course.

In-Class Assignment:

Create an narrative description of at least 3 classes you are taking this term using RMarkdown. Include:

Use the Rmd template above (i.e. adapt stat184Template.Rmd or start fresh with the “DCF Work” or “DataComputing simple” template). Feel free to work together and help each other, but each student should submit their own work as an html file with embedded .Rmd on Canvas.

Note: narrative should be written with text connecting each portion, don’t just dump all the required elements into a document together.

Help each other, divide and conquor, share .Rmd code, post tips/questions/answers to Piazza!

Homework: