##################################################################### # NAME: Chris Bilder # # DATE: 9-8-11 # # Purpose: Miscellaneous code # # # # NOTES: # ##################################################################### ##################################################################### # for() example #Create a 3x2 matrix of observed normal random variables set.seed(1929) set1<-matrix(data = rnorm(n = 6, mean = 0, sd = 1), nrow = 3, ncol = 2) set1 save.it<-numeric(3) #Initialize vector to save results into save.it for (i in 1:3) { save.it[i]<-mean(set1[i,]) } save.it ##################################################################### # apply() example #Apply a function by row (MARGIN = 1) apply(X = set1, MARGIN = 1, FUN = mean) #Apply a function by column (MARGIN = 2) apply(X = set1, MARGIN = 2, FUN = mean) ##################################################################### # setwd() example #Create a work directory and read in a data file setwd(dir = "C:\\chris\\unl\\Dropbox\\NEW\\workshop\\Gallup\\") gpa<-read.table(file = "gpa.txt", header = TRUE, sep = "") head(gpa)