Command Patterns in R

Danny Kaplan

CVC June 21, 2016

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.

Part of Speech

From the dictionarty

part of speech 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 frames
  2. Functions
  3. Arguments
  4. Variables
  5. Constants
  6. Assignment
  7. Formulas (we won’t use these until the end)

Data frames

Functions

Arguments

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

You can also consider the data frame passed along by %>% as an argument to the following function.

Variables

Variables are the components of data frames.

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.