Data and Computing Fundamentals

Exercises on R Syntax

Asad Zaidi

Tue Feb 26 22:11:22 2013

Answer the following by constructing appropriate R commands and/or giving a text narrative.

Object Names

Show that names with different capitalization refer to different objects.

age = 21
Age = "The 21st century"
age
## [1] 21
Age
## [1] "The 21st century"

Age and age are different.


The name of the object being assigned to always goes to the left of the = operator. What happens if you put it on the right?

Strings and Names

Show that a string and a name are different.

A is a string. B is a name.


A = "Hello."
B = 21 * 16
A
## [1] "Hello."
B
## [1] 336

Assign a string value to an object.

M = "What time is it?"
M
## [1] "What time is it?"

Numbers and Arithmetic

Use scientific notation to create these numbers: 100, 10, 0.001, 0.001

100
## [1] 100
10
## [1] 10
0.01
## [1] 0.01
0.001
## [1] 0.001

Try these mathematical operations and explain what the values mean:

1/0
## [1] Inf

0/0
## [1] NaN

sqrt(-3)
## Warning: NaNs produced
## [1] NaN

What's the difference between NaN and NA?

NaN means Not a Number. NA is an error you get when there are missing expressions in a command.

Functions and arguments

Show that the order of unnamed arguments makes a difference. Use seq() to illustrate this.

seq(11, 1)
##  [1] 11 10  9  8  7  6  5  4  3  2  1

seq(1, 11)
##  [1]  1  2  3  4  5  6  7  8  9 10 11

Find the names of the arguments to seq(). Show that when you use the names, the order doesn't matter.

seq(from = 1, to = 11)
##  [1]  1  2  3  4  5  6  7  8  9 10 11
seq(to = 11, from = 1)
##  [1]  1  2  3  4  5  6  7  8  9 10 11

What kind of object is created by date()?

date() produces a timestamp as shown

date()
## [1] "Tue Feb 26 22:11:23 2013"

Data Frames

Read in the data set “utilities.csv” with fetchData("utilities.csv")

How many rows are there?

What are the names of the variables?

ut = fetchData("utilities.csv")
## Retrieving from http://www.mosaic-web.org/go/datasets/utilities.csv
nrow(ut)
## [1] 99
names(ut)
##  [1] "month"        "day"          "year"         "temp"        
##  [5] "kwh"          "ccf"          "thermsPerDay" "dur"         
##  [9] "totalbill"    "gasbill"      "elecbill"     "notes"

R Markdown Syntax

Write the Markdown to reproduce this outline. See below for a hint.

Fruits

  1. Sweet
  2. Sour
  3. Savory

Vegetables

  1. Leafy
  2. Flower

Hint:

### Fruits

1. Sweet
    * Apples