如何在R中创建数据框的副本 [英] How do I create a copy of a data frame in R

查看:69
本文介绍了如何在R中创建数据框的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想复制从* .csv文件中读取的数据帧的全部内容。我不相信如果执行 copyOfFirstFrame<-firstFrame ,这是重复的。那我该怎么办?

I want to duplicate the full contents of a data frame that has been read in from a *.csv file. I don't believe it is a 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
tracemem(firstFrame) == tracemem(copyOfFirstFrame)
[1] TRUE

副本必须产生两个唯一的内存地址。检查在R,如何检查两个变量名是否引用了相同的基础对象?以获取详细信息。

The copy must result in two unique memory addresses. Check In R, how can I check if two variable names reference the same underlying object? for details.

推荐答案

让数据是预先存在的数据框对象。
我正在创建一个新对象COPY,它是DATA的精确副本,但它占用的内存位置不同,因此没有指向原始数据框。

Let DATA be a pre-existing data frame object. I am creating a new object, COPY which is an exact copy of DATA, but it occupies a different memory location and hence doesn't point to the original data frame.

我像这样使用data.frame()函数:

I use the function data.frame() like this:

> COPY<-data.frame(DATA)

我使用tracemem检查内存地址是否相同():

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

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

我认为还不错。

这篇关于如何在R中创建数据框的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆