7.5 Creating Data Frames

There are many ways to create data frames in R. Here we will introduce just two ways.

7.5.1 Creation from Vectors

Whenever you have vectors of the same length, you can combine them into a data frame, using the data.frame() function:

n <- c("Dorothy", "Lion", "Scarecrow")
h <- c(58, 75, 69)
a <- c(12, 0.04, 18)
ozFolk <- data.frame(name = n, height = h, age = a)
ozFolk
##        name height   age
## 1   Dorothy     58 12.00
## 2      Lion     75  0.04
## 3 Scarecrow     69 18.00

Note that at the time of creation you can provide the variables with any names that you like. If later on you change your mind about the names, you can always revise them:

names(ozFolk)  
## [1] "name"   "height" "age"
names(ozFolk)[2] <- "Height"  # "height" was at index 2"
ozFolk
##        name Height   age
## 1   Dorothy     58 12.00
## 2      Lion     75  0.04
## 3 Scarecrow     69 18.00

Let’s check the structure of the frame we have made:

str(ozFolk)
## 'data.frame':    3 obs. of  3 variables:
##  $ name  : Factor w/ 3 levels "Dorothy","Lion",..: 1 2 3
##  $ Height: num  58 75 69
##  $ age   : num  12 0.04 18

Maybe we would prefer that the name variable not be a factor. We have a couple of options to accomplish this.

  1. We could coerce names to a character variable, and assign it to the data frame:

    ozFolk$name <- as.character(ozFolk$name)
    str(ozFolk)
    ## 'data.frame':    3 obs. of  3 variables:
    ##  $ name  : chr  "Dorothy" "Lion" "Scarecrow"
    ##  $ Height: num  58 75 69
    ##  $ age   : num  12 0.04 18
  2. We could prevent names from being made into a factor at the time of creation:

    ozFolk <- data.frame(name = n, height = h, age = a,
                         stringsAsFactors = FALSE)
    str(ozFolk)
    ## 'data.frame':    3 obs. of  3 variables:
    ##  $ name  : chr  "Dorothy" "Lion" "Scarecrow"
    ##  $ height: num  58 75 69
    ##  $ age   : num  12 0.04 18

7.5.2 Creation From Other Frames

If two frames have the same number of rows, you may combine their columns to form a new frame with the cbind() function:

ozMore <- data.frame( color = c("blue", "red", "yellow"),
                      desire = c("Kansas", "courage", "brains"))
cbind(ozFolk, ozMore)
##        name height   age  color  desire
## 1   Dorothy     58 12.00   blue  Kansas
## 2      Lion     75  0.04    red courage
## 3 Scarecrow     69 18.00 yellow  brains

Similarly if two data frames have the same number and type of columns then we can use the rbind() function to combine them:

ozFolk2 <- data.frame(name = c("Toto", "Glinda"),
                      height = c(12, 66), age = c(3, 246),
                      stringsAsFactors = FALSE)
rbind(ozFolk, ozFolk2)
##        name height    age
## 1   Dorothy     58  12.00
## 2      Lion     75   0.04
## 3 Scarecrow     69  18.00
## 4      Toto     12   3.00
## 5    Glinda     66 246.00

Note: cbind() and rbind() work for matrices, too.