Create DataFrame Row by Row in R
Last Updated :
27 Jun, 2021
In this article, we will discuss how to create dataframe row by row in R Programming Language.
Method 1: Using for loop and indexing methods
An empty data frame in R language can be created using the data.frame() method in R. For better clarity, the data types of the columns can be defined during the declaration. Each row of the data frame is a vector consisting of values belonging to different columns. The ith row in the data frame can then be assigned to this vector.
Syntax:
dataframe[ i, ] <- vec
The rows are appended to the end of the data frame. The addition of rows can also be done in a for loop, with the loop iterations equivalent to the number of rows to add. The length of the vector appended should be equivalent to the number of columns in the data frame.
Example:
R
# declaring an empty data frame
data_frame = data.frame(
col1 = numeric(), col2 = character(),stringsAsFactors = FALSE)
print ("Original dataframe")
print (data_frame)
# appending rows to the data frame
for(i in 1:3) {
# creating a vector to append to
# data frame
vec <- c(i+1, LETTERS[i])
# assigning this vector to ith row
data_frame[i, ] <- vec
}
print ("Modified dataframe")
print (data_frame)
Output
[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Modified dataframe"
col1 col2
1 2 A
2 3 B
3 4 C
Method 2 : Using manual insertion
The rows in an empty data frame can also be inserted using the row index reference. After each iteration, a new row is appended to the data frame. Modifications are made to the original data frame.
Example:
R
# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
col2 = character(),
stringsAsFactors = FALSE)
print ("Original dataframe")
print (data_frame)
# appending rows to the data frame
data_frame[1,] <- c(1,"firstrow")
print ("Dataframe : Iteration 1")
print (data_frame)
# appending rows to the data frame
data_frame[2,] <- c(2,"secondrow")
print ("Dataframe : Iteration 2")
print (data_frame)
data_frame[3,] <- c(3,"thirdrow")
print ("Dataframe : Iteration 3")
print (data_frame)
Output
[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Dataframe : Iteration 1"
col1 col2
1 1 firstrow
[1] "Dataframe : Iteration 2"
col1 col2
1 1 firstrow
2 2 secondrow
[1] "Dataframe : Iteration 3"
col1 col2
1 1 firstrow
2 2 secondrow
3 3 thirdrow
The current number of rows of the data frame can be captured by the nrow(dataframe) method. An individual row can be added to the data frame at the nrow(df)+1 index.
Syntax:
df[ nrow(df)+1, ] <- vec
Example:
R
# declaring an empty data frame
data_frame = data.frame(col1 = c(2:3),
col2 = letters[1:2],
stringsAsFactors = FALSE)
print ("Original dataframe")
print (data_frame)
# appending rows at the end
vec <- c(8,"n")
data_frame[nrow(data_frame)+1,] <- vec
print ("Modified dataframe")
print (data_frame)
Output
[1] "Original dataframe"
col1 col2
1 2 a
2 3 b
[1] "Modified dataframe"
col1 col2
1 2 a
2 3 b
3 8 n
Method 3 : Using rbind() method
rbind() method is used to bind vectors or data frame objects together to form a larger object. Initially, we can create an empty data frame and then define a new row using a vector, and bind the declared row to the data frame using the rbind() method. The new row is appended at the end. The data types of the row should be compatible with the data types declared for the original data frame. This can be enclosed within a loop to bind rows during each loop iteration.
Syntax:
rbind(df , vec)
Example:
R
# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
col2 = character(),
stringsAsFactors = FALSE)
print ("Original dataframe")
print (data_frame)
# appending rows at the end
vec <- c(8,"n")
# binding row at the end of the data frame
data_frame <- rbind(data_frame,vec,stringsAsFactors = FALSE)
print ("Modified dataframe")
print (data_frame)
Output
[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Modified dataframe"
X.8. X.n.
1 8 n
A data frame can also be created row by row, using repeated rbind() operations on the data frame in the for loop, with number of iterations equivalent to the number of rows to insert.
Example:
R
# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
col2 = character(),
stringsAsFactors = FALSE)
print ("Original dataframe")
print (data_frame)
# appending rows at the end
for(i in 1:3) {
# creating a vector to append
# to data frame
vec <- c(i, LETTERS[i])
# assigning this vector to ith row
data_frame <- rbind(data_frame,vec,stringsAsFactors=FALSE)
}
print ("Modified dataframe")
print (data_frame)
Output
[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Modified dataframe"
X.1. X.A.
1 1 A
2 2 B
3 3 C
Similar Reads
How to create an empty DataFrame in R ?
In this article, we are going to see how to create an empty DataFrame in R Programming Language. An empty data frame corresponds to the tabular structure where the axes are of length 0, that is it does not contain any data items. Method 1: We first create a matrix with both rows and columns and then
3 min read
Merge DataFrames by Row Names in R
In this article, we are going to see how to merge Dataframe by Row Name using merge in R Programming Language. The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to
2 min read
Convert Matrix to Dataframe in R
In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the
1 min read
Create Lagged Variable by Group in R DataFrame
Lagged variable is the type of variable that contains the previous value of the variable for which we want to create the lagged variable and the first value is neglected. Data can be segregated based on different groups in R programming language and then these categories can be processed differently
5 min read
Create Dataframe of Unequal Length in R
In this article, we will be looking at the approach to create a data frame of unequal length using different functions in R Programming language. To create a data frame of unequal length, we add the NA value at the end of the columns which are smaller in the lengths and makes them equal to the colum
2 min read
How to add a row to R dataframe ?
In this article, we will see how to add rows to a DataFrame in R Programming Language. To do this we will use rbind() function. This function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. Syntax: rbind(dataframe 1, dataframe 2) Example 1 : R # creating a data frame
2 min read
Reorder DataFrame by column name in R
It is very difficult any time taking task if we reorder the column name, so we use R Programming Language to do it effectively. In this article, we will be discussing the three different ways to reorder a given DataFrame by column name in R. Method 1: Manually selecting the new order of the column n
2 min read
How to Sort a DataFrame in R ?
In this article, we will discuss how to sort the dataframe in R Programming Language. In R DataFrame is a two-dimensional tabular data structure that consists of rows and columns. Sorting a DataFrame allows us to reorder the rows based on the values in one or more columns. This can be useful for var
5 min read
How to change Row Names of DataFrame in R ?
The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming. Dataset in use: First SecondThird1a72a
3 min read
How to add dataframe to dataframe in R ?
In this article, we will see how to add dataframe at the end of another dataframe in R Programming Language. Method 1: Using rbind() method The rbind() method in R works only if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a different num
4 min read