CSSS 508: Intro R



CSSS 508: Intro R

2/03/06

Homework 4 Solutions

These solutions are just one way you could write these functions. There will be lots of correct solutions. I will post clever/good/different ones for your perusal.

Coding style is personal, but I’ve tried to mix it up.

1) Write a function that takes in a vector of numbers and returns only the minimum and the maximum of the vector. Generate 10 random normal values (print them) and test your function on them.

########################################

##Function 1

##

##Arguments: vector of numbers

##Outputs: min and max of the vector

########################################

min.max add.odd(17)

[1] 81

If you’re fancy, you could write it to handle both positive and negative numbers.

add.odd0)odd.numbers add.odd(-6) (1 + -1 + -3 + -5)

[1] -8

3) Write a function that only takes in the dimensions of a matrix and a vector of n means. Generate a matrix of the given size where the first row is a random sample from a normal with the first mean and a standard deviation of 1, the second row is a random sample from a normal with the second mean and a standard deviation of 1, etc. (All standard deviations should be 1.) Find the median number in each row. Return both the matrix and the vector of n medians.

########################################

##Function 3

##

##Arguments: n: nrow of a matrix; p: ncol of a matrix; mean.vec: vector of n means

##Outputs: generated matrix; vector of medians

########################################

gen.norm.matrix ................
................

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

Google Online Preview   Download