Exercise 1 - UiO

[Pages:2]Exercise 1

1. Vectors:

Create the vectors (look at the help function for c, seq and rep): (a) (1, 2, 3, ..., 19, 20)

(b) (20, 19, . . . , 2, 1)

(c) (1, 2, 3, . . . , 19, 20, 19, 18, . . . , 2, 1)

(d) (4, 6, 3) and assign it to the name tmp.

(e) (4, 6, 3, 4, 6, 3, . . . , 4, 6, 3) where there are 10 occurrences of 4.

(f) (4, 6, 3, 4, 6, 3, . . . , 4, 6, 3, 4) where there are 11 occurrences of 4, 10 occurrences of 6 and 10 occurrences of 3.

(g) (4, 4, . . . , 4, 6, 6, . . . , 6, 3, 3, . . . , 3) where there are 10 occurrences of 4, 20 occurrences of 6 and 30 occurrences of 3. 2. Create a vector of the values of ex*cos(x) at x = 3, 3.1, 3.2,..., 6. Hint: use function exp and cos.

3. Execute the following lines which create two vectors of random integers which are chosen with replacement from the integers 1, 2, ..., 999. Both vectors have length 250.

xVec 600?

(c) What are the values in xVec which correspond to the values in yVec which are > 600? (By correspond, we mean at the same index positions.)

(d) Create the vector ( sqrt(|x1-meanX|), sqrt(|x2-meanX|), ..., sqrt(|xn-meanX|). Where meanX is the mean of xVec and | | is the absolut sign.

(e) How many values in yVec are within 200 of the maximum value of yVec? (f) How many numbers in xVec are divisible by 2? (Note that the modulo operator is denoted %%.)

(g) Sort the numbers in the vector xVec in the order of increasing values in yVec.

(h) Pick out the elements in yVec at index positions 1, 4, 7, 10, 13, ... 5. Matrix operations

(a) Make the matrix A equal to 1 2 4 4 3 5 3 4

(b) Give A columns name by using the function colnames() (c) Add 3 to each element in the matrix and call this matrix B (d) Add A and B. What do you get? (e) Attach the column [1,2] to your matrix A and call the new matrix D. Use the

command cbind() (f) Get the third column of D. Dataframe There are built-in data frames examples in R. One of them are called iris (a) What does iris look like? If you write head(iris) will you only see the first 6 rows. If you write summary(iris) you will get an overview of iris. (b) How big is the data frame? Use the function dim() or the functions nrow() and ncol(). (c) What is the cell value from the first row, second column of mtcars? (d) Could you get the same value by using row and column names instead? Which names? (e) Make some plot of the dataset iris.

? Is the values in iris$Sepal.Width normally distributed? ? Is there any systematic correlation between some of the variables in iris, find

both the correlation and make plots, plots with colors might be more illustrative and tell more? ? Make a boxplot of the different variables in iris. Are there any values that can be considered as outliers?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download