How to create a copy of a data frame in R

I want to duplicate the full contents of the data frame that was read from the csv file. I do not believe that this is duplication if I do copyOfFirstFrame <- firstFrame. So what do I need to do?

firstFrame <- read_csv("fileName.csv") copyOfFirstFrame <- ????? 

if I do the following, the memory address remains the same copyOfFirstFrame <- firstFrame

eg.

 copyOfFirstFrame <- firstFrame tracemem(firstFrame) == tracemem(copyOfFirstFrame) [1] TRUE 

The copy must contain two unique memory addresses. Check In R, how can I check if two variable names refer to the same base object? for details.

+6
source share
2 answers

Using cbind with one data.frame ensures that you have a copy:

 > df <- cbind(NA, NA) > df2 <- cbind(df) > df2 [,1] [,2] [1,] NA NA > df2[,1] <- 1 > df [,1] [,2] [1,] NA NA > df2 [,1] [,2] [1,] 1 NA > 
+6
source

Let DATA be an existing data frame object. I am creating a new COPY object, which is an exact copy of DATA, but it takes up a different place in memory and therefore does not indicate the original data frame.

I use the data.frame () function as follows:

 > COPY<-data.frame(DATA) 

I check if the memory addresses are the same or not using tracemem ():

 > tracemem(COPY)==tracemem(DATA) > [1] FALSE 

I think that is not enough.

+2
source

All Articles