##################################################################### ##################################################################### ### ### Practice num. 1: Starting with R ### August 2009 ### ### R website: http://www.r-project.org/ ## R programme, optional packages and manuals can be downloaded from R website. ### Instructions: In this file, every line that starts with "#" is a ## comment. The rest of lines are R code to be executed in the R console. ### OPENING R: Double click in R icon and the R console appears. ## Copy line-by-line the R codes below into the R console, ## see the results and extract your own conclusions. ## You can add your own comments or explanations. ### 1. DOING SIMPLE CALCULATIONS AND ASIGNING VALUES TO VARIABLES 2*7 30/5 2*(3+4) sqrt(4) exp(1) n <- 15 # Asignment symbol "<-" n 5 -> x x ### 2. CHANGE OF DEFAULT WORKING DIRECTORY # -> File -> Change directory -> [Type or browse your Directory] # [Close R] -> Save workspace image? Yes # [Run R from your Directory] n x ### 3. CREATION, PRINT OUT AND DELETION OF OBJECTS IN MEMORY x <- 1 X <- 10 x;X x1.char <- "I am nice" x1.char; x n <- 10 + 2; n n <- (10+2)*5 n m <- n + x; m N <- 1.23e6 N ls() objects() M <- m; M rm(M); M rm(list=ls()) objects() name <- "Isabel" nam name n<-10 ls(pattern = "n", all = TRUE) ls.str() ### 4. HELP IN R ?ls help(ls) help("ls") ?survfit #Packages->Load package->survival->OK ?survfit # Task: Load the package nlme containing the function lme (we will need it) help.start() apropos("fit") ### 5. OBJECT ATTRIBUTES x<-1 mode(x) length(x) str(x) ?str logic.value <- TRUE mode(logic.value) length(logic.value ) str(logic.value ) # Task: Find out the mode and length of variable "name" ### 6. TREATMENT OF NAs AND INCONSISTENT CALCULATION RESULTS not.available <- NA ; not.available # Task: Find out the mode and length of variable "not.available" na.number <- c(1,2,NA,4) # Vector with elements 1, 2, NA and 4 na.number mode(na.number) length(na.number) str(na.number) na.fail(na.number) na.fail(x) ?na.fail na.omit(na.number) na.omit(x) is.vector(x) ?is.vector is.vector(na.number) a <- 256/0 a exp(a) exp(-a) a - a ### 7. OBJETS MANIPULATION ### 7.1. Vectors: Definition and basic operations x1 <- c(5, 1, 8 ,3) order(x1) sort(x1) x2 <- c(1,2,2,3,3,4) order(x2) x1+x2 x3 <- c(3,2,1) x2+x3 2*x3 x2*x3 # Wach out!! ### Indexation of elements of vectors x1[3] x2[2] x1[-4] x1[-4]+x2 x1 x1[c(1,2)] x1[-c(1,4)] x2 x2[x2==3] x2[x2<3] x2[x2==3] <-99; x2 x1[order(x1)] sort(x1) ### Initialization of vectors a<-vector(mode="numeric",length=6) str(a) a<-vector(mode="character",length=6) str(a) b<-NULL b[1]<-2 b[2]<-3 ### 7.2. Matrices and Arrays a1<-matrix(0,nr=5,nc=4);a1 a1 <-array(9,dim=c(5, 4)); a1 dim(a1) mode(a1) length(a1) attributes(a1) m3 <- matrix(1:20, nrow=5); m3 m3 <- matrix(1:20, nrow=5,byrow=TRUE); m3 # Task: Create a matrix called m4 with 2 rows and 3 columns, with elements c(2,1,4,5,2,3) filled by rows. z<-3 a2 <- matrix(c(x, z, name), nr=5,nc=4); a2 a2 <- matrix(c(x, z, name), nr=5,nc=4,byrow=TRUE); a2 mode(a2) a1[1,] a1[,2] a1[1,2] a1[1,2]<- NA; a1 a1[,4] <- NA; a1 m5 <- 0:19 m5 dim(m5) <- c(5, 4) m5 m5[1, ] m5[1, , drop=FALSE] m5[1, , drop=TRUE] # Task: Extract the second column of m5: first as a vector, secondly as a submatrix ### Basic operations with matrices cbind(m3,m5) rbind(m3,m5) m3 t(m3) m3*m5 m3m5t<-m3%*%t(m5) m3m5t diag(3) diag(m3) v <- c(10,20,30) diag(v) diag(57,nr=5,nc=7) Id3 <- diag(3) solve(Id3) det(Id3) eigen(Id3) ### 7.3. Factors f1 <- factor(c(1, 2, 3)); f1 ?factor f1[2] str(f1) is.vector(f1) f2 <- factor(c(1, 2, 3), labels = c("A", "B", "C")) f2 f4<-factor(c(1,1,3),levels=c(1,2,3),labels=c("A", "B", "C")) f4 # Task: Create a factor with elements 1,2,1,2,1,3,3, levels 1,2,3, and labels "lev1","lev2","lev3". ### Data.frames d1 <- data.frame(g1 = x2, g2 = z); d1 ?data.frame d1$g2 d1[,2] d1$g1[2] d1[,1] d1[1] d1$g3 <- x3; d1 names(d1) # Task: Create a new data.frame with first column equal to z a with name c1 # and second column equal to x2 and with name c1. # Then add a new column equal to x3 and with name c3, using function cbind ### 7.4. Lists L1 <- list(x1, x2, x3) ?list L1 L1[1] L1[[1]] L1[[1]][3] # Task: What type of object is the result of L1[1]? And of L1[[1]] names(L1) L2 <- list(A=x1, B=x2, C=x3) L2 # Task: Extract the first element of L2 as a vector, or as a list # Task: Extract the third element of vector A in the list L2 names(L2) L2$A L2$A[3] identical(L2$A, L2[1]) identical(L2$A, L2[[1]]) L3 <- list(ls()) L3 # Task: Create a list called L4 with elements x1,x2,a1,a2,d1,name) ### 7.5. Expressions (mathematical functions): Evaluation and derivatives e1 <- expression(x3 / (x2 + exp(z))) e1 eval(e1) D(e1, "z") D(e1, "x3") D(e1, "x2") ### 8. DATA GENERATION x <- 1:50 x x <- -25:25 x x <- 0.33:68.15024 x y<-rep(0,20) y 1:10-2 1:(10-2) seq(from=23, to=25, by=0.5) seq(23, 25, 0.5) ?seq seq(length = 100, from = -3e-2, to = 5) seq(from = -3, by = 0.025, to = 1.86) rep(18, 30) ?rep rep("Hi", 12) rep(m4, 3); m4 rep(name, 10) sequence(4:5) sequence(c(2,5,3)) ?sequence gl(n=4, k=5) gl(4, 5) gl(n=2, k=5) ?gl gl(4, 5, length = 29) sex<-gl(2, 6, label=c("Male", "Female")) # Task: Create a factor with 3 levels, each repeated 10 times # Task: Create a factor with 3 levels, number of replicates 1, of length 30 f1; f2; c("Male", "Female") expand.grid(g1 = f1, g2 = f2, gen = c("Male", "Female")) ?expand.grid # 9. RANDOM SEQUENCES n <- 50 nvar<-rnorm(n, mean = 0, sd = 1) nvar ?rnorm evar<-rexp(n, rate = 1) evar ?rexp rgamma(10, shape = 1, scale = 1) ?rgamma rpois(10, lambda = 0.5) ?rpois ### 10. SIMPLE ARITHMETIC AND STATISTICAL FUNCTIONS x1 x2 y <- x2[-c(5, 6)] y sum(x1) prod(x1) max(x1) min(x1) which.max(x1) which.min(x1) range(x1) length(x1) mean(x1) median(x1) var(x1) cov(x1, x1) cor(x1, x1) var(x1, y) cov(x1, y) cor(x1, y) summary(x1) # Task: Calculate the mean and variance of the random vector nvar # Task: Calculate the correlation between random vectors nvar and evar # Task: Do a summary of the normal vector nvar # Task: Do a summary of the factor sex and observe the result ### 11. SIMPLE STATISTICAL PLOTTING FUNCTIONS plot(nvar,evar) hist(nvar) hist(nvar,prob=TRUE) boxplot(nvar) x<-seq(-3,3,by=0.2) plot(x,dnorm(x,0,1)) plot(x,dnorm(x,0,1),type="l") plot(x,dnorm(x,0,1),type="b") plot(x,dnorm(x,0,1),type="n") points(x,dnorm(x,0,1),type="l",lwd=2) points(x,dnorm(x,0,1.5),type="l",col=2,lwd=2) abline(h=0.1) abline(v=-2) # Task: Plot the histogram of evar. # Then superpose a line of the density of an Exponential variable # with the same parameter value (rate) ### 12. READING AND WRITING DATA ### Reading from keyboard a <- scan() 1 2 3 4 5 6 7 8 9 10 ?scan ### Reading from a file data<-read.table("silc0106_ISI09.txt",header=TRUE) data[1:10,] prov[1:10] data$prov[1:10] data[,2][1:10] attach(data) prov[1:10] ?read.table # Task: Read the file PopnSizeProv.txt and see the population sizes of Spanish provinces ### Writing results to a file x1 <- rnorm(100) x2 <- rnorm(100) x3 <- rnorm(100) x4 <- rnorm(100) x5 <- rnorm(100) a <- data.frame(a=x1, b=x2, c=x3, d=x4, e=x5) write.table(a, file = "a.txt", row.names = FALSE) ?write.table write.table(a, file = "a.xls", row.names = FALSE, sep="\t") sink(file = "sinkfile.txt") i <- 1:10 ; i sim1 <- rnorm(100) sim1 sim2 <- rpois(100,1) sim2 sink() ?sink