4 Ways to make Data Frames in R!
Written by Matt Dancho on February 2, 2021
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:
Here are the links to get set up. 👇
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.
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
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.
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
Why do we need enframe()?
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
Once you take these actions, you’ll be set up to receive R-Tips with Code every week. =)
👇 Top R-Tips Tutorials you might like:
- mmtable2: ggplot2 for tables
- ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2
- ggside: Plot linear regression with marginal distributions
- DataEditR: Interactive Data Editing in R
- openxlsx: How to Automate Excel in R
- officer: How to Automate PowerPoint in R
- DataExplorer: Fast EDA in R
- esquisse: Interactive ggplot2 builder
- gghalves: Half-plots with ggplot2
- rmarkdown: How to Automate PDF Reporting
- patchwork: How to combine multiple ggplots
- Geospatial Map Visualizations in R
Want these tips every week? Join R-Tips Weekly.