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

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.

主站蜘蛛池模板: 河南省| 伊吾县| 奎屯市| 仁化县| 罗定市| 腾冲县| 昔阳县| 巧家县| 营山县| 航空| 普宁市| 洛扎县| 特克斯县| 忻州市| 鲁甸县| 江达县| 招远市| 屏东市| 湘乡市| 遂平县| 玛多县| 神农架林区| 礼泉县| 长泰县| 桑植县| 温泉县| 寻甸| 固安县| 靖宇县| 南康市| 广昌县| 潞城市| 星座| 宁城县| 浦北县| 南涧| 边坝县| 琼中| 金沙县| 兰溪市| 汾阳市|