R Data Frames

KyraThompson's avatar
Published Oct 7, 2022
Contribute to Docs

Data frames are objects that store data into a table with two dimensions represented by columns and rows. The columns are the different characteristics and the rows are instances of a set of characteristics. Data frames are useful for storing different data types.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.
    • Beginner Friendly.
      14 hours

Creating Data Frames

Data frames can be created using the data.frame() function.

Syntax

data.frame(arguments)

The arguments are the list of vectors with the values for each column of the data frame.

Note: Each vector should be of the same length.

Example

The following example creates a data frame using a sample of team stats then prints it:

team_stats <- data.frame(Team = c("Aces", "Sky", "Mystics", "Storm", "Sun", "Liberty", "Wings", "Mercury"),
Games_Played = c( 6, 7, 2, 6, 7, 3, 3, 2),
Wins = c(5, 4, 0, 3, 4, 1, 1, 0),
Losses = c(1, 3, 2, 3, 3, 2, 2, 2),
Win_Percentage = c(.833, .571, .000, .500, .571, .333, .333, .000))
print(team_stats)

The example above will print the following:

Team Games_Played Wins Losses Win_Percentage
1 Aces 6 5 1 0.833
2 Sky 7 4 3 0.571
3 Mystics 2 0 2 0.000
4 Storm 6 3 3 0.500
5 Sun 7 4 3 0.571
6 Liberty 3 1 2 0.333
7 Wings 3 1 2 0.333
8 Mercury 2 0 2 0.000

Other Operations

There are several functions that can be used for analyzing data frames. Some are listed below:

Data Frames

colnames()
Returns or sets the names of the columns in a data frame.
ncol()
Returns the number of columns in a data frame.
nrow()
Returns the number of rows in a data frame.
subset()
Returns a subset of an object that matches the specified conditions.

All contributors

Contribute to Docs

Learn R on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.
    • Beginner Friendly.
      14 hours