官术网_书友最值得收藏!

  • Learning Shiny
  • Hernán G. Resnizky
  • 204字
  • 2021-07-09 21:46:10

Variables in R

Unlike Java or C#, R enables total flexibility in the assignment of variables. This means that you can assign objects of different types to the same variable. This will cause overriding:

var1 <- 10
var1 <- "a string"

In this case, for instance, R will not throw an error for var1. In addition, there is no need to pre-declare the class of the variable.

The assignment of variables in R can be done in the following three ways:

  • <- or ->: These arrows assign the corresponding value to a variable. However, the first alternative is more common:
    var1 <- 10
    10 -> var1
    
  • =: This is similar to <- or ->.

    Tip

    As in most programming languages, it is important to keep in mind that whenever a comparison is needed, == must be used.

  • assign(): This is a function with the name of the variable as its first parameter and the value as its second:
    > assign("var1",6)
    

For conventional variable assignments, there is no need to use this function. However, it becomes particularly useful if dynamic naming of a variable is needed (a function could eventually be used to determine the name of a variable in the first argument of the function) or, if needed, a variable can be assigned to another environment (this issue will not be covered in this book, for further information about this, visit http://adv-r.had.co.nz/Environments.html).

R is case sensitive. This means that upper and lower case are relevant. For instance, the var1 and Var1 variables are semantically two completely different objects.

主站蜘蛛池模板: 潮安县| 罗田县| 潞西市| 新化县| 巴彦淖尔市| 读书| 定南县| 西城区| 文成县| 郁南县| 永顺县| 邛崃市| 博湖县| 保德县| 太仆寺旗| 屏南县| 彝良县| 平果县| 嘉峪关市| 甘肃省| 永安市| 福建省| 浪卡子县| 河西区| 南涧| 侯马市| 屏山县| 镇平县| 嘉祥县| 日喀则市| 莒南县| 巴彦县| 淮滨县| 宁强县| 黔西县| 大邑县| 保德县| 阿克陶县| 石首市| 弥渡县| 丹东市|