Skip to main content

4.2) Handling Dataframes: As Lists


Indexing Lists

A dataframe is a special type of list: a list where all the elements have the same length, so that it can be seen as a 2-D table. We’ve already seen how an element of a list can be extracted using its name after a dollar sign, e.g.: GRASS$Dms. The other way to extract a single element is by using double square brackets.

Try GRASS[[3]] and GRASS[["Dms"]], and a couple of other columns as well. Can you specify two columns at the same time?

We can add new columns to data frames by simply naming columns that don’t exist. Try GRASS$tlrs.1 <- GRASS_Tlrs.1, then check the colnames() of the data frame.

In fact, because lists are a kind of composite vector, you can also put a list name or element-number(s) in single square brackets – e.g. GRASS["Dms"]; the subtle difference is that the result is a sub-list, complete with the element (column) names. That's four possibilities - try them all now!