Naming columns and rows

It is often convenient to name the columns and rows within a dataset to keep things clearly organized.  This is very easy to do in R, here’s how.  First let’s make some data.

a <- c(62.3, 55.3, 65.3, 59.3, 67.3)
b <- c(2.2, 5.4, 1.3, 2.8, 5.4)
c <- c(0.1, 1.5, 1.6, 2.1, 0.3)
data <- cbind(a, b, c)
data
##         a   b   c
## [1,] 62.3 2.2 0.1
## [2,] 55.3 5.4 1.5
## [3,] 65.3 1.3 1.6
## [4,] 59.3 2.8 2.1
## [5,] 67.3 5.4 0.3

We can see that this dataset “data” already has the column names “a”, “b”, and “c” stored for me when I used the function cbind. ***For more information on cbind and creating data, check out my other post here***.

Now let’s update the column names to something meaningful using the function colnames. For this function we have to define the data we are working on and then the names of the columns.
colnames(data name here) <- c(“column name 1”, “column name 2”, “etc”)

colnames(data) <- c("temp_F", "wind_m/s", "precip_in")
data
##      temp_F wind_m/s precip_in
## [1,]   62.3      2.2       0.1
## [2,]   55.3      5.4       1.5
## [3,]   65.3      1.3       1.6
## [4,]   59.3      2.8       2.1
## [5,]   67.3      5.4       0.3

Now let’s say that we want to update the row names to be meaningful as well. The function is you guessed it rownames and works the same way as colnames. rownames(data name here) <- c(“row name 1”, “row name 2”, “etc”)

rownames(data) <- c("Site 1", "Site 2", "Site 3", "Site 4", "Site 5")
data
##        temp_F wind_m/s precip_in
## Site 1   62.3      2.2       0.1
## Site 2   55.3      5.4       1.5
## Site 3   65.3      1.3       1.6
## Site 4   59.3      2.8       2.1
## Site 5   67.3      5.4       0.3

Let’s try to break rownames quickly by adding too many row names (6 row names instead of 5). We’ll find that R will get angry and let us know with a semi-cryptic error message.

rownames(data) <- c("Site 1", "Site 2", "Site 3", "Site 4", "Site 5", "Site 6")
## Error in `rownames<-`(`*tmp*`, value = c("Site 1", "Site 2", "Site 3",  : 
## length of 'dimnames' [1] not equal to array extent

If you make a data frame, your data will already reflect the column headers as well. Remember from last time that we can make a data frame using the function data.frame. Here’s an example:

numbers <- c(1, 2, 3, 4, 5)
letters <- c("a", "b", "c", "d", "e")
symbols <- c("!", "@", "#", "$", "%")
data2 <- data.frame(numbers, letters, symbols)
data2
##   numbers letters symbols
## 1       1       a       !
## 2       2       b       @
## 3       3       c       #
## 4       4       d       $
## 5       5       e       %

We can see that our new data frame already has the column names “numbers”, “letters”, and “symbols” from the way we build our dataset.

Advertisements

One thought on “Naming columns and rows

  1. Pingback: Creating data | The Practical R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s