CSC121 - R Console Wednesday March 7th --------------------------- > # Built-in R functions to manipulate data > # Summing up a set of numeric data > sum(c(1,2,3,4)) [1] 10 > prod(c(1,2,3,4)) [1] 24 > mean(c(1,2,3,4)) [1] 2.5 > sd(c(1,2,3,4)) [1] 1.290994 > var(c(1,2,3,4)) [1] 1.666667 > sqrt(var(c(1,2,3,4))) [1] 1.290994 > # The standard deviation is the square root of the variance > > # To get data from a text file on your computer, ensure your working directory is the same as where the data is located > getwd() [1] "/Users/csc121/Desktop" > # setwd("PUT YOUR DIRECTORY STRING HERE") > scan("numbers.txt") Read 4 items [1] 1 2 3 4 > data <- scan("numbers.txt") Read 4 items > data [1] 1 2 3 4 > mean(data) [1] 2.5 > matrix(scan("numbers.txt"), ncol=2, nrow=2) Read 4 items [,1] [,2] [1,] 1 3 [2,] 2 4 > setwd("/Users/csc121/Desktop") > data2 <- scan("http://www.cs.utoronto.ca/~radford/csc121.S17/data2") Read 50 items > data2 [1] 2.170 1.985 1.616 3.181 -0.978 1.597 0.862 -0.186 0.956 0.169 -0.403 1.107 0.965 [14] 2.155 -0.141 1.647 3.007 0.631 0.393 2.883 1.588 -0.228 1.078 0.355 -0.113 0.852 [27] 0.913 2.876 -0.499 -1.607 1.749 0.167 1.366 1.976 2.907 3.470 1.162 0.871 0.506 [40] 3.138 0.920 0.743 -1.242 -0.678 1.104 0.817 2.226 1.521 1.915 0.095 > > # Back to plotting > # One thing that is often plotted on a graph is a straight line > # Straight lines can indicate important values > # To draw a line we use the abline() function > plot(data) > abline(h=2) > abline(v=2) > abline(v=2, lty="dashed") > plot(data) > abline(v=2, lty="dashed") > abline(h=c(2.5, 3), lty="dotted") > # Plotting more than one graph on the same plot > par(mfrow = c(1, 2))