4 Ways to make Data Frames in R!

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This article is part of a R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.


Data frames (like Excel​ tables) are the main way for storing, organizing, and analyzing data in R​. Here are 4 ways using the tidyverse​: tibble, as_tibble(), read_excel(), and enframe()/deframe().


Here are the links to get set up. 👇

(Click image to play tutorial)


Making Data Frames in R

Data frames are the most important data structure in R. They are just like Excel Tables. They keep your data organized.

We’re going to shed light on 4 SUPER POWERFUL ways to create data frames (from beginner to intermediate):

  • tibble() – For making simple data frames from scratch
  • read_excel() – For importing data from Excel worksheets as data frames.
  • as_tibble() – For converting lists and matrix objects to data frames
  • enframe() – A SUPER-POWER. Convert ANYTHING to a data frame 🤯

As you go along, you can use my Ultimate R Cheatsheet for getting R importing & data wrangling down. It consolidates the most important R packages I use every day into one cheatsheet.

Method 1: Using tibble()
Make simple data frames from scratch.

The tidyverse uses a structure called a “tibble”, which is simply a Data Frame (like an excel table) but with more informative printing than the default data frame.

We use the tibble() function to create a “tibble” from scratch. Here’s a simple tibble I created and compared to a basic R dataframe. The tibble printing is much more informative.

Method 2: Using read_excel()
Use read_excel() to read excel worksheets.

Data importing is how we get data into R. There are a ton of ways to import data (check out my Ultimate R Cheatsheet for getting R importing down).

If we are working in Excel, we can import the data as a tibble using the readxl package’s read_excel() function.

Method 3: Using as_tibble()
For converting from other data structures

The next function, as_tibble(), helps convert from list or matrix data structures to tibbles. Here we have a pretty complex (nested) list.

Using as_tibble(), we just made it an organized data frame that’s ready for analysis!

Method 4: Using enframe()
For converting ANYTHING to a data frame.

The last function, enframe(), is a MORE POWERFUL / FLEXIBLE version of as_tibble().

Why do we need enframe()?

When as_tibble() fails, enframe() is your Plan B.


You’re becoming a data ninja one R-tip at a time


But if you really want to improve your productivity…

Here’s how to master R. 👇 What happens after you learn R for Business.

When your CEO gets word of your Shiny Apps saving the company tons of $$$ (cha-ching!). 👇

This is career acceleration.


SETUP R-TIPS WEEKLY PROJECT

  1. Sign Up to Get the R-Tips Weekly (You’ll get email notifications of NEW R-Tips as they are released)

  2. Set Up the GitHub Repo

  3. Check out the setup video. Or, Hit Pull in the Git Menu to get the R-Tips Code

Once you take these actions, you’ll be set up to receive R-Tips with Code every week. =)




To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)