Skip to main content

2.7) Complex Expressions


Try:

which(GRASS$Tlrs > 1)

How does this work? Break it down by running just the bit inside the brackets on its own. Then create a new vector by typing:

GRASS_tlrs.1 <- GRASS$tlrs > 1 # NB 1 is easily confused with l!

Look at this and check what has happened. Try sum(GRASS_tlrs.1) to see coercion in action.

Type data() to see what datasets are provided with R. Choose one or two to explore using class(), str(), colnames() and summary(). We’ll come back to these in the next section.

Two more functions

array() This creates matrices or higher-order arrays: e.g. MyMatrix <- array(1:8, dim=c(2,4)). Here we supply two vectors as arguments; the second (dim=) specifies the dimensions for an array with 2 rows and 4 columns. You don’t actually need to name any arguments so long as they come in the right order – but it’s often safer to do so. Again, typing ?array will explain this and other cheerful facts about the function.
t() transposes a matrix (or vector).