> # Tables of counts > > # First off, we can figure out the unique values in a set > # We use the unique() > colours <- c("red", "blue", "red", "red", "green", "blue") > unique(colours) [1] "red" "blue" "green" > # These would also be the factor levels if we converted colours to a factor > as.factor(colours) [1] red blue red red green blue Levels: blue green red > > # But what if we wanted to find out how many times each unique value appears in the set? > # I.e., how many times does "red" appear in the colours vector? > # R has a class that allows us to easily do so, and it's called 'table' > # All we have to do, is feed our vector to table() > t <- table(colours) > t colours blue green red 2 1 3 > class(t) [1] "table" > # We can also do this with numbers > ages <- c(22,20,21,21,23,21) > t2 <- table(ages) > t2 ages 20 21 22 23 1 3 1 1 > # > # Table of joint counts > # You can also make a table with two vectors and count how often each combination occurs > shapes <- c("round", "round", "square", "round", "square", "round") > colours <- c("red", "blue", "red", "red", "green", "blue") > table(colours, shapes) shapes colours round square blue 2 0 green 0 1 red 2 1 > # They are counted by considering the values at each index in the two vectors > # You can also use a two-column data frame > d <- data.frame(col=colours, shape=shapes) > table(d) shape col round square blue 2 0 green 0 1 red 2 1 > colours <- c("red", "blue", "red", "red", "green", "blue", "red") > table(colours, shapes) Error in table(colours, shapes) : all arguments must have the same length > > # Bar plots > > # Something that we could use our table of counts for is to make a barplot > # A barplot is a plot that shows the frequency of each unique value using different sized bars > # Let's make a bar plot of our colours table 't' > barplot(t) > ages [1] 22 20 21 21 23 21 Warning message: In doTryCatch(return(expr), name, parentenv, handler) : invalid graphics state > ages [1] 22 20 21 21 23 21 > barplot(table(ages)) > # You can make a bar plot with a table joint counts > barplot(table(colours, shapes)) > barplot(table(colours, shapes)) Error in table(colours, shapes) : all arguments must have the same length > colours <- c("red", "blue", "red", "red", "green", "blue") > barplot(table(colours, shapes)) > barplot(t) > barplot(table(ages)) > help(barplot) > barplot(t, names.arg = c("b", "g", "r")) > source('~/Desktop/word_frequencies.R') > > # There are many ways to change how your plot looks (colours, labels, etc.) > # You can have a look at some R documentation online and start getting creative > > # R built-in data: trees > trees Girth Height Volume 1 8.3 70 10.3 2 8.6 65 10.3 3 8.8 63 10.2 4 10.5 72 16.4 5 10.7 81 18.8 6 10.8 83 19.7 7 11.0 66 15.6 8 11.0 75 18.2 9 11.1 80 22.6 10 11.2 75 19.9 11 11.3 79 24.2 12 11.4 76 21.0 13 11.4 76 21.4 14 11.7 69 21.3 15 12.0 75 19.1 16 12.9 74 22.2 17 12.9 85 33.8 18 13.3 86 27.4 19 13.7 71 25.7 20 13.8 64 24.9 21 14.0 78 34.5 22 14.2 80 31.7 23 14.5 74 36.3 24 16.0 72 38.3 25 16.3 77 42.6 26 17.3 81 55.4 27 17.5 82 55.7 28 17.9 80 58.3 29 18.0 80 51.5 30 18.0 80 51.0 31 20.6 87 77.0 > lm (trees$Volume ~ trees$Girth) Call: lm(formula = trees$Volume ~ trees$Girth) Coefficients: (Intercept) trees$Girth -36.943 5.066 > m <- lm(trees$Volume ~ trees$Girth) > abline(m)