Skip to Content
The R Book
book

The R Book

by Michael J. Crawley
June 2007
Beginner to intermediate
950 pages
27h 8m
English
Wiley
Content preview from The R Book

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, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

The R Book, 2nd Edition

The R Book, 2nd Edition

Michael J. Crawley
The R Book, 3rd Edition

The R Book, 3rd Edition

Elinor Jones, Simon Harden, Michael J. Crawley
The Book of R

The Book of R

Tilman M. Davies

Publisher Resources

ISBN: 9780470510247Purchase book