# R macros for Math 325, originally written spring 2005. source("http://www.math.jmu.edu/~garrenst/math300.dir/Rmacros") # Math 324 and 325, modified 6/14/05 scan2 <- function(file.name, matrix.format=FALSE, character.rm=FALSE, nrow=NULL, ncol=NULL, disk.drive=NULL, course.num=325) { # Scans in a matrix or vector. # `file.name' is the name of the data set to be scanned. # If `file.name' is the complete path, then `disk.drive' is ignored. # Path is assumed to be complete if `file.name' does not begin with # `example', `exercise', `problem' or `table'. # If `matrix.format' is TRUE, then data are attempted to be saved as a rectangular matrix. # If `matrix.format' is FALSE, then data are saved as a vector. # If `character.rm' is TRUE, then remove all character (i.e., non-numeric) data. # `nrow' is the desired number of rows for the matrix in the output. # If `nrow' is NULL, then `nrow' is determined by the function. # `ncol' is the desired number of columns for the matrix in the output. # If `ncol' is NULL, then `ncol' is determined by the function. # `disk.drive' is the disk drive where the data are located, is NULL for web-based data, # and is "linux" for a disk drive on a linux-based computer. # Some choices for `disk.drive' are NULL, "linux", "a", "b", "c", "d", "e", .... # `course.num' is the course number and may be numeric or character. # Missing values, denoted by `.', are assumed to be numeric. # If the first row of data contains a missing value, then `nrow' or `ncol' should be nonNULL. course.num <- as.numeric(course.num) if (is.null(disk.drive)) { full.name <- paste("http://www.math.jmu.edu/~garrenst/math", course.num, ".dir/datasets/", file.name, sep="") } else { if (disk.drive=="linux" & course.num==325) { full.name <- paste("/mnt/cdrom/SAS/DATASETS.SAS/", file.name, sep="") } if (disk.drive!="linux" & course.num==325) { full.name <- paste(disk.drive, ":/SAS/DATASETS.SAS/", file.name, sep="") } } data <- scan(full.name, what=character(), quiet=TRUE, comment.char="#") a.character <- FALSE ; options(warn = -1) if (course.num==324) { for (i in 1:length(data)) { if ( is.na(as.numeric(data[i])) ) {a.character <- TRUE} } } if (course.num==325) { for (i in 1:length(data)) { if ( is.na(as.numeric(data[i])) & data[i]!="." ) {a.character <- TRUE} if ( data[i]=="." ) data[i] <- NA } } # Missing data. options(warn = 0) if (a.character & character.rm) { options(warn = -1) ; data1 <- NULL for (i in 1:length(data)) { if ( !is.na(as.numeric(data[i])) || is.na(data[i]) ) data1 <- c(data1, data[i]) } options(warn = 0) ; data <- data1 } if (!a.character || character.rm) data <- as.numeric(data) if (matrix.format) { if (!is.null(nrow)) data <- matrix( data, nrow=nrow, byrow=TRUE ) if (!is.null(ncol)) data <- matrix( data, ncol=ncol, byrow=TRUE ) if (is.null(nrow) & is.null(ncol)) { ncol <- 0 ; line.num <- 0 while (ncol==0 & line.num<=1000) { line.num <- line.num+1 first.line <- scan(full.name, what=character(), nlines=line.num, quiet=TRUE, comment.char="#") if (!a.character || !character.rm) {ncol <- length(first.line)} else { options(warn = -1) for (i in 1:length(first.line)) { if ( !is.na(as.numeric(first.line[i])) || is.na(first.line[i]) ) {ncol <- ncol+1} } options(warn = 0) } } if (ncol!=0) data <- matrix( data, ncol=ncol, byrow=TRUE ) } } return(data) } # Garren, 2005 # Math 324 and 325 read.table2 <- function(file.name, course.num=325, na.strings=".", ...) { # Reads in a table. # `file.name' is the name of the data set to be scanned. # `course.num' is the course number and may be numeric or character. # `na.strings' is a vector of strings which are to be interpreted as `NA' # values. Blank fields are also considered to be missing values. # `...' are the optional arguments used in `read.table'. full.name <- paste("http://www.math.jmu.edu/~garrenst/math", as.numeric(course.num), ".dir/datasets/", file.name, sep="") return(read.table(full.name, na.strings=na.strings, ...)) } # Garren, 2005 # stratified.survey <- NULL; stratified.survey <- NULL; cluster.survey <- NULL; ratio.survey <- NULL