Create DataFrame Row by Row in R - GeeksforGeeks

27 Jun, 2021
In this article, we will discuss how to create dataframe row by row in R Programming Language.
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 framedata_frame = data.frame(  col1 = numeric(), col2 = character(),stringsAsFactors = FALSE)print ("Original dataframe")print (data_frame) # appending rows to the data framefor(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
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 framedata_frame = data.frame(col1 = numeric(),                        col2 = character(),                        stringsAsFactors = FALSE) print ("Original dataframe")print (data_frame) # appending rows to the data framedata_frame[1,] <- c(1,"firstrow") print ("Dataframe : Iteration 1")print (data_frame) # appending rows to the data framedata_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 framedata_frame = data.frame(col1 = c(2:3),                        col2 = letters[1:2],                        stringsAsFactors = FALSE) print ("Original dataframe")print (data_frame) # appending rows at the endvec <- 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
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 framedata_frame = data.frame(col1 = numeric(),                        col2 = character(),                        stringsAsFactors = FALSE) print ("Original dataframe")print (data_frame) # appending rows at the endvec <- c(8,"n") # binding row at the end of the data framedata_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 framedata_frame = data.frame(col1 = numeric(),                        col2 = character(),                        stringsAsFactors = FALSE) print ("Original dataframe")print (data_frame) # appending rows at the endfor(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
anikakapoor
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
How to change Row Names of DataFrame in R ?
Remove rows with NA in one column of R DataFrame
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?