获取访问的最后一个数据框的名称,并在后续代码中插入该名称
我想保存最后使用的数据框的名称,然后在向前移动的代码中使用该名称。 SAS通过%syslast
和_last _last _
系统变量提供此功能。有等效的吗?想做这样的事情:
mydf1 <- data.frame(x = c('my data'), y = c('more data'))
lastDF$newVarnames <- paste(lastDF$oldVarnames, "baseline", sep = "_") #do something to the mydf1 dataframe
mydf2 <- data.frame(x = c('my data'), y = c('more data'))
lastDF$newVarnames <- paste(lastDF$oldVarnames, "baseline", sep = "_") #do something to mydf2 dataframe
想要使用lastdf
的引用,而不是实际的数据帧名称。另外,想避免仅仅做
lastDF <- mydf1, mydf2, ...
I would like to save the name of the last dataframe used and then use that name in code moving forward. SAS provides this feature via %syslast
and _last_
system variables. Is there an R equivalent? Would like to do something like this:
mydf1 <- data.frame(x = c('my data'), y = c('more data'))
lastDF$newVarnames <- paste(lastDF$oldVarnames, "baseline", sep = "_") #do something to the mydf1 dataframe
mydf2 <- data.frame(x = c('my data'), y = c('more data'))
lastDF$newVarnames <- paste(lastDF$oldVarnames, "baseline", sep = "_") #do something to mydf2 dataframe
Want to use a reference like lastDF
instead of the actual dataframe name. Also, would like to avoid just doing
lastDF <- mydf1, mydf2, ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如注释中所述,此功能在R中不存在。最接近的东西是
.last.value
,它是最近运行命令的结果。基本r的
基本R方法将相同的代码应用于多个对象,将是应用自定义功能。您仍然需要指定对象的名称:
magrittr
%&gt;%
管道向前管道
%&gt;%
从magrittr
package允许允许我们要将data.frame()
的输出直接发送到dosomething()
(作为第一个参数),因此我们可以做一个不错的单线:在场 :用于全面性的数据修改
:一些来自其他编程语言的人用于定期修改变量(通过参考)。这在R中并不常见,但是使用
sign> nistion()
+deparse()
+替代()
:As mentioned in the comments, this feature does not exist in R. The closest thing is
.Last.value
, which holds the result of the most recently ran command.Base R
The base R approach to apply identical code to multiple objects, would be to apply a custom function. You'll still need to specify the object's name:
Magrittr
%>%
pipeThe forward pipe
%>%
from themagrittr
package allows us to send the output ofdata.frame()
directly todoSomething()
(as the first argument), so we can make a nice one-liner:In-place data modification
For comprehensiveness: Some people coming from other programming languages are used to in-place modification of variables (passing by reference). This is not commonplace in R, but it is possible with a little hack using
assign()
+deparse()
+substitute()
: