## BERD R Short Course ## Session 1 Homework Key # 1. Use two ways to create a numeric sequence X with elements 1.1, 1.3, 1.5, 1.7, 1.9, 2.1 X=c(1.1, 1.3, 1.5, 1.7, 1.9, 2.1) X=seq(from=1.1, to=2.1, by=.2) #2. Use two ways to create a character vector Y with elements “a”, “b”, “c”, “a”, “b”, “c” Y=c("a", "b", "c", "a", "b", "c") Y=rep(c("a","b","c"),2) #3.Create a factor vector Z based on Y Z=factor(Y) #4. Compute sum of X according to the levels in Z tapply(X, Z, sum) #5.Create a data frame U whose first column is X and second column is Y U=data.frame(X,Y) #6.Create a list V whose first component is X and second component is Y V=list(X=X,Y=Y) #7. Compute the sum of the first component X in list V sum(V$X) sum(V[[1]]) #8. Use table() function to generate a frequency table of Y in list V table(V$Y) table(V[[2]]) #9. Close out R session and save .Rhistory and .RData #10. Rename .Rhistory to be Session1.Rhistory and Rename .RDatat to be Session1.RData #11. Open R and view Session1.Rhistory #12. Load Session1.RData #13. List of all the objects in this R workspace objects() #or ls() #14. Quit R