zoukankan      html  css  js  c++  java
  • R中的<-和=赋值符号的细致区别

    <-创建的变量的作用范围可以在整个顶层环境,而=仅仅在一个局部环境。
    但要<-创建的变量如果是在函数实参传递的时候创建的,其的作用范围可以在整个顶层环境,有一个前提条件:对应的形参在函数内部被用到了。

    (一)
    一般情况下我觉得使用<-合适,但当给函数参数传值,以及创建对象,对行/列/组件命名的时候用等号
    如:
    1. person <- list(name="payal", x=2,
    2.                 y=9, year=1990)
    3. person
    4. $name
    5. [1] "payal"
    6.  
    7. $x
    8. [1] 2
    9.  
    10. $y
    11. [1] 9
    12.  
    13. $year
    14. [1] 1990
    否则,如果写成:
    1. person <- list(name<-"payal", x=2,
    2.                y=9, year=1990)
    则运行完后,结果看起来一致,但在内存中多了一个name变量,且我们在顶层空间即可访问。
    而我们用的
    1. person <- list(name="payal", x=2,
    2.                y=9, year=1990)
    这些name,y,year就不能直接在顶层访问了,而需要通过$符号。

    (二)
    简单点说,=和<-这两个赋值操作符的区别在于其作用域。

    What’s the difference?
            The main difference between the two assignment operators is scope(作用范围). It’s easiest to see the difference with an example:
            ##Delete x (if it exists)
            > rm(x)
            > mean(x=1:10) #[1] 5.5
            > x #Error: object 'x' not found
            Here x is declared within the function’s scope of the function, so it doesn’t exist in the user workspace. Now, let’s run the same piece of code with using the <- operator:
     
            > mean(x <- 1:10)# [1] 5.5
            > x # [1] 1 2 3 4 5 6 7 8 9 10
            This time the x variable is declared within the user workspace.
            When does the assignment take place?
     (三)
    用到的时候才执行,这是R中的惰性求值原则决定的。
            In the code above, you may be tempted to thing that we “assign 1:10 to x, then calculate the mean.” This would be true for languages such as C, but it isn’t true in R. Consider the following function:
            > a <- 1
            > f <- function(a) return(TRUE)
            > f <- f(a <- a + 1); a
            [1] TRUE
            [1] 1
     
            Notice that the value of a hasn’t changed! In R, the value of a will only change if we need to evaluate the argument in the function. This can lead to unpredictable behaviour:
            > f <- function(a) if(runif(1)>0.5) TRUE else a
            > f(a <- a+1);a
            [1] 2
            > f(a <- a+1);a
            [1] TRUE
            [1] 2
            > f(a <- a+1);a
            [1] 3

    (四)
    让我仔细的举几个例子吧
    让我来告诉你什么时候会用到吧:
    1. 例子1
    2. > a <- 1
    3. > f <- function(a) return(TRUE)
    4. > f <- f(a <- a + 1);
    5. > a
    6. [1] 1
    7. > f
    8. [1] TRUE
    a <- a + 1这条语句并没有被执行。
    如上面的解释,这是因为,需要的时候,这个语句才会执行。
    我们看以下几个例子。
    1. 例子2
    2. > a <- 1
    3. > f <- function(a) return(1+2)
    4. > f <- f(a <- a + 1);
    5. > a
    6. [1] 1
    7. > f
    8. [1] 3
    9. > a <- 1
    10. > f <- function(a) {
    11. + 1+10
    12. + return(1+2)
    13. + }
    14. > f <- f(a <- a + 1);
    15. > a
    16. [1] 1
    17. > f
    18. [1] 3
    发现,a <- a + 1还是没有被执行
    注意,下面这个例子中,a <- a + 1被执行了
    1. 例子3
    2. > a <- 1
    3. > f <- function(a) {

    4. +   print(a)
    5. +   return(1+2)
    6. + }
    7. > f <- f(a <- a + 1);
    8. [1] 2
    9. > a
    10. [1] 2
    11. > f
    12. [1] 3
    为什么这个例子中,a <- a + 1被执行了呢,按照在函数中需要的时候才计算值的的逻辑,就是,我们的函数体中用到了这个形参a,所以对于的实参被计算了。
    还有一点,我们为什么说print中打印的是形参a呢?可以看如下代码:
    1. 例子4
    2. > a <- 1
    3. > f <- function(x) {
    4. +   print(a)
    5. +   return(1+2)
    6. + }
    7. > f <- f(a <- a + 1); 
    8. [1] 1
    9. > a
    10. [1] 1
    11. > f
    12. [1] 3
    看吧,a <- a + 1还是没执行了呢,所以说,例子3中的形参,本身作为一个局部变量,是会覆盖全局变量中的a的

    那么下面的代码为什么还是没执行a <- a + 1呢,因为a<-5这一句,是新生成了一个局部变量a。
    1. 例子5
    2. > f <- function(a) {
    3. +   a<-5
    4. +   return(1+2)
    5. + }
    6. > f <- f(a <- a + 1); 
    7. > a
    8. [1] 1
    9. > f
    10. [1] 3

    再来一个强力的佐证,下面的a<-a+5右侧的a就是形参中的a,所以a <- a + 1再一次被执行了
    1. 例子6
    2. > a <- 1
    3. > f <- function(a) {

    4. +   a<-a+5
    5. +   return(1+2)
    6. + }
    7. > f <- f(a <- a + 1);
    8. > a
    9. [1] 2
    10. > f
    11. [1] 3


    (五)
    再来验证下,一开始的结论,我们一开始的结论是<-创建的变量的作用范围可以在整个顶层环境,而=仅仅在一个局部环境。
    注意,运行下述例子之前,先清空内存中的a。
    1. 例子7
    2. > f <- function(a) {

    3. +   a<a+6
    4. +   return(1+2)
    5. + }
    6. > f <- f(a <- 1:5);
    7. > a
    8. [1] 1 2 3 4 5
    9. > f
    10. [1] 3
    这个单独的例子中,我们事先并没有创建a,但是由于a <- 1:5语句执行了,我们发现我们在函数外的环境,仍然能访问到a。
    (这是因为a+6用到了形参a,所以a <- 1:5执行了)

    注意,运行下述例子之前,先清空内存中的a。
    1. 例子8
    2. > f <- function(a) {
    3. + a<-6
    4. + return(1+2)
    5. + }
    6. > f <- f(a <- 1:5);
    7. > a
    8. Error: object 'a' not found
    9. > f
    10. [1] 3
    (同理,没用到a,所以a <- 1:5没执行)

    我们的结论是:
    <-创建的变量的作用范围可以在整个顶层环境,而=仅仅在一个局部环境。
    但要<-创建的变量如果是在函数实参传递的时候创建的,其的作用范围可以在整个顶层环境,有一个前提条件:对应的形参在函数内部被用到了。





  • 相关阅读:
    1.27
    1.25
    Representation Learning with Contrastive Predictive Coding
    Learning a Similarity Metric Discriminatively, with Application to Face Verification
    噪声对比估计(负样本采样)
    Certified Adversarial Robustness via Randomized Smoothing
    Certified Robustness to Adversarial Examples with Differential Privacy
    Dynamic Routing Between Capsules
    Defending Adversarial Attacks by Correcting logits
    Visualizing Data using t-SNE
  • 原文地址:https://www.cnblogs.com/xuanlvshu/p/5493222.html
Copyright © 2011-2022 走看看