CSC121 - R Console Friday March 9th --------------------------- > # Max and min functions > max(c(4,3,1,7,2)) [1] 7 > min(c(4,3,1,7,2)) [1] 1 > > # DATA FRAMES > # Reading into a data frame from a text file online > heights_and_weights <- + read.table ("http://www.cs.utoronto.ca/~radford/csc121/data7", + header=TRUE) > heights_and_weights name height weight 1 Fred 62 144 2 Mary 60 131 3 Joe 71 182 > > View(heights_and_weights) > > # Checking NAs: use is.na() > is.na(NA) [1] TRUE > a <- c(NA, 4, NA, 6, NA, NA) > is.na(a) [1] TRUE FALSE TRUE FALSE TRUE TRUE > > # Let's read in our students data > getwd() [1] "/Users/csc121" > setwd("/Users/csc121/Desktop") > getwd() [1] "/Users/csc121/Desktop" > studentData <- read.table("students.txt", header=TRUE) Warning message: In read.table("students.txt", header = TRUE) : incomplete final line found by readTableHeader on 'students.txt' > # ^ Don't forget to add a new line to the end of text file with data > studentData <- read.table("students.txt", header=TRUE) > studentData sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS 2 2 Gries Paul 2 M 2.7 CS 3 3 Dolderman Dan 4 M 3.1 PSY > # Notice how column names and the data beneath them line up correctly > View(studentData) > studentData$fname [1] Jen Paul Dan Levels: Dan Jen Paul > # We'll talk about the 'Levels' next week > > # We can access different rows, columns, and specific elements of the data frame > studentData$gpa [1] 3.8 2.7 3.1 > studentData[1,] sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS > studentData[1:2,] sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS 2 2 Gries Paul 2 M 2.7 CS > studentData[c(1,3),] sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS 3 3 Dolderman Dan 4 M 3.1 PSY >