###################################################################### # R commands Stat 704 # Lab 2 University of South Carolina ###################################################################### # This file contains the R commands for the lab. # # Lines beginning with the symbol '#' are comments in R. All other # lines contain code. # # In R for Windows, you may wish to open this file from the menu bar # (File:Display file); you can then copy commands into the command # window. (Use the mouse to highlight one or more lines; then # right-click and select "Paste to console".) ###################################################################### ########################################################################## # Import data ############################################################################# ## Need to creat a dataset with missing values in excel and save it as test.csv (comma delimited) for this exercise getwd() setwd("/Users/hoyen/Desktop/STAT704/Data") dat<-read.csv("test.csv", header=T) str(dat) remove<-is.na(dat$total) remove<-which(is.na(dat$total)) newdata<-dat[-remove,] ######Alternative using !is.na keep<-which(!is.na(dat$total)) newdata<-dat[keep,] ############################## ## Export data ############################## write.table(newdata, file="test2.txt", row.names=F, quote=F, sep="\t") ###RData save(newdata, file="new.RData") load("new.RData") ############# FAMuss example getwd() ## change it using setwd() fmsURL<-"http://people.stat.sc.edu/hoyen/STAT704/Data/FMS_data.txt" fms<-read.delim(file=fmsURL, header=TRUE, sep="\t") colnames(fms) dim(fms) ## check the dimension of the data str(fms[,1:10]) ## check the structure of the data fms$id[1:10] fms[1,1:10] fms$pre.BMI fms$actn3_rs540874 index<-match(c("actn3_rs540874","pre.BMI"), colnames(fms)) dat<-fms[!is.na(fms$pre.BMI) & !is.na(fms$actn3_rs540874) , index] ## observations without NA attach(fms) mean(fms$pre.BMI, na.rm=T) ########################### dURL<-"http://itl.nist.gov/div898/strd/univ/data/PiDigits.dat" pidigits<-read.table(file=dURL, skip=60) table(pidigits) str(pidigits) prop<-table(pidigits)/5000 ##proportions prop barplot(prop, xlab="digit", ylab="proportion") abline(h=0.1, lty=2) abline(v=1, lty=3, lwd=4) ##scan x<-scan() str(x) ############################## # Export data ############################## newdata<-fms[,1:3] write.table(newdata, file="newdata.txt", sep="\t") save(newdata, file="newdata.RData") ############################## # Exercise 1 ############################## # 1. create a file using excel # 2. save it as csv # 3. read it into R using read.csv ############################## # Exercise 2 ############################## # 1. create a file using excel # 2. save it as txt (tab-delimited) # 3. read it into R using read.table ############################## ############################## # Generate Random Number ############################### ################################################## # generate 100 points from Normal(mu=0, sigma=2) ################################################## x<-rnorm(100, mean=0, sd=2) mean(x) hist(x) plot(density(x)) mean(x) quantile(x) ################################################## # generate 100 points from uniform distribution(0,1) ################################################## y<-runif(100) sum(y > 0.5) ################################################## # Toss a fair coin five times ################################################## h<-rbinom(1, 5, p=0.5) ################################################## # ordering ################################################## x<-runif(10) x order(x) x[order(x)]