Creating a Dataframe from Another Kind of Object

We have seen that the simplest way to create a dataframe in R is to read a table of data from an external file using the read.table function. Alternatively, you can create a dataframe by using the data.frame function to bind together a number of objects. Here are three vectors of the same length:

x<-runif(10)
y<-letters[1:10]
z<-sample(c(rep(T,5),rep(F,5)))

To make them into a dataframe called new, just type:

new<-data.frame(y,z,x)
new
      y      z           x
1     a   TRUE  0.72675982
2     b  FALSE  0.83847227
3     c  FALSE  0.61765685
4     d   TRUE  0.78541650
5     e  FALSE  0.51168828
6     f   TRUE  0.53526324
7     g   TRUE  0.05552335
8     h   TRUE  0.78486234
9     i  FALSE  0.68385443
10    j  FALSE  0.89367837

Note that the order of the columns is controlled simply the sequence of the vector names (left to right) specified within the data.frame function.

In this example, we create a table of counts of random integers from a Poisson distribution, then convert the table into a dataframe. First, we make a table object:

y<-rpois(1500,1.5)
table(y)

y

  0    1    2    3   4   5  6  7
344  502  374  199  63  11  5  2

Now it is simple to convert this table object into a dataframe with two variables, the count and the frequency using the as.data.frame function:

as.data.frame(table(y))
    y   Freq
1   0    344
2   1    502
3   2    374
4   3    199
5   4     63
6   5     11
7   6      5
8   7      2

In some cases you might want to expand a dataframe like the one above such that it had a separate row for every distinct count (i.e. 344 rows with y = 0, 502 rows with y = 1, ...

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.