##################################################################### # R commands STAT540 R Basic 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".) ###################################################################### #### Navigating and Managing the Filesystem ### working directory getwd() setwd("/Users/hoyen/Desktop/STAT540") ## ------------------------------------------------------ system.file(package = "dslabs") ## ------------------------------------------------------ dir <- system.file(package = "dslabs") list.files(dir) ## ------------------------------------------------------ dir <- system.file(package = "dslabs") file_path <- file.path(dir, "extdata/murders.csv") ## ------------------------------------------------------ file.copy(file_path, "murders.csv") ## ------------------------------------------------------ readLines("murders.csv", n = 3) ## ------------------------------------------------------ fn <- "calificaciones.csv" file.copy(file.path(system.file("extdata", package = "dslabs"), fn), fn) readLines(fn, n = 1) ## ------------------------------------------------------ dat <- read.csv("murders.csv") ## ------------------------------------------------------ x <- scan("murders.csv", sep = ",", what = "c") x[1:10] #Section 5.1 Creating data #Using rep rep(1,10) rep("Gamecocks Rule",1000) rep("While Gamecocks Rule, cockfighting itself is banned in SC",10) #Creating constant matrices and variables zeromat = matrix(0,5,3) onesvec.col = matrix(1,10,1) rep(1,10) #Converting a variable to a matrix bigvec = 1:200 mat.bycols = matrix(bigvec,100,2) mat.bycols mat.byrows = matrix(bigvec,100,2,byrow=T) mat.byrows #Section 5.2 #Reading tab-delimited files with and without headers #Reading files from working directory brainbod = read.table("brainbod2.txt") brainbod brainbod = read.table("brainbod1.txt", header=T) brainbod is.numeric(brainbod$brainwt) brainbod1 = read.table("brainbod1.txt") brainbod1 is.numeric(brainbod1$brainwt) brainbod = read.table("brainbod.txt", sep=',', header=T) brainbod # read.csv brainbod3 = read.csv("brainbod.csv") brainbod3 #Assigning row and column names names(brainbod)=c("Species", "Brain.Weight","Body.Weight") brainbod brainbod = read.table("brainbod2.txt",col.names=c("Species","Body Weight", "Brain Weight")) brainbod # pay attention that blank spaces in the names are changed to "." row.names(brainbod)=brainbod$Species brainbod # some special types of missing values brainbod = read.table("brainbod4.txt", na.strings="*") brainbod is.numeric(brainbod[,2]) is.character(brainbod[,2]) brainbod[,2]<- as.numeric(brainbod[,2]) brainbod ratio= brainbod[, 2]/brainbod[, 1]/1000 ratio=round(ratio, digit=6) cbind(brainbod, ratio) ## try attach and detach again brainbod$Species Species attach(brainbod) #brainbod=data.frame(Brain.Weight,Body.Weight,row.names=Species) Species rm(brainbod) ls() Species objects(2) search() detach(brainbod) # read data file from non-working directory brainbod = read.table("C:/WangFiles/Stat540_Fall2023/Rcodes/brainbod2.txt") brainbod is.factor(brainbod$V1) is.character(brainbod$V1) is.numeric(brainbod$V2) brainbod = read.table("C:/WangFiles/Stat540_Fall2023/Rcodes/brainbod2.txt") brainbod brainbod = read.table("C:/WangFiles/Stat540_Fall2023/Rcodes/brainbod2.txt", col.names=c("Species","Body Weight", "Brain Weight")) brainbod # read data from a website brainbod = read.table("http://people.stat.sc.edu/hitchcock/brainbod.txt") brainbod #Section 5.3 help(scan) brainbod = as.data.frame(scan("brainbod2.txt",what=list(Species="",Body.Weight=0, Brain.Weight=0))) brainbod my.dfr <- as.data.frame(scan("Garden.txt", what=list(name="", tomato=0, grapes=0, type=""), multi.line=T)) my.dfr # use scan to input numeric data using keyboard xz=scan() ##Chapter 6 #Outputting a matrix into a text file mymat=matrix(1:15,5,3) mymat colnames(mymat)<- c("x1", "x2","x3") rownames(mymat)<- c("row1", "row2", "row3", "row4", "row5") mymat write(mymat, "mymat.txt",ncol=ncol(mymat)) # check out what is in mymat.txt write(t(mymat),"mymat2.txt",ncol=ncol(mymat)) write(t(mymat),"mymat3.txt") # what if the column number is not specified? write(t(mymat),"mymat2.txt",ncol=ncol(mymat), append=T) write.table(mymat, "mymat4.txt") # save the matrix together with the row and column names # write to a designated file in the non-working directory write(mymat, "C:/WangFiles/Stat540_Fall2023/Rcodes/mymat.txt",ncol=ncol(mymat)) write(t(mymat),"C:/WangFiles/Stat540_Fall2023/Rcodes/mymat2.txt",ncol=ncol(mymat)) # save objects in a .rdata file help(save) save(mymat, brainbod, file="lecture1.rdata") # save mydata and brainbod into an r workspace named "lecture1.rdata" save.image(file="lecture.rdata") # save all objects in the current workspace into an r workspace "lecture.rdata" # write a function into a file myfct<-function(x){c(mean(x)-2*sd(x),mean(x)+2*sd(x) )} a=rnorm(20) myfct(a) dput help(dput) dput(myfct, "") # display the function on screen dput(myfct, "try1.txt") # write the function in a file called try1.txt fix(myfct) # open a editor window for myfct # print results to screen x<-101 print(paste("this output is",x)) print(paste("the output x is ", x, " and x+1 is ", x+1 )) a=23 b=11 xxx=paste("the score of today's game is ", a,":", b, ", Gamecocks win!!!" ) xxx print(paste("the score of today's game is ", a,":", b, ", Gamecocks win!!!" )) # print some results within a loop on screen for (i in 1:8){ x=runif(1) print(paste("iteration =", i, ", x[", i, "]=", round(x, digit=4)) ) }