How to rename a variable in R without copying the object?

后端 未结 3 880
予麋鹿
予麋鹿 2020-12-08 07:01

I have a huge data frame loaded in global environment in R named df. How can I rename the data frame without copying the data frame by assigning it to another s

3条回答
  •  旧时难觅i
    2020-12-08 07:46

    There is a function called mv in the gdata package.

    library(gdata)
    
    x <- data.frame(A = 1:100, B = 101:200, C = 201:300)
    
    tracemem(x)
    "<0000000024EA66F8>"
    
    mv(from = "x", to = "y")
    
    tracemem(y)
    "<0000000024EA66F8>"
    

    You will notice that the output from tracemem is identical for x and y. Looking at the code of mv, you will see that it assigns the object to the environment in scope and then removes the old object. This is quite similar to the approach C8H10N4O2 used (although mv is for a single object), but at least the function is convenient to use.

提交回复
热议问题