Creating Data Frames

We create data frames using the data.frame() function. This function takes vectors as arguments, and each vector becomes a column in the resulting data frame.

In the following code, we create a data frame called d1 that contains 10 rows and 3 columns.

  1. The first column is the integer sequence from 1 to 10
  2. The second column alternates between -1 and 1
  3. The third column contains the sequence \(\{0.1, 0.2, \ldots, 1.0\}\).
(d1 <- data.frame(1:10, c(-1, 1), seq(0.1, 1, 0.1)))
   X1.10 c..1..1. seq.0.1..1..0.1.
1      1       -1              0.1
2      2        1              0.2
3      3       -1              0.3
4      4        1              0.4
5      5       -1              0.5
6      6        1              0.6
7      7       -1              0.7
8      8        1              0.8
9      9       -1              0.9
10    10        1              1.0

Since we did not name the arguments to data.frame() when creating d1, R generated default (ugly) column names by parsing the argument values.

colnames(d1)
[1] "X1.10"            "c..1..1."         "seq.0.1..1..0.1."

We can assign our own column names by naming the arguments to data.frame().

# Generate a version of d1 with better column names
(d2 <- data.frame(x = 1:10, y = c(-1, 1), z = seq(0.1, 1, 0.1)))
    x  y   z
1   1 -1 0.1
2   2  1 0.2
3   3 -1 0.3
4   4  1 0.4
5   5 -1 0.5
6   6  1 0.6
7   7 -1 0.7
8   8  1 0.8
9   9 -1 0.9
10 10  1 1.0

We can also assign column names to an existing data frame using the names() or colnames() functions.

names(d1) <- c("foo", "bar", "baz")
d1
   foo bar baz
1    1  -1 0.1
2    2   1 0.2
3    3  -1 0.3
4    4   1 0.4
5    5  -1 0.5
6    6   1 0.6
7    7  -1 0.7
8    8   1 0.8
9    9  -1 0.9
10  10   1 1.0
colnames(d1) <- c("alice", "bob", "suzy")
d1
   alice bob suzy
1      1  -1  0.1
2      2   1  0.2
3      3  -1  0.3
4      4   1  0.4
5      5  -1  0.5
6      6   1  0.6
7      7  -1  0.7
8      8   1  0.8
9      9  -1  0.9
10    10   1  1.0

One of the greatest strengths of data frames is their ability to store heterogeneously typed columns. For example, in the following code, we create a data frame called d3 that comprises three uniquely typed columns.

  1. A logical vector containing 10 randomly sampled boolean values.
  2. A character vector containing 10 random selections from the set {“foo”, “bar”}.
  3. A numeric vector containing 10 values randomly sampled from the interval [0, 1].
(d3 <- data.frame(a = sample(c(TRUE, FALSE), 10, replace = TRUE),
                  b = sample(c("foo", "bar"), 10, replace = TRUE),
                  c = runif(10)))
       a   b         c
1   TRUE foo 0.9728604
2  FALSE foo 0.7672692
3  FALSE bar 0.3949973
4   TRUE foo 0.7527920
5  FALSE bar 0.5344230
6  FALSE bar 0.1929254
7  FALSE bar 0.3806470
8   TRUE foo 0.9981252
9   TRUE foo 0.2077513
10 FALSE foo 0.4680463

Predict what the following code will print.

If we want to quickly generate an empty data frame, we can supply a matrix as the sole argument to data.frame().

(d4 <- data.frame(matrix(NA, 10, 3)))
   X1 X2 X3
1  NA NA NA
2  NA NA NA
3  NA NA NA
4  NA NA NA
5  NA NA NA
6  NA NA NA
7  NA NA NA
8  NA NA NA
9  NA NA NA
10 NA NA NA
Practice

Create a data frame containing 7 observations of 3 variables.

  • Use a different type for each of the variables.

There are an infinite number of correct solutions to this problem (literally). The following is one possibility.

df <- data.frame(
    text = rep("text", 7),
    integer = sample(1:10, 7, replace = TRUE),
    complex = complex(7, 1:5, 1:3)
)

df
  text integer complex
1 text       6    1+1i
2 text       4    2+2i
3 text       3    3+3i
4 text       2    4+1i
5 text      10    5+2i
6 text       3    1+3i
7 text       1    2+1i
Knowledge Check
  • Can two columns in the same data frame have different types? Why or why not?
  • Can two columns in the same data frame have different lengths? Why or why not?
Back to top