Combine multiple columns as row names in R

Sometimes your data contains more than one column that you want to use as labels. You may want “combination” labels for graphs or perhaps to use as row names.

This is where the paste() command comes in useful, as it allows you to combine items into a new item.

Use paste() to combine elements

The paste() command allows you to combine several items and reform them into a single item. There are many uses for this command such as making labels for plots or row names for a data object. Look at these example data for example (you can get/view the datafile here):

Colour    Coat Obs Ratio
 Green  Smooth 116     9
 Green Wrinkle  40     3
Yellow  Smooth  31     3
Yellow Wrinkle  13     1

The data shows some phenotype data. There are four varieties of pea plants with two main characteristics, the colour and the smoothness. The Obs column shows the observed frequency of plants in an experiment. The Ratio column shows the expected ratio of the four phenotypes under standard genetic theory.

It would be helpful to have the Colour and Coat combined into one and used as the row names. You can use the paste() command to achieve this.

The paste() command in its simplest form requires the names of the objects to combine and the separator (a text character).

Start by getting the data from the .txt file:

> peas <- read.table(file.choose(), header = TRUE, sep = "\t")
> peas
  Colour    Coat Obs Ratio
1  Green  Smooth 116     9
2  Green Wrinkle  40     3
3 Yellow  Smooth  31     3
4 Yellow Wrinkle  13     1

Then you need to combine the Colour and Coat columns, the separator will be a colon “:”.

> rownames(peas) <- with(peas, paste(Colour, Coat, sep = ":"))

Now you’ve got the rownames sorted you can remove the original Coat and Colour columns:

> peas <- peas[, -1:-2]
> peas
               Obs Ratio
Green:Smooth   116     9
Green:Wrinkle   40     3
Yellow:Smooth   31     3
Yellow:Wrinkle  13     1

Now you have a combination of coat and colour as a single element.

Comments are closed.