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

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.

主站蜘蛛池模板: 广昌县| 洪泽县| 仙居县| 大厂| 当阳市| 玉龙| 长丰县| 陆河县| 龙里县| 徐水县| 金湖县| 宁国市| 沙洋县| 商城县| 上虞市| 扶风县| 鹤山市| 洪洞县| 康保县| 卢湾区| 衡阳市| 利辛县| 阿坝| 咸阳市| 光泽县| 宜春市| 永靖县| 闽清县| 元谋县| 吴旗县| 丹巴县| 青铜峡市| 枣庄市| 泌阳县| 文昌市| 新兴县| 凤台县| 邯郸县| 灵武市| 昭通市| 衢州市|