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

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.

主站蜘蛛池模板: 屯昌县| 阜南县| 南开区| 遵义县| 鄂托克旗| 平湖市| 遂昌县| 沂水县| 青浦区| 洪江市| 上饶县| 连江县| 通渭县| 自治县| 德昌县| 怀化市| 兴化市| 肇东市| 宜黄县| 谢通门县| 当涂县| 隆昌县| 绥中县| 屏山县| 额尔古纳市| 团风县| 北安市| 泗阳县| 三门县| 株洲县| 六枝特区| 临江市| 平度市| 观塘区| 河北省| 兴城市| 新巴尔虎右旗| 蒙自县| 尉犁县| 娱乐| 紫阳县|