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
data_frame = data.frame (
col1 = numeric (), col2 = character (),stringsAsFactors = FALSE )
print ( "Original dataframe" )
print (data_frame)
for (i in 1:3) {
vec <- c (i+1, LETTERS [i])
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
data_frame = data.frame (col1 = numeric (),
col2 = character (),
stringsAsFactors = FALSE )
print ( "Original dataframe" )
print (data_frame)
data_frame[1,] <- c (1, "firstrow" )
print ( "Dataframe : Iteration 1" )
print (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
data_frame = data.frame (col1 = c (2:3),
col2 = letters [1:2],
stringsAsFactors = FALSE )
print ( "Original dataframe" )
print (data_frame)
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
data_frame = data.frame (col1 = numeric (),
col2 = character (),
stringsAsFactors = FALSE )
print ( "Original dataframe" )
print (data_frame)
vec <- c (8, "n" )
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
data_frame = data.frame (col1 = numeric (),
col2 = character (),
stringsAsFactors = FALSE )
print ( "Original dataframe" )
print (data_frame)
for (i in 1:3) {
vec <- c (i, LETTERS [i])
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...