#Kevin O'Brien - University of Georgia - Psychology Department - Vision Sciences Laboratory - R Workshop Number 2 #This script is intended to accompany a set of lecture slides and requires an R script (AllCorrelations.R) to run properly #This script is best run in RStudio #Slide 2 ?mean() #Slide 3 print("This demonstrates the print command"); #Slide 4 numerals<-c(1,2,3); names<-c("one","two","three"); capitalNames<-c('ONE','TWO','THREE'); numbers<-data.frame(numerals, names, capitalNames); names(numbers)<-c('Numerals','LowerCase','UpperCase'); #Slide 6 isTRUE(1!=1); #Slide 7 #left column 1==1; #True 1==0; #False 1!=0; #True 'a'=='a'; #True 1<0; #False 1>0; #True 1<=1; #True 1>=1; #True #right column (TRUE & TRUE); (TRUE & FALSE); (TRUE | FALSE); (TRUE | TRUE); (FALSE | FALSE); (!FALSE); (!TRUE); #Slide 8 x<-10; if(x==10){ print("x is indeed ten"); } #Slide 9 a<-5; if(a==5){ print("Yep. It's a five.") }else{ print("Your contrived demonstration did not satisfy the if() condition"); } #Slide 11 EvenOrOdd<-function(inputValue){ if(inputValue%%2 == 0){ print(paste(inputValue,"is even.",sep=" ")); }else{ print(paste(inputValue,"is odd.", sep=" ")); } } EvenOrOdd(5); EvenOrOdd(6); #Slide 12 p<-0; p<-ifelse(p=="not_potato","potato","not_potato"); z<-0; if(z==0){ print("Z's zero, so do this thing."); }else if(z > 0){ print("Z's bigger than zero, so do this thing."); }else if(z < 0){ print("Z's smaller than zero, so do this thing."); }else{ print("Wait, if it's not zero, and not larger or smaller..."); print("WHY DID WE EVEN WRITE THIS PART?"); } #Slide 16 print(1); print(2); print(3); print(4); print(5); for(i in seq(1:5)){ print(i); } for(i in seq(1:100)){ print(i); } for(i in seq(from=0, to=1000, by=100)){ print(i); } #Slide 17 subNum<-seq(1:1000); subNum[473]<-4730; for(i in 1:length(subNum)){ if(subNum[i]>1000 | subNum[i]<0){ subNum[i]=NA; print(paste(i,"had an error!",sep=" ")); } } #Slide 18 set<-data.frame(); currentRow = 1; for(i in 1:10){ for(j in 1:10){ for(k in 1:10){ set[currentRow,1]<-currentRow; set[currentRow,2]<-i; set[currentRow,3]<-j; set[currentRow,4]<-k; set[currentRow,5]<-rnorm(1,mean=100,sd=15); set[currentRow,6]<-rnorm(1,mean=100,sd=15); set[currentRow,7]<-rnorm(1,mean=100,sd=15); set[currentRow,8]<-rnorm(1,mean=100,sd=15); set[currentRow,9]<-rnorm(1,mean=100,sd=15); currentRow=currentRow+1; } } } names(set)<-c("SubjectNo","Cond1","Cond2","Cond3","IQ1","IQ2","IQ3","IQ4","IQ5"); #Slide 19 for(i in 1:nrow(set)){ set[i,10]<-sum(set[i,5:9])/5; if(set[i,10]>105){ set[i,11]="HIGH"; }else if(set[i,10]<95){ set[i,11]="LOW"; }else{ set[i,11]="AVG"; } } names(set)[10:11]<-c("Mean","Group"); #Slide 20 demoVariable<-'q'; switch(demoVariable, a="Got a", b="Got b", c="Got c", "Got something else."); #Slide 21 someNumbers<-data.frame(rnorm(1000,0,1),rnorm(1000,6,2),rnorm(1000,12,3.6)); names(someNumbers)<-c("Group1", "Group2", "Group3"); sapply(someNumbers, summary); lapply(someNumbers, summary); #Slide 22 seq(from=0, to=1000, by=20); rep(1,50); rep(seq(1,5),20); #Slide 23 #Requires slide 21 code attach(someNumbers); output<-t.test(Group1, Group2); names(output); tVal<-output[[1]]; tValue<-as.numeric(output[1]); #Slide 24 source(file=file.choose(new = FALSE)); corOut<-all.correlations(someNumbers); #Slide 25 library(tcltk); setwd(tk_choose.dir(default = "", caption = "Select directory")); getwd();