Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn data.frame() Function | Data Frames
R Introduction: Part II

book
data.frame() Function

Good! Now you can see that data frames can be used to connect different data types. In the previous chapter, you considered one of the build-in datasets. How can we create a data frame with our data?

It can be done by using data.frame() function. This function receives vectors or lists (which we will consider in the next section) as columns. The names for columns are variable's names. For example, let's create a data frame with abstract information on three people.

# Data
name <- c("Alex", "Julia", "Finn")
age <- c(24, 43, 32)
gender <- c("M", "F", "M")

# Creating a data frame
test <- data.frame(name, age, gender)
test # Outputting the data frame
12345678
# Data name <- c("Alex", "Julia", "Finn") age <- c(24, 43, 32) gender <- c("M", "F", "M") # Creating a data frame test <- data.frame(name, age, gender) test # Outputting the data frame
copy

See, the names for columns come from variables names.

Task

Swipe to start coding

Given two vectors: items and prices containing the names and prices of goods in an abstract local furniture store. Your tasks are:

  1. Create a data frame named store with two columns, items and prices (these are respective variables names).
  2. Output store variable value.

Solution

# Data
prices <- c(340, 150, 115, 45, 160)
items <- c('Sofa', 'Armchair', 'Dining table', 'Dining chair', 'Bookshelf')

# Create a data frame
store <- data.frame(items, prices)
# Output its value
store

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
# Data
prices <- c(340, 150, 115, 45, 160)
items <- c('Sofa', 'Armchair', 'Dining table', 'Dining chair', 'Bookshelf')

# Create a data frame
___ <- ___(___)
# Output its value
___
toggle bottom row
some-alt