Data Frames in R Programming: Properties & Manipulation

Instructor: Alexis Kypridemos

Alexis is a technical writer for an IT company and has worked in publishing as a writer, editor and web designer. He has a BA in Communication.

This lesson will explain what data frames are in R programming. We will cover how to create them, both manually and by importing existing files, and how to manipulate them using a number of built-in R functions.

Data Frames in R Programming

A data frame is a type of data structure in R programming. Essentially, it is a data table, storing data in rows and columns, like a spreadsheet. For this reason, it is possible to import a CSV file or Excel spreadsheet directly into a data frame in R.

Fundamental Properties

Data frames can store data of just about any type... for example, numeric, integer, character, and so on. The user can create multiple vectors, each vector holding a different type of data, and merge all of them into a single data frame. Certain limitations apply to this. Firstly, all the vectors or columns must contain the same number of values, as each value will be translated into a row once these vectors are imported into the data frame. Secondly, no blank or null values are permitted.

Creating a Data Frame Manually

A data frame can be created manually, or, as mentioned above, data can be imported directly into a data frame from a comma delimited file (CSV) or spreadsheet (so long as the data is already in tabular format). Using the first method, let's create a data frame named ''myTable'' to store the average monthly temperature for the city of Turin.

First, let's decide how many values per row we need. That's easy - a year has 12 months, so we will make sure each row has 12 values. A good way to do this is to make the first row a set of 12 integers numbered 1 through 12:

monthID <- c(1:12)

Since figuring out the name of the month from a number isn't always easy, let's add a row with matching names for the months:

monthName <- c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

Lastly, we need a row describing the average monthly temperature in Turin, from January to December:

temperature <- c(3, 3, 7, 10, 14, 18, 21, 21, 18, 12, 7, 4)

Notice how each of these three vectors contains a different data type. R interprets monthName as containing integers, monthName as a Factor, and temperature as numeric. Now all we need to do is use the data.frame() function to combine these vectors into an R data frame:

myTable <- data.frame(monthID, monthName, temperature)

Isn't that easy? To view the contents of this newly created data frame, either type its name, or use the print() function:

myTable

print(myTable)

Importing a CSV File

It is also possible to create a data frame by loading existing data which is in the right format, such as CSV or XLS. Although we won't describe the XLS method here, we will describe CSV. Since we already have a data frame entered into R, let's save that to a CSV file so we can do this:

write.csv(myTable, file = 'myTable.csv')

Okay, now we can show how to load a data frame from a CSV file. We'll use a new variable because we want to verify that it worked:

newTable <-read.csv('myTable.csv', header=TRUE, sep=',')

Just remember that the read.csv() function requires three arguments:

  • The file name and extension
  • Whether the first row contains the column headers (enter TRUE or FALSE depending on the case)
  • The delimiter used to separate the values in the CSV file. In this example a comma was used, but other CSV files might use a different character like a semicolon.

Manipulating Data Frames

We can do a lot of things once we have data in our data frame. We will cover a few of these here.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support