Dr. Mark Gardener

Home

Providing training for:

• Ecology
• Data analysis
• Statistics
• R The statistical programming language
• Data management
• Data mining

# Tips and Tricks - for R and Excel

On these pages you can find tips, tricks and hints for using both R and Excel. At the end of each tip there are links forwards and backwards as appropriate. There is also an index of R tips and an index of Excel tips.

For most analytical purposes the combination of Excel and R is unbeatable! Excel is great as a data management tool and for preparing data for analysis. You can also use it to get an overview of your data or to make simple (and not so simple) graphs. R is an analytical "swiss army knife" and can carry out a mind-boggling array of analytical routines as well as producing great graphics.

Object elements: \$ [] and [[]]

\$ selects named elements from list and data.frames

[] selects element(s) from any object

[[]] selects element(s) but suppresses display of element name

Top

### Object elements: brackets [], double brackets [[]] and \$

Many R objects are composed of multiple elements. There are various ways to extract one (or more) elements from an object, depending on the object itself.

A simple vector for example is a 1-D object; you can get elements from a vector using the square brackets:

```> data1 = c(3, 5, 7, 5, 3, 2, 6, 8, 5, 6, 9) # Make a numeric vector> data1 [1] 3 5 7 5 3 2 6 8 5 6 9
> data1[1]                     # The first item[1] 3
> data1[3]                     # The third item[1] 7
> data1[1:4]                   # The first 4 items[1] 3 5 7 5
> data1[-1]                    # All except the first [1] 5 7 5 3 2 6 8 5 6 9
> data1[c(1, 3, 4, 8)]         # The 1st, 3rd, 4th, 8th[1] 3 7 5 8
> data1[data1 > 3]             # All items > 3[1] 5 7 5 6 8 5 6 9
> data1[data1 < 5 | data1 > 7] # Items < 5 OR > 7[1] 3 3 2 8 9```

If your object has 2 dimensions, such as a data.frame or a matrix you can use the same idea but now specify [rows, columns]. Extra dimensions can be supplied if needed (e.g. for a table).

`> mymat = matrix(1:30, ncol = 5, dimnames = list(letters[1:6], LETTERS[1:5]))> mymat  A  B  C  D  Ea 1  7 13 19 25b 2  8 14 20 26c 3  9 15 21 27d 4 10 16 22 28e 5 11 17 23 29f 6 12 18 24 30`
```> mymat[2, 3]  # Item from 2nd row and 3rd column[1] 14
> mymat[, 2]   # All rows but only 2nd column a  b  c  d  e  f  7  8  9 10 11 12
> mymat[3, ]   # All columns but only 3rd row A  B  C  D  E  3  9 15 21 27
> mymat[-1, ]  # All columns and all rows except the first  A  B  C  D  Eb 2  8 14 20 26c 3  9 15 21 27d 4 10 16 22 28e 5 11 17 23 29f 6 12 18 24 30
> mymat[, "B"] # All rows and the row named "B" a  b  c  d  e  f  7  8  9 10 11 12```

You can also use conditional statements just like for a vector. With some objects you can use the \$, particularly data.frame and list objects:

```> mydf = data.frame(num = 1:12, mnths = month.abb[1:12],
fac = gl(3,4, labels = c("high", "mid","low")),
let = LETTERS[12:1])
> mydf
num mnths  fac let
1    1   Jan high   L
2    2   Feb high   K
3    3   Mar high   J
4    4   Apr high   I
5    5   May  mid   H
6    6   Jun  mid   G
7    7   Jul  mid   F
8    8   Aug  mid   E
9    9   Sep  low   D
10  10   Oct  low   C
11  11   Nov  low   B
12  12   Dec  low   A

> mylist = list(num = 1:6, let = letters[9:1], mnth = month.abb[1:7])
> mylist
\$num
[1] 1 2 3 4 5 6```
```\$let
[1] "i" "h" "g" "f" "e" "d" "c" "b" "a"```
```\$mnth
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul"```

The \$ is used with the element name like so:

```> mydf\$let [1] L K J I H G F E D C B ALevels: A B C D E F G H I J K L
> mydf\$num [1]  1  2  3  4  5  6  7  8  9 10 11 12
> mylist\$mnth[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul"```

You can also use square brackets. For the data.frame the [row, column] syntax works as it did for the matrix. Giving a single value selects a column.

```> mylist[1]
\$num
[1] 1 2 3 4 5 6```
```> mydf[2]
mnths
1    Jan
2    Feb
3    Mar
4    Apr
5    May
6    Jun
7    Jul
8    Aug
9    Sep
10   Oct
11   Nov
12   Dec

> mydf[2,]
num mnths  fac let
2   2   Feb high   K    ```

Note that the \$ does not work with a matrix object.

You can use double brackets to select elements in more or less the same way as single brackets. The difference between single and double is that with double brackets any element names are not displayed:

```> mydf[[2]]
[1] Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Levels: Apr Aug Dec Feb Jan Jul Jun Mar May Nov Oct Sep

> mydf[2]
mnths
1    Jan
2    Feb
3    Mar
4    Apr
5    May
6    Jun
7    Jul
8    Aug
9    Sep
10   Oct
11   Nov
12   Dec

> mylist[3]
\$mnth
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul"```
```> mylist[[3]]
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul"```

You don't have to use an index value, the element name gives a similar result:

```> mylist[["let"]][1] "i" "h" "g" "f" "e" "d" "c" "b" "a"
> mylist["let"]\$let[1] "i" "h" "g" "f" "e" "d" "c" "b" "a"```

So, the \$ can be used with vector and data.frame objects and the [] with more or less any object. Use [[]] to suppress the element name.

Top << Previous tip: Row & column names using dimnames() >> Next tip: Rotating objects using t()

Top Tips & Tricks Home Index of R Tips Index of Excel Tips

An introduction to R

See my Publications about Excel, R, statistics and data analysis Courses in R, data analysis, data management and statistics Visit the R Project website

See my Publications about statistics and data analysis.

MonogRaphs: random topics in R

Writer's Bloc – my latest writing project includes R scripts

Courses in data analysis, data management and statistics.

Top Home
Data Analysis
Contact GardenersOwn Homepage