###################################################################### # R commands Stat 704 # Lab 1 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".) ###################################################################### ####### install a package in R install.packages("UsingR") library(UsingR) ####### R as a calculator 2+3 2^3 2+3-2^3 (2+3-2^3)*4 (2+3-2^3)/4 2+3^4 1:4 2+(1:4)^4 log(1:4) log2(1:4) log(1:4, base=3) sin(0.5) 7%% 3 ## answer 1, modulo reduction abs(-3) exp(1) sqrt(3) x<-3 x x<-4 x ############################## # Creating simple vectors ############################## x<-3 x <- c(1,3.5,-28.4,10) #numerical vector x y<-c("cat","dog","mouse","monkey") #character z<-c(TRUE,TRUE,TRUE,FALSE,FALSE) #logical vector x<-1:10 seq(1,10) seq(3, 9, by=3) rep(2, 10) rep(c(1,2,3),each=5) log(seq(1,2, by=0.1)) x<-c(1, 5, 10, NA, 15) sum(x) sum(x, na.rm=T) prod(x, na.rm=T) mean(x, na.rm=T) x<-1:10 cumsum(x) ## running sum cummax(x) ## running maximum cummin(x) ## running minimum ###################### ################################### # Accessing Elements in a Vector ################################### y <- c(18,1, 2,-7,12,19) x<-c(1, 2, 3) x<-1:3 length(y) y[3:5] ##position in vector as positive integer y[-c(1,5,6)] ## exclude: use negative integers y<15 y[y<15] which(y==32) x<-10:1 match(y,x) x<-c(1, 4, 6) y[x] colors<-c("red", "blue", "pink") which(colors=="yellow") x<-c(1,5,10, NA, 15) which(is.na(x)) which(!is.na(x)) x<-runif(10) x[4]<-NA rep(1, 10) rep(c(1,2,3), 10) rep(c(1,2,3), c(5,6,7)) ############################## ## &, | ############################## x<-c(T, T, F, F) y<-c(T, F, T, F) mat<-cbind(x,y) mat and<- x & y or<-x | y and or x<-runif(10) x[4]<-NA wh<-which(!is.na(x) & x<0.5) x[wh] x[!is.na(x) & x<0.5] ############################## # Factors # vector with categories ############################## scale<-c(1,1,2,3) likerscale<-factor(scale, label=c("agree", "neutral", "disagree")) table(likerscale) ############################## # Matrices ############################## help(cbind) y<-c(8,32, 15, -7, 2, 19) x<-1:6 mat<-cbind(x,y) mat<-rbind(x, y) help(rbind) dim(mat) ## check dimension ncol(mat) ## the number of columns of a matrix nrow(mat) ## the number of rows of a matrix mat[2,3] # the value in the 2nd row and the 3rd column mat[1:3,] ## the first three row of mat mat[,2] ## the 2nd column of mat mat[-1,] ## exclude the first row newmat<-matrix(1:9, nrow=3) ## create new matrix newmat rowMeans(newmat) colMeans(newmat) m<-matrix(1:9, nrow=3, byrow=T) ## fill row first colnames(m)<-c("a", "b", "c") rownames(m)<-c("r1", "r2", "r3") vect<-as.vector(newmat) ############################## # Matrices Multiplication ############################## mat<-matrix(1:9, nrow=3) mat^2 mat%*%mat ############################## # Arrays ############################## myarray<-array(1:64, dim=c(4,4,4)) myarray myarray[1,2,3] ############################## # Data Frames ############################## muscle <- rnorm(n=10,mean=3,sd=1) sex <- factor(rep(c("M","F"),c(6,4))) speed <- rep(0,10) speed[1:6] <- rnorm(6,30-2*muscle[1:6],2) speed[7:10] <- rnorm(4,40-2*muscle[7:10],2) mydata <- data.frame(y=speed,x1=muscle,x2=sex) summary(mydata) str(mydata) temp <- lm(y~x1+x2,data=mydata) summary(temp) ############################## # Lists ############################## x <- list(one=c(18:36),two=c("AK","AL","AZ"),three=c(T,T,F,T),four=matrix(1:12,3,4)) x x[[1]][3:6] x$one[3:6] y<-unlist(x) str(y) ############################## # For loops ############################## for(i in ){ } for (i in 1:10){ d<-Sys.time() print(paste("Now is", d, sep=" ")) print(i*i) Sys.sleep(2) } ################################################## # sample from a vector ################################################## x<-1:10 samp<-sample(x) samp2<-sample(x, 5) samp samp2 dna<-sample(c("A", "C", "G", "T"), 100, replace=T) dna seq<-paste(dna, collapse="") seq seq2<-paste(dna[2:4], collapse="") seq2 ################################################## # writing a function ################################################## ##f(x,y)= x+y adding<-function(x, y){ z<-x+y return(z) } mysquare<-function(x){ ans<-x^2 return(ans) } x<-seq(-3,3, by=0.1) y<-rep(NA, length=length(x)) for(i in 1:length(x)){ y[i]<-mysquare(x[i]) } plot(x, y, type="l", main="y=x^2", lwd=4) plot(x, y, type="b", lwd=4) sumsquare<-function(x, y){ ans<-x^2 + y^2 return(ans) } sumsquare(3,3) ############################## # Generate random number ############################## ## from uniform distribution x<-runif(100) hist(x) ## from normal distribution y<-rnorm(100) plot(density(y)) ################## # End of lab1.R ##################