CSC121 - R Console Monday March 12th --------------------------- > getwd() [1] "/Users/csc121/Desktop" > studentData = read.table("students.txt") > studentData V1 V2 V3 V4 V5 V6 V7 1 sn lname fname year gender gpa program 2 1 Campbell Jen 3 F 3.8 CS 3 2 Gries Paul 2 M 2.7 CS 4 3 Dolderman Dan 4 M 3.1 PSY > # Don't forget to put header=TRUE > 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 > View(studentData) > nrow(studentData) [1] 3 > ncol(studentData) [1] 7 > dim(studentData) [1] 3 7 > # We can change values in data frame > 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 > studentData[2] lname 1 Campbell 2 Gries 3 Dolderman > studentData[2,] sn lname fname year gender gpa program 2 2 Gries Paul 2 M 2.7 CS > studentData[2,]$gpa [1] 2.7 > studentData[2,]$gpa <- 3.0 > studentData sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS 2 2 Gries Paul 2 M 3.0 CS 3 3 Dolderman Dan 4 M 3.1 PSY > # Data frame elements can be changed in place > > # 'Querying' a data set to get a particular subset of data based on some condition > # We can use the subset() function to extract data with certain conditions > subset(studentData, gpa==3.8) sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS > subset(studentData, gpa>3.1) sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS > subset(studentData, gpa>=3.1) 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 > # Use '&' to combine conditions together > subset(studentData, gpa>=3.1 & year==4) sn lname fname year gender gpa program 3 3 Dolderman Dan 4 M 3.1 PSY > subset(studentData, gpa>=3.1 & year==6) [1] sn lname fname year gender gpa program <0 rows> (or 0-length row.names) > studentData sn lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS 2 2 Gries Paul 2 M 3.0 CS 3 3 Dolderman Dan 4 M 3.1 PSY > > # Column headers of the data frame are the 'names' of the data > names(studentData) [1] "sn" "lname" "fname" "year" "gender" "gpa" [7] "program" > names(studentData)[1] [1] "sn" > names(studentData)[1] <- "studentNum" > studentData studentNum lname fname year gender gpa program 1 1 Campbell Jen 3 F 3.8 CS 2 2 Gries Paul 2 M 3.0 CS 3 3 Dolderman Dan 4 M 3.1 PSY > subset(studentData, gpa>=3.1 & year==4) studentNum lname fname year gender gpa program 3 3 Dolderman Dan 4 M 3.1 PSY > > > > # Random Numbers > > runif(1,0,10) [1] 3.707812 > runif(2,0,10) [1] 7.332454 4.865090 > runif(1,0,10) [1] 1.531535 > runif(1,0,10) [1] 4.177907 > runif(1,0,10) [1] 3.042981 > runif(1,0,10) [1] 5.252659 > runif(1,0,10) [1] 6.576456 > runif(1,0,10) [1] 7.312626 > runif(1,0,10) [1] 5.850115 > > plot(runif(500))