如何更改数据框的名称? [英] How can I change the name of a data frame

查看:113
本文介绍了如何更改数据框的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复发的情况,我在一组长度很长的R代码的顶部设置一个值,用于子集一个或多个数据帧。这样的一个例子:

  city_code < - 202

在整个过程结束时,我想将结果保存在适当命名的数据框中,例如,根据将city_code附加到常用存根。

  city_results<  - 粘贴(city_stats,city_code,sep =)

我的问题是我无法弄清楚如何将结果数据框重命名为city_results的值。很多关于如何重命名数据帧列的信息,而不是如何重命名数据帧本身。根据建议的答案,这里有一个澄清:



谢谢@ mike-wise。有助于研究Hadley的Advanced R,具体具体问题。

  library(dplyr)
gear_code < - 4
gear_subset< - paste(mtcars_,gear_code,sep =)
mtcars_subset< - mtcars%>%filter(gear == gear_code)
head(mtcars_subset)
write.csv(mtcars_subset,file = paste(gear_subset,.csv,sep =))

这可以让我将子集写入适当命名的csv文件。但是,您的建议类型的作品,但我不能,例如,以新名称引用data.frame:

  assign(gear_subset,mtcars_subset)
head(gear_subset)


解决方案

事实是,R中的对象没有名称。存在不同种类的环境,包括每个进程的全局环境。这些环境有名称列表,指向各种对象。两个不同的名称可以指向同一个对象。这最好地解释了我在Hadley Wickhams Advanced R书的环境章节中的知识
http://adv-r.had.co.nz/Environments.html



所以没有办法更改数据框的名称,因为没有变化。但是你可以创建一个新的名字(如 newname )指向同一个对象(在你的情况下是一个数据)框架对象)作为给定的名称(如 oldname )只需执行以下操作:

  newname<  -  oldname 

请注意,如果您更改其中一个变量,则新副本将内部引用将不再一样。这是由于R的修改复制语义。看到这篇文章的解释:什么是R中的修改后修改语义,以及规范来源在哪里?



希望有所帮助。我知道痛苦。功能语言与程序语言不同...



当然,可以为数据框计算一个新名称,并在环境中注册 assign 命令 - 也许你正在寻找这个。但是之后提到的将是相当复杂的。



示例(假设 df 是有问题的数据框):

  assign(paste(city_stats,city_code,sep =),df)

如有更多信息,请始终参阅分配的帮助 http://stat.ethz.ch/R-manual/R-devel /library/base/html/assign.html



编辑:
回复您的编辑,以及关于使用 eval(parse(...)可以解析这样的名字:

  head(get(gear_subset))


I have a recurrent situation where I set a value at the top of a long set of R code that's used in subsetting one or more data frames. Something like this:

city_code <- "202"

At the end of the whole process I'd like to save the results in a data frame that's named appropriately, say, based on appending "city_code" to a common stub.

city_results <- paste("city_stats", city_code, sep = "")

My problem is that I can't figure out how to rename the resulting data frame as the value of 'city_results'. Lots of info out there on how to rename the columns of a data frame, but not on how to rename the data frame itself. Based on a proposed answer, here's a clarification:

Thanks, @mike-wise. Helpful to study Hadley's Advanced R with a concrete problem in hand.

library(dplyr)
gear_code <- 4
gear_subset <- paste("mtcars_", gear_code, sep = "")
mtcars_subset <- mtcars %>% filter(gear == gear_code)
head(mtcars_subset)
write.csv(mtcars_subset, file = paste(gear_subset, ".csv", sep = ""))

That lets me write the subset to an appropriately named csv file. However, your suggestion kind of works, but I can't, for example, reference the data.frame with the new name:

assign(gear_subset, mtcars_subset)
head(gear_subset)

解决方案

The truth is that objects in R don't have names per-se. There exists different kinds of environments, including a global one for every process. These environments have lists of names, that point to various objects. Two different names can point to the same object. This is best explained to my knowledge in the environments chapter of Hadley Wickhams Advanced R book http://adv-r.had.co.nz/Environments.html

So there is no way to change a name of a data frame, because there is nothing to change.

But you can make a new name (like newname) point to the same object (in your case a data frame object) as an given name (like oldname) simply by doing:

   newname <- oldname

Note that if you change one of these variables a new copy will be made and the internal references will no longer be the same. This is due to R's "Copy on modify" semantics. See this post for an explanation: What exactly is copy-on-modify semantics in R, and where is the canonical source?

Hope that helps. I know the pain. Functional languages are different than procedural languages...

Of course it is possible to calculate a new name for a dataframe and register it in the environment with the assign command - and perhaps you are looking for this. However referring to it afterwards would be rather convoluted.

Example (assuming df is the dataframe in question):

   assign(  paste("city_stats", city_code, sep = ""), df )

As always see the help for assign for more information http://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html

Edit: In reply to your edit, and various comments around the problems with using eval(parse(...) you could parse the name like this:

head(get(gear_subset))

这篇关于如何更改数据框的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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