# STAT_540_02_R_basic_ops.R x <- 5 y <- 8 x**y x <- c(1,2,3) y <- c(5,6,7) x*y x+y x^y v <- c(1,2,3,4,5,6) v + y # values of shorter vector get "recycled" a <- c(4,9,6,4) a + v # a has length 4, v has length 6 ## Logical operations 4 > 5 4 == 4 4 >= 4 3!=4 x <- 1/2 # ask if x lies in the interval [0,1) ? (x >= 0) & (x < 1) # is x outside the interval [0,1] ? (x < 0) | (x > 1) # What about this? x <- 1/2 ((x >= 0) & (x < 1)) | ((x >= 2) & (x < 3)) # x <- 1/2 !(x < 1) # witness the coercion to numeric!! -!(x < 1) -(x < 1) !FALSE !TRUE ## "cat" == "dog" "cat" < "dog" "cat" <= "cat" "cat" != "dog" # logical operations on vectors x <- 1:10 x x <= 5 # shorter vectors also recycled with logical operators y <- c(0,10) x < y # subsetting vector (or matrices/array) with logicals x <- 1:10 x x[x <= 5] # which() returns indices in which logical = TRUE a <- c(2,4,8,5,3,2,8,9) b <- c(1,6,8,8,65,3,5,3) a < b which(a < b) # take only those values in a which are less than the corresponding entry of b a[a < b] # these two are equivalent a[which(a < b)] # replace a subset of values... a[a >= b] <- 0 # replace entries of a with zero if they meet or exceed value of entry b # the functions any() and all() u <- c(-2,4,-6,7) any(u < 0) all(u < 0) # Clever ways to use logical values as numeric values # replace negative values with 0 x <- c(-3,4,6,-2,0,5) x*(x >= 0) # set to zero values not exceeding a threshold in absolute values thresh <- 3 x*(x >= 3) + x*(x <= - 3) ## The modulo operator %%: returns the remainder of division 5 %% 2 x <- 3759 x %% 2 # round() function pi round(pi,4) floor(c(1.2,12.8)) ceiling(c(1.2,12.8)) # basic statistics functions x <- c(10.00,7.30,8.50,4.25,16.00,9.50,10.40,6.40,9.75,2.10,19.00,5.60,8.75,8.90,10.40,3.40) mean(x) median(x) sd(x) # standard deviation min(x) max(x) length(x) # get the length of a vector summary(x) # get numerical summary sort(x) sort(x, decreasing = TRUE) x order(x) # gives the indices of smallest to largest obs x <- c(4,8,1,2) x sort(x) rank(x) # missing values in R NA # this is code for a missing value x <- c(1,45,2,NA,5,87) x mean(x) mean(x,na.rm = T) # NA remove = TRUE sort(x) is.na(x) # ask if each entry is missing or not any(is.na(x)) # ask if there are ANY missing values in x # what about NaN? 0 / 0 # NaN 1 / 0 # Inf NaN # means Not a Number x na.omit(x) # omit missing value # applying functions to matrices and arrays: M <- matrix(1:24,nrow = 6, ncol = 4) M[3,4] # sum of each row of M. Use apply() function apply(M,1,sum) # put 1 to apply across rows apply(M,2,sum) # put 2 to apply across columns M apply(M,2,max) # Making plots x <- c(10.00,7.30,8.50,4.25,16.00,9.50,10.40,6.40,9.75,2.10,19.00,5.60,8.75,8.90,10.40,3.40) hist(x) boxplot(x) # making customized plots # start with a scatterplot x <- c(1,2,3,4,5,6,7) y <- c(3,6,8,3,5,9,10) # these put y on the vertical axis: plot(y~x) plot(x,y) # these put x on the vertical axis plot(x~y) plot(y,x) # use par() to set up some settings for all your plots par(mar= c(4.1,4.1,1.1,1.1), cex = .75) # cex can scale things up or down plot(y~x, xlab = "horizontal axis", ylab = "vert axis", pch = 19,# any number between 1 and 20?? main = "This is a plot", bty = "l", # change style of box around plot col = "red", xlim = c(-2,10), ylim = c(0,12), xaxt = "n") # no x axis # let's plot the sine function x_seq <- seq(0,4*pi,length=200) plot(sin(x_seq)~x_seq,type = "l") lines(cos(x_seq)~x_seq, col = "blue", lty = 4) x_seq2 <- seq(0,4*pi,by = pi/4) points(cos(x_seq2)~x_seq2, pch = 19, col = rgb(0.545,0,0,.3)) # how to add a legend to a plot legend(x = 0, # location of legend y = 1, legend = c("sine","cosine","points of interest"),# what will the legend say? lty = c(1,4,NA), # since I have lines, use lty= for line type pch = c(NA,NA,19), col = c("black","blue",rgb(0.545,0,0,.3))) # drawing polygons par(mar= c(4.1,4.1,1.1,4.1), cex = .75) plot(NA, # begin with a blank plot xlim = c(-1,1), ylim = c(-1,1)) # draw a triangle and fill it in. # with corners at the points (0,0),(1,0),(-1/2,1/2) x <- c(0,1,-1/2,0) y <- c(0,0,1/2,0) polygon(x,y,col = "red", border = NA) # place text at (-.5,.5) text(x = -.5, y = 0.5, labels = "A") # adding another axis to your plot: axis(side = 4, # this is the right side at = c((-3):3)/3, # where to put tick marks labels = paste((-3):3,"/3",sep =""), las = 2) # multiple plots in the same figure: # first way: # make a grid of plots par(mfrow = c(3,2)) # plot(1:20,pch=1:20) plot(1:20,pch=1:20, col = "blue") plot(1:20,pch=1:20, col = "red") plot(1:20,pch=1:20, col ="chartreuse") plot(1:20,pch=1:20, col = "gray") plot(1:20,pch=1:20, col = "darkgreen") # second way: # use the layout() function # you need to make a matrix # 2 plots on top, and a big plot on the bottom M <- matrix(c(1,2,3,3),nrow = 2,byrow = T) layout(M) plot(1:20,pch=1:20, col ="chartreuse") plot(1:20,pch=1:20, col = "gray") plot(1:20,pch=1:20, col = "darkgreen")