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

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.

主站蜘蛛池模板: 南澳县| 兴业县| 阜康市| 平顶山市| 芷江| 清苑县| 沈阳市| 托克逊县| 东乡| 贡山| 屏东县| 德格县| 合肥市| 城市| 正镶白旗| 广灵县| 尼勒克县| 东安县| 旬阳县| 荆门市| 乳源| 宁化县| 和政县| 伊川县| 沙雅县| 兖州市| 盘山县| 施秉县| 扶余县| 通化县| 凭祥市| 宾川县| SHOW| 锡林郭勒盟| 焦作市| 仲巴县| 陵川县| 靖宇县| 阿克陶县| 阿城市| 桦南县|