R-在函数内部赋值 [英] R- assign inside a function

查看:279
本文介绍了R-在函数内部赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个输入另一个数据框名称的函数创建一个数据框并重新整理数据。当我需要在输入表名称转换后命名输出时,问题就出现了。
我的代码到目前为止:

pre $ tablaXYZ< -data.frame(a = seq(1,2,1 )'X1'= seq(2,3,1),'X2'= seq(3,4,1))
rownames(tablaXYZ)< -c('X1','X2')

并且函数I worte是:

<$ p (tbl)){
library(reshape2)
texto< -deparse(substitute(tbl))
tbl2< -melt(tbl, id.vars = rownames(tbl))
texto2< -substr(texto,4,nchar(texto))
colnames(tbl2)< -c('userId','movieId',texto2)
tblName<< -paste0('df',texto2)
print(paste('tblName',tblName,''))
assign(tblName,tbl2)
return (assign(tblName,tbl2))
}

运行时:

  creaMelts(tablaXYZ)

我得到:

 tblName dflaXYZ

因此,考虑在t中给定的对象中返回的赋值作品他第二名的名字存储在第一名的对象中,如:

 `m < - 'hola'` 
assign(m,tablaXYZ)

然后我要求m,我得到:

 hola

但如果我要求hola,我得到表格:

  hola 
a X1 X2
X1 1 2 3
X2 2 3 4

我以为我会有数据框架命名为dflaXYZ与值:

  userId movieId laXYZ NA 
1 2 3 a 1
2 3 4 a 2

但是,如果我运行:

  dflaXYZ 

我只得到:

 dflaXYZ

如果我运行例如:

  a< -creaMelts(tablaXYZ)

然后询问a,我得到所需的表格。

  userId movieId laXYZ NA 
1 2 3 a 1
2 3 4 a 2

我需要获取同一个表,但在这种情况下命名为 dflaXYZ (删除前3个字母并添加 df 到输入表名)。

解决方案

我们需要使用 envir code>参数用于 assign 来访问函数外部的环境。

  creaMelts< -function(tbl){
library(reshape2)
texto< -deparse(substitute(tbl))
tbl2< -melt(tbl,id.vars = rownames (tbl))
texto2< -substr(texto,4,nchar(texto))
colnames(tbl2)< -c('userId','movieId',texto2)
tblName< ;< -paste0('df',texto2)
print(paste('tblName',tblName,''))
assign(tblName,tbl2,envir = parent.frame())

}

creaMelts(tablaXYZ)
dflaXYZ
#userId movieId laXYZ NA
#1 2 3 a 1
#2 3 4 a 2


I need to create a data frame from a function entering another data frame name and reshaping the data. The issue comes when I need to named the output after a transformation of the input table name. My code so far is:

tablaXYZ<-data.frame(a=seq(1,2,1), 'X1'=seq(2,3,1), 'X2'=seq(3,4,1))
rownames(tablaXYZ)<-c('X1', 'X2')

And the function I worte is:

creaMelts<-function(tbl){
    library(reshape2)
    texto<-deparse(substitute(tbl))
    tbl2<-melt(tbl, id.vars=rownames(tbl))
    texto2<-substr(texto,4,nchar(texto))
    colnames(tbl2)<-c('userId','movieId', texto2)
    tblName<<-paste0('df', texto2)
    print(paste('tblName', tblName, ' '))
    assign(tblName, tbl2)
    return(assign(tblName, tbl2))
}

When I run:

creaMelts(tablaXYZ)

I get:

"tblName dflaXYZ  "

So, considering "assign" works returning in the object given in the 2nd place with the name stored in the object in the 1st place, like:

 `m<-'hola'`
 assign(m, tablaXYZ)

And then I ask for m, I get:

"hola"

But if I ask for "hola" I get the table:

hola
   a X1 X2
X1 1  2  3
X2 2  3  4

I thought I would have a data frame named "dflaXYZ" with the values:

  userId movieId laXYZ NA
1      2       3     a  1
2      3       4     a  2

But if I run:

 dflaXYZ

I only get:

"dflaXYZ"

and if I run for example:

a<-creaMelts(tablaXYZ)

And then ask for "a", I get the desired table.

  userId movieId laXYZ NA
1      2       3     a  1
2      3       4     a  2

I need to get the same table but named in this case dflaXYZ (deleting the 1st 3 letters and adding df to the input table name).

解决方案

We need to use envir argument for the assign to access the environment outside the function.

creaMelts<-function(tbl){
   library(reshape2)
   texto<-deparse(substitute(tbl))
   tbl2<-melt(tbl, id.vars=rownames(tbl))
   texto2<-substr(texto,4,nchar(texto))
   colnames(tbl2)<-c('userId','movieId', texto2)
   tblName<<-paste0('df', texto2)
    print(paste('tblName', tblName, ' '))
    assign(tblName, tbl2, envir = parent.frame() )

 }

creaMelts(tablaXYZ)
dflaXYZ
#  userId movieId laXYZ NA
#1      2       3     a  1
#2      3       4     a  2

这篇关于R-在函数内部赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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