R语言 变量的范围

R语言 变量的范围

在R语言中,变量是存储数据值的容器。它们是对内存中某个对象的引用或指针,这意味着每当一个变量被分配到一个实例时,它就会被映射到该实例。R语言中的变量可以存储一个向量、一组向量或许多R对象的组合。

例子:

# R program to demonstrate 
# variable assignment  
  
# Assignment using equal operator
var1 = c(0, 1, 2, 3)
print(var1)
  
# Assignment using leftward operator
var2 <- c("Python", "R")
print(var2)
  
# A Vector Assignment
a = c(1, 2, 3, 4)
print(a)
b = c("Debi", "Sandeep", "Subham", "Shiba")
print(b)
  
# A group of vectors Assignment using list
c = list(a, b)
print(c)
R

输出

[1] 0 1 2 3
[1] "Python" "R"     
[1] 1 2 3 4
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  
[[1]]
[1] 1 2 3 4

[[2]]
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  
R

变量的命名规则

  • R中的变量名必须是 字母数字 字符,但 下划线( ‘_’)和 句号( ‘.’)除外,这些特殊字符可以在变量名中使用。
  • 变量名必须总是以字母开头。
  • 其他特殊字符如(‘!’, ‘@’, ‘#’, ‘$’)不允许出现在变量名中。

例子

# R program to demonstrate 
# rules for naming the variables 
  
# Correct naming
b2 = 7 
  
# Correct naming
Amiya_Profession = "Student" 
  
# Correct naming
Amiya.Profession = "Student" 
  
# Wrong naming
2b = 7 
  
# Wrong naming
Amiya@Profession = "Student" 
R

上述代码在执行时将产生一个错误,因为变量的命名有误。

Error: unexpected symbol in "2b"
Execution halted
R

变量的范围

我们可以找到一个变量并在需要时访问它的位置称为变量的范围。主要有两种类型的变量作用域。

  • 全局变量: 全局变量是那些在程序执行过程中一直存在的变量。它可以从程序的任何部分改变和访问。
  • 局部变量: 局部变量是那些只存在于程序的某个部分(如函数)的变量,当函数调用结束时,这些变量会被释放。

R语言中变量的范围

全局变量

顾名思义,全局变量可以从程序的任何部分访问。

  • 它们在程序的整个生命周期中都是可用的。
  • 它们可以在程序中的任何地方声明,在所有的函数或块之外。
  • 声明全局变量: 全局变量通常在所有的函数和块之外声明。它们可以从程序的任何部分被访问。
# R program to illustrate  
# usage of global variables  
   
# global variable 
global = 5 
   
# global variable accessed from 
# within a function 
display = function(){
  print(global)
} 
display() 
  
# changing value of global variable  
global = 10 
display()
R

输出

[1] 5
[1] 10
R

在上面的代码中,变量 ‘global ‘被声明在所有函数之外的程序顶部,所以它是一个全局变量,可以从程序的任何地方访问或更新。

本地变量

在一个函数或程序块中定义的变量被称为是这些函数的局部变量。

  • 本地变量不存在于声明它们的块之外,也就是说,它们不能在该块之外被访问或使用。
  • 声明局部变量: 局部变量是在一个块内声明的。

例子

# R program to illustrate  
# usage of local variables  
  
func = function(){
  # this variable is local to the 
  # function func() and cannot be  
  # accessed outside this function 
  age = 18    
} 
  
print(age)
R

输出

Error in print(age) : object 'age' not found
R

上面的程序显示一个错误:”没有找到对象’age'”。变量age是在函数 “func() “中声明的,所以它是该函数的局部,对该函数以外的程序部分不可见。

为了纠正上述错误,我们必须只显示函数 “func() “中的变量age的值。

例子

# R program to illustrate  
# usage of local variables  
  
func = function(){
  # this variable is local to the 
  # function func() and cannot be  
  # accessed outside this function 
  age = 18    
  print(age)
} 
  
cat("Age is:\n")
func()
R

输出

Age is:
[1] 18
R

访问全局变量

全局变量可以从代码中的任何地方访问,这与局部变量不同,局部变量的范围仅限于创建它们的代码块。

例子

f = function() {
   # a is a local variable here
   a <-1
}
f()
  
# Can't access outside the function
print(a) # This'll give error
R

输出

Error in print(a) : object 'a' not found
R

在上面的代码中,我们看到我们无法在函数之外访问变量 “a”,因为它是由一个 **赋值运算符( <-) **分配的,它使 “a “成为一个局部变量。为了对全局变量进行赋值,我们使用了 **超级赋值运算符( <<-) **。

超级赋值运算符是如何工作的?
当在一个函数中使用这个运算符时,它会在父环境框架中搜索变量,如果没有找到,它会继续搜索下一级,直到它到达全局环境。如果仍然没有找到变量,它就会在全局层面上创建和分配。

例子

# R program to illustrate
# Scope of variables
  
outer_function = function(){
  inner_function = function(){
    # Note that "<<-" operator here
    # makes a as global variable
    a <<- 10
    print(a)
  }
  inner_function()
  print(a)
}
outer_function()
  
# Can access outside the function
print(a)
R

输出

[1] 10
[1] 10
[1] 10
R

当在 inner_function() 中遇到语句 “a <<- 10 “时,它在 outer_function() 环境中寻找变量 “a”。当搜索失败时,它会在 R_GlobalEnv 中搜索 由于 “a “没有在这个全局环境中定义,它被创建并分配到那里,现在它被 inner_function()outer_function() 引用并打印。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册