1.

Create a vector called x that consists of the integers 1 through 12, inclusive. Do the following:

a. Print x
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12
b. Add the vector consisting of the integers 13 through 20 to x
c. Print x
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20


2.

The text file contains four variables for each United States President: the last name, the year his term started, the year his team ended, and his party affiliation. The data start on line one of the file, each variable is separated by a space, and there is a hard-return after each observation. There is a missing value in the last row expressed by “.”.

Download the file pres.txt, read the file by using read.table(), specify the column names and missing value, and print first 6 rows out as follows.

Hints:

  • col.names: specify column names
  • sep: specify the delimiter, default: white space
  • na.strings - how missing values are recorded
  • head() - show the first n observations, default: first 6 rows
>         name startyear endyear party
> 1 Washington      1789    1797    NP
> 2      Adams      1797    1801     F
> 3  Jefferson      1801    1809    DR
> 4    Madison      1809    1817    DR
> 5     Monroe      1817    1825    DR
> 6      Adams      1825    1829    DR


3.

This file contains different information for the United States Presidents who served 1961-2009. The first nine lines of text give basic information about the format of the variables as they are given in the file.

Download the file pres2.txt, read the file by using read.fwf(), specify the column names, skip first 9 rows, and print first 6 rows out as follows.

Hints:

  • col.names: specify column names
  • skip: number of lines to skip before reading
  • widths - width of characters of each column, use 17, 8, 8, 4 here.
  • head() - show the first n observations, default: first 6 rows
>                name    start      end days
> 1 John F. Kennedy   01/20/61 11/22/63 1036
> 2 Lyndon B. Johnson 11/22/63 01/20/69 1886
> 3 Richard Nixon     01/20/69 08/09/74 2027
> 4 Gerald Ford       08/09/74 01/20/77  895
> 5 Jimmy Carter      01/20/77 01/20/81 1461
> 6 Ronald Regan      01/20/81 01/20/89 2922


4.

Create a matrix called X that looks like

\[ \begin{pmatrix} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \\ 16 & 17 & 18 & 19 & 20 \end{pmatrix} \]

Do the following:

a. Print X
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    2    3    4    5
> [2,]    6    7    8    9   10
> [3,]   11   12   13   14   15
> [4,]   16   17   18   19   20
b. Change the value of 8 to 100. Print X.
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    2    3    4    5
> [2,]    6    7  100    9   10
> [3,]   11   12   13   14   15
> [4,]   16   17   18   19   20
c. Change the first row to all 1’s. Print X.
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    1    1    1    1
> [2,]    6    7  100    9   10
> [3,]   11   12   13   14   15
> [4,]   16   17   18   19   20
d. Change the fourth column to 19, 14, 9, 4. Print X.
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    1    1   19    1
> [2,]    6    7  100   14   10
> [3,]   11   12   13    9   15
> [4,]   16   17   18    4   20


5.

Use the built-in dataset cars (You don’t have to specify the package, just use data(cars).) to do the following.

a. Find the dimension, minimum, and maximum
> [1] 50  2
> [1] 2
> [1] 120
b. Assign the first column to the workspace variable s. Print s, and find its dimension, minimum, and maximum.
> NULL
> [1] 4
> [1] 25
c. Assign the second column to the workspace variable d. Print d, and find its dimension, minimum, and maximum.
> NULL
> [1] 2
> [1] 120


6.

Create the following list and show it just as I have.

> $cylinder
> [1] 4
> 
> $mpg
> [1] 22.8 24.4 33.9
> 
> $car.brand
> [1] "Datsun"   "Mercedes" "Toyota"  
> 
> $is.German
> [1] FALSE  TRUE FALSE