# 1a mat<-matrix(data=c(3,5,7,4,3, 2,5,8,6,5, 7,3,5,3,4), ncol=5,nrow=3,byrow="TRUE") mat # 1b colnames(mat)<-c("one", "two", "three", "four", "five") rownames(mat)<-c("Alice", "Bill", "Sue") mat # 1c mean(mat[2,]) #or mean(mat["Bill",]) # 1d RowMeans<-apply(mat,MARGIN=1,FUN=mean) RowMeans mat<-cbind(mat,RowMeans) mat # 2a Sex<-rep(c("Male","Female"),each=4) Commsize<-c("Small","Large","Small","Large", "Large","Large","Small","Small") Altruism<-c(3,6,8,4,5,8,3,2) newdat<-data.frame(Sex,Commsize,Altruism) newdat # 2b setwd("C:/Users/cribbie/Dropbox/Courses/Intro to R/SCS_2016") write.csv(newdat,file="myfile.csv") # 2c dat1<-read.csv(file.choose()) dat1 # 2d dat2<-subset(dat1, Sex=="Male" & Commsize=="Large") mean(dat2$Altruism) #or mean(dat1$Altruism[Sex=="Male" & Commsize=="Large"]) # 2e median(dat1$Altruism[Sex=="Female"]) # 2f is.factor(dat1$Commsize) # 2g hist(dat1$Altruism) hist(dat1$Altruism,breaks=6) # 2h library(car) leveneTest(y=dat1$Altruism,group=dat1$Sex) # 2i dat1 dat1$Altruism[1]<-9 ## need xquartz if working with a mac dat2<-edit(dat1) dat2<-fix(dat1) #or dat2<-dat1 dat2$Altruism[1]<-9 dat2 # 3 sq<-function (x) { squ<-x*x return(squ)} sq(5) sq(x=5)