Open In App

Duplicate a data frame using R

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore various methods to duplicate the data frame by using the R Programming Language.

How to duplicate a data frame

R language offers various methods to duplicate the data frame. By using these methods provided by R, it is possible to duplicate the data frame. Some of the methods to duplicate the data frame are:

By using the data.frame() function

This method is used to duplicate the data frames efficiently. The syntax is as follows:

data.frame(data frame)

In the below example, we created a data frame and performed duplication on it by using the function data. frame().

R
df = data.frame(
  ID = 1:6,
  value=10:15,
  number =20:25
)
print("The original data frame is")
print(df)

# Duplicating dataframe 
result = data.frame(df)

print("The duplicate data frame is")
print(result)

Output:

[1] "The original data frame is"
ID value number
1 1 10 20
2 2 11 21
3 3 12 22
4 4 13 23
5 5 14 24
6 6 15 25

[1] "The duplicate data frame is"
ID value number
1 1 10 20
2 2 11 21
3 3 12 22
4 4 13 23
5 5 14 24
6 6 15 25

In the below example, we created a data frame and performed duplication on it by using the function data.frame.

R
name=c("rahul","rishi","raki","lokesh","suresh")
id_no=c(51,44,65,76,87)
branch=c("cse","ece","eee","it","ds")

df = data.frame(name, id_no,branch)

print("The original data frame is")
print(df)

# Duplicating dataframe 
result = data.frame(df)

print("The duplicate data frame is")
print(result)

Output:

[1] "The original data frame is"
name id_no branch
1 rahul 51 cse
2 rishi 44 ece
3 raki 65 eee
4 lokesh 76 it
5 suresh 87 ds

[1] "The duplicate data frame is"
name id_no branch
1 rahul 51 cse
2 rishi 44 ece
3 raki 65 eee
4 lokesh 76 it
5 suresh 87 ds

Using replicate() function to duplicate the data frame

The function replicate() is used to create number of duplicates or copies of the data frame according to the requirements. The syntax is as follows:

replicate(data frame)

In the below example, we created a data frame and performed duplication on it by using the function ‘replicate()’.

R
# creating data frame
 df <- data.frame(
  id = 5:10,
  name = c("Bravo","deepak","watson","ruthu","ambati","fab"),
  age = c(15, 20, 28, 35, 40, 45)
)
print("The original data frame is")
print(df)

# No of duplicates
num =2 

# Duplicating the data frame
res <- replicate(num, df, simplify = FALSE)
print("The duplicates of data frame are")
print(res)

Output:

[1] "The original data frame is"
id name age
1 5 Bravo 15
2 6 deepak 20
3 7 watson 28
4 8 ruthu 35
5 9 ambati 40
6 10 fab 45

[1] "The duplicates of data frame are"
[[1]]
id name age
1 5 Bravo 15
2 6 deepak 20
3 7 watson 28
4 8 ruthu 35
5 9 ambati 40
6 10 fab 45

[[2]]
id name age
1 5 Bravo 15
2 6 deepak 20
3 7 watson 28
4 8 ruthu 35
5 9 ambati 40
6 10 fab 45

In the below example, we created a data frame and performed duplication on it by using the function ‘replicate()’.

R
a=c(31:38)
b=c(11:18)
c=c(21:28)

# creating data frame
 df <- data.frame(a,b,c)
print("The original data frame is")
print(df)

# No of duplicates
n =3

# Duplicating the data frame
result = replicate(n, df, simplify = FALSE)
print("The duplicates of data frame are")
print(result)

Output:

[1] "The original data frame is"
a b c
1 31 11 21
2 32 12 22
3 33 13 23
4 34 14 24
5 35 15 25
6 36 16 26
7 37 17 27
8 38 18 28

[1] "The duplicates of data frame are"
[[1]]
a b c
1 31 11 21
2 32 12 22
3 33 13 23
4 34 14 24
5 35 15 25
6 36 16 26
7 37 17 27
8 38 18 28

[[2]]
a b c
1 31 11 21
2 32 12 22
3 33 13 23
4 34 14 24
5 35 15 25
6 36 16 26
7 37 17 27
8 38 18 28

[[3]]
a b c
1 31 11 21
2 32 12 22
3 33 13 23
4 34 14 24
5 35 15 25
6 36 16 26
7 37 17 27
8 38 18 28

Conclusion

In Conclusion, we learned about how to duplicate the data frame by using various methods. R language offers versatile tools while dealing with duplication of data sets.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads