Class Exercise

advertisement
CSSS 567
Introduction to R Exercises
1. What is (1198/2) - 63 + 27
2.
Type the following into R
xx <- c(29,3,6,11,0,41,101)
a. what command will print out the 4th element of xx?
b. what is the length of x?
c. print out the 4th and 7th element of xx
d. create a new vector whose values are the square of the values of xx
3. Set the vector "xvals" equal to the numbers from -2.5 to +2.5 in increments of .02. How
many elements are in this vector?
4. Type in the following command into R
mat <- matrix(seq(21,71,10),nrow=3)
a. print out the value in the 2nd row, 2nd column
b. print out the last row
c. print out the first column
5. Look up the sample() function and use it to create a sample of size 100 of the first 20
integers.
a. How many 1's are in this sample?
b. How many sample values are less than or equal to 10?
6. Type in the following command to R
a.list <- list(a=25,b="what?",c=matrix(1:12,nrow=4))
a. print out the value in the 2nd row, 2nd column of the matrix in this list
b. create a vector of the names of the members of your family
c. add this vector to a.list using the c() function
1
Autumn 2006
CSSS 567
7. Create the following list
> knit.basket
$yarn
[1] "wool"
$num.skeins
[1] 3
$colors
[1] "red"
"green" "blue"
$needle.sizes
[1] 15 18 19 20
8. Go to http://csde.washington.edu/training/courses/Rintro2006/ an d click on “example2.dat”
to look at it. Use the read.table() function to read this file into your R workspace either by
copying this file to your local folder (file="example2.dat") or without copying by using
the full path name for the file location:
(file =
"http://csde.washington.edu/training/courses/Rintro2006/example2.dat")
a. How many variables are in the data?
b. How many cases are in the data?
c. Make a histograms of “yrs” and “happy” and save them to a word document.
d. plot yrs (years of education) on the x-axis versus happy (happiness) on the y-axis
e. fit a linear regression model to the data
f. add the linear regression line to the raw data plot
9. Type the following commands into R
> x <- sample(x=0:30,size=100,replace=TRUE)
> y <- 5 + 2 * x + rnorm(100,mean=0,sd=5)
a. Make a histogram of x
b. Make a histogram of y
c. Make a scatterplot of x versus y
d. Make a scatterplot of x verus y with a blue fitted regression line on the plot
e. Add a title and axis labels to this plot
2
Autumn 2006
Download