使用get()和替换函数 [英] Using get() with replacement functions

查看:295
本文介绍了使用get()和替换函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #创建简单数据框
assign(df ,data.frame(P = runif(5),Q = runif(5),R = runif(5)))

#返回给定字符向量的数据框
get (df)
PQR
1 0.17396222 0.90994676 0.90590685
2 0.33860092 0.98078739 0.38058921
3 0.80751402 0.93229290 0.82853094
4 0.05460417 0.55448507 0.01605027
5 0.04250316 0.03808318 0.40678270
$ b $#返回df
colnames的列名(get(df))
[1]PQR

$使用替换函数...
colnames(get(df))< - c(S,T,U)
colnames中的错误(get (df))<-c(S,T,U):
赋值目标扩展为非语言对象
pre>

我想要
A)想要知道替换函数为什么不能以得到()

我的问题是我有很多对象,在循环中创建(使用玩具示例),如下所示: assign(paste(对象,i,sep =。),rnorm(1000,i)),其中 i 是一个向量,例如 i <-1:1000 ,然后我希望能够为循环中的每个对象指定名称(例如来自不同的向量),但名称(get(paste(Object,i,sep =。))< - someNewName 不能像上面的例子那样工作。



但是 get(paste(Object,i,sep =。))会返回名称(或 NULL )。



感谢!

解决方案

为了理解为什么这不起作用,您需要了解 colnames< - 的作用。像每个函数看起来像修改对象一样,它实际上是修改一个副本,所以概念上 colnames(x)< - y 会被扩展为:

  copy <-x 
colnames(copy)<-y
x < - copy

如果以通常的方式调用替换运算符,可以更简洁地写入:

 x<  - `colnames< -`(x,y)

所以你的例子变成了

  get(x)<  - `colnames< -`(get(x ),y)

右边是有效的R,但是整个命令不是,因为你不能给函数的结果赋值:

  x < -  1 
get( x)< - 2
#get(x)中的错误< - 2:
#赋值目标扩展为非语言对象


Can anyone explain to me why the following example occurs?

#Create simple dataframe
assign( "df" , data.frame( P = runif(5) , Q = runif(5) , R = runif(5) ) ) 

#Return the dataframe from the given character vector
get( "df" ) 
            P          Q          R
1  0.17396222 0.90994676 0.90590685
2  0.33860092 0.98078739 0.38058921
3  0.80751402 0.93229290 0.82853094
4  0.05460417 0.55448507 0.01605027
5  0.04250316 0.03808318 0.40678270

#Return the column names of df
colnames( get( "df" ) )
[1] "P" "Q" "R"

#But using a replacement function...
colnames( get( "df" ) ) <- c( "S" , "T" , "U" ) 
    Error in colnames(get("df")) <- c("S", "T", "U") : 
    target of assignment expands to non-language object

I'd A) like to know why the replacement functions won't work in this way with get()?

And b) if there is some way to work around this, given my problem which I outline below;

My problem is that I have many objects, created (using a toy example) in a loop, something like this: assign( paste( "Object" , i , sep = "." ) , rnorm(1000 , i) ), where i is a vector, say i <- 1:1000 and then I would like to be able to assign names (for instance from a different vector) to each object in the loop, but names( get( paste( "Object" , i , sep = "." ) ) <- someNewName doesn't work as in the example above.

But get( paste( "Object" , i , sep = "." ) ) does return the names (or NULL) of those objects.

Thanks!

解决方案

To understand why this doesn't work, you need to understand what colnames<- does. Like every function in that looks like it's modifying an object, it's actually modifying a copy, so conceptually colnames(x) <- y gets expanded to:

copy <- x
colnames(copy) <- y
x <- copy

which can be written a little more compactly if you call the replacement operator in the usual way:

x <- `colnames<-`(x, y)

So your example becomes

get("x") <- `colnames<-`(get("x"), y)

The right side is valid R, but the command as a whole is not, because you can't assign something to the result of a function:

x <- 1
get("x") <- 2
# Error in get("x") <- 2 : 
#  target of assignment expands to non-language object

这篇关于使用get()和替换函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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