Introduction
So far, we have learned about Vector in R which is similar to a one-dimensional array. It contains only elements of only one data type, i.e., all elements of a vector will be of a similar data type.
Now, we will learn about Data Frame in R. Let's start now.
Data Frame in R
Data Frame in R is a kind of data type similar to other data types, such as numeric, character, vector, etc. It is a two-dimensional object. It can contain different data type elements like numeric, character or logical in different columns. Here, one thing we need to remember is that while creating it, in any single column, all the elements should be of the same type i.e., either numeric, character, logic or something else.
In general, we can say the data frame is the more general form of the matrix as well as the collection of different vectors.
Let's see the below example of Data Frame in the image. Here, as you can see, there are seven columns containing different data type elements. But, in any single column, all elements are of the same type which verifies the definition of Data Frame in R as said above.

Creating Data Frame in R Studio
To create a Data Frame in R, we use the method data.frame(). Now, we shall see the below steps to create a Data Frame.
First, we create a vector of Rank, Country, 2019 Population, 2018 Population, and Growth Rate and then, we will use these vectors to create the above-shown vector.
We will use c() function to create vector as shown below. Other way of creating vector in R using colon (:). We will use both methods here.
- #Creating Rank Vector
- Rank <- 1:10
- #Creating Country Vector
- Country <- c("China", "India", "United States", "Indonesia", "Pakistan", "Brazil",
- "Nigeria", "Bangladesh", "Russia", "Mexico")
- #Creating 2019 Population Vector
- Population.2019 <- c(1433783686, 1366417754, 329064917, 270625568, 216565318,
- 211049527, 200963599, 163046161, 145872256, 127575529)
- #Creating 2018 Population Vector
- Population.2018 <- c(1427647786, 1352642280, 327096265, 267670543, 212228286,
- 209469323, 195874683, 161376708, 145734038, 126190788)
- #Creating Growth Rate Vector
- Growth.Rate <- c("0.43%", "1.02%", "0.60%", "1.10%", "2.04%", "0.75%", "2.60%",
- "1.03%", "0.09%", "1.10%")
- #Creating Data Frame using above vectors
- DataFrame.WorldPopulation <- data.frame(Rank, Country, Population.2019, Population.2018,
- Growth.Rate)
- #Printing the above Data Frame
- DataFrame.WorldPopulation









Join the conversation! Your thoughts help the community grow.