# R programs consist of creating or reading in a dataset # R is also case-sensitive # 1st way to read in data using the c() command name=c("Hamb","Chsbrg","Delxbg","Fish","Chix") size=c(107,121,216,156,223) totfat=c(9,13,31,25,20) burgers=data.frame(name,size,totfat) # look at it by either calling the object (dataset) or use View() burgers View(burgers) # 2nd way using read.csv() or read.table() burgers=read.table("S:/webpages/~renaes/Data/burgs1.txt",sep='',header=T) burgers=read.csv("S:/webpages/~renaes/Data/burgs1.csv",header=T) # In RStudio, you can use the Import Dataset option in the Environment window # (which cannot really be shown here) # this dataset was a bit picky and I had to change the variable types # it coded most of the numeric variables as character and I changed them back to integer students=read.csv("S:/webpages/~renaes/Data/STUDENT.csv",header=T) View(students) with(students,mean(Sys1,na.rm=T)) #na.rm=T removes missing values for calculation with(students,mean(Dias1,na.rm=T)) with(students,plot(Dias1,Sys1)) # table with counts with(students,table(Eye)) # Contingency table with cross-calssifying factors with(students,xtabs(~Eye+Thumb)) # sorting data # sorted by eye color (Eye) # sorting is ascending by default students2=students[order(students$Eye),] # the comma means all columns # sort by eye color (ascending) and Thumb (descending) (for thumb descending use a - sign) students3=students[order(students$Eye,-students$Thumb),] # means of Sys1, Dias1 by eye color with(students,tapply(Sys1,Eye,mean,na.rm=T)) with(students,tapply(Dias1,Eye,mean,na.rm=T)) # we would have been able to do both variables at once if there were no missing values # example that won't work here but will if dataset had no missing values with(students,tapply(c(Sys1,Dias1),Eye,mean,na.rm=T))