Often you may want to create a copy of a data.table in R so that you can perform specific tasks on a copy of the original data without changing the original data.table in any way.
Fortunately this is easy to do by using the copy() function from the data.table package in R, which is designed to perform this exact task.
You can use the following basic syntax to create a copy of a data.table object in R:
library(data.table)
dt2 <- copy(dt)
This particular example creates a copy of the data.table object named dt and stores it in a new data.table object named dt2.
Note that the new object that is created will have the exact same class as the original data.table and the same column names, attributes, etc.
The following example shows how to use the copy() function from the data.table package in practice to create a copy of a data.table object.
Note that you may need to first use the following syntax to install the data.table package if it is not already installed:
install.packages('data.table')
Once the package is installed, you can proceed to use the copy() function to create a copy of a data.table object.
Example: How to Use the copy() Function in R
Suppose we create the following data frame that contains information about various basketball players:
library(data.table)
#create data table
dt <- data.table(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
points=c(99, 68, 86, 88, 95, 74, 78, 93),
assists=c(22, 28, 31, 35, 34, 45, 28, 31),
rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))
#view data table
dt
team points assists rebounds
1: A 99 22 30
2: A 68 28 28
3: A 86 31 24
4: A 88 35 24
5: B 95 34 30
6: B 74 45 36
7: B 78 28 30
8: B 93 31 29
The data.table object named dt contains the following columns:
- team: The team name the player belongs to
- points: The total points scored by the player
- assists: The total assists made by the player
- rebounds: The total rebounds made by the player
We can use the class() function to view the class of this object named dt:
#view class of dt
class(dt)
[1] "data.table" "data.frame"
We can see that dt has a class of data.table and data.frame.
Now suppose that we would like to create an exact copy of this data.table.
We can use the copy() function to do so:
#create copy of dt dt2 <- copy(dt) #view dt2 dt2 team points assists rebounds 1: A 99 22 30 2: A 68 28 28 3: A 86 31 24 4: A 88 35 24 5: B 95 34 30 6: B 74 45 36 7: B 78 28 30 8: B 93 31 29 #view class of dt2 class(dt2) [1] "data.table" "data.frame"
When we view dt2 we can see that it contains the exact same values as dt and it also has a class of data.table and data.frame.
We can now perform operations on dt2 without changing the original data from dt.
For example, we can create a new column named points2:
#multiply points column of dt2 by 2 dt2$points <- dt2$points * 2 #view dt2 dt2 team points assists rebounds 1: A 198 22 30 2: A 136 28 28 3: A 172 31 24 4: A 176 35 24 5: B 190 34 30 6: B 148 45 36 7: B 156 28 30 8: B 186 31 29 #view dt dt team points assists rebounds 1: A 99 22 30 2: A 68 28 28 3: A 86 31 24 4: A 88 35 24 5: B 95 34 30 6: B 74 45 36 7: B 78 28 30 8: B 93 31 29
We can see that the values in the points column of dt2 have been multiplied by 2 while the points column of dt has remained unchanged.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Convert Data Frame Column to List in R
How to Count Number of Elements in List in R