Data and Computing Fundamentals

Exercises on R Syntax

Chidinma Emenike

Tue Feb 26 23:57:45 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"

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?

x = 1 + 2
x
## [1] 3
1 + 2 = y
## Error: object 'y' not found
y
## Error: object 'y' not found

Strings and Names

Show that a string and a name are different.

a = "thisisfun"
thisisfun = 7
a
## [1] "thisisfun"
thisisfun
## [1] 7

Assign a string value to an object.

x = "This is fun"
x
## [1] "This is fun"

Numbers and Arithmetic

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

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

Try these mathematical operations and explain what the values mean:

1/0
## [1] Inf

Inf represents infinity, which is created when a non-zero number is divided by 0.

0/0
## [1] NaN

NaN means 'Not a Number', and is created when a equation has impossible values.

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

What's the difference between NaN and NA?
'NaN' is used exclusively for numerical operations, but NA is used for things besides numbers.


Functions and arguments

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

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

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

f = seq(1:5)
g = seq(2, 10, 2)

What kind of object is created by date()?

typeof(date())
## [1] "character"

Data Frames

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

utilities = fetchData("utilities.csv")
## Retrieving from http://www.mosaic-web.org/go/datasets/utilities.csv

How many rows are there?

nrow(utilities)
## [1] 99

What are the names of the variables?

names(utilities)
##  [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.

  1. Sweet
  2. Sour
  3. Savory

Vegetables

  1. Leafy
  2. Flower

Hint:

### Fruits

1. Sweet
    * Apples