- R Object-oriented Programming
- Kelly Black
- 203字
- 2021-08-05 17:40:03
Assignment
The R environment is an interactive shell. Commands are entered using the keyboard, and the environment should feel familiar to anyone used to MATLAB or the Python interactive interpreter. To assign a value to a variable, you can usually use the = symbol in the same way as these other interpreters. The difference with R, however, is that there are other ways to assign a variable, and their behavior depends on the context.
Another way to assign a value to a variable is to use the <-
symbols (sometimes called operators). At first glance, it seems odd to have different ways to assign a value, but we will see that variables can be saved in different environments. The same name may be used in different environments, and the name can be ambiguous. We will adopt the use of the <-
operator in this text because it is the most common operator, and it is also the least likely to cause confusion in different contexts.
The R environment manages memory and variable names dynamically. To create a new variable, simply assign a value to it, as follows:
> a <- 6
> a
[1] 6
A variable has a scope, and the meaning of a variable name can vary depending on the context. For example, if you refer to a variable within a function (think subroutine) or after attaching a dataset, then there may be multiple variables in the workspace with the same name. The R environment maintains a search path to determine which variable to use, and we will discuss these details as they arise.
The <-
operator for the assignment will work in any context while the =
operator only works for complete expressions. Another option is to use the <<-
operator. The advantage of the <<-
operator is that it instructs the R environment to search parent environments to see whether the variable already exists. In some contexts, within a function for example, the <-
operator will create a new variable; however, the <<-
operator will make use of an existing variable outside of the function if it is found.
Another way to assign variables is to use the ->
and ->>
operators. These operators are similar to those given previously. The only difference is that they reverse the direction of assignment, as follows:
> 14.5 -> a > 1/12.0 ->> b > a [1] 14.5 > b [1] 0.08333333
- 數(shù)據(jù)庫(kù)原理及應(yīng)用教程(第4版)(微課版)
- 復(fù)雜性思考:復(fù)雜性科學(xué)和計(jì)算模型(原書(shū)第2版)
- Python數(shù)據(jù)分析入門:從數(shù)據(jù)獲取到可視化
- 虛擬化與云計(jì)算
- 云計(jì)算服務(wù)保障體系
- Live Longer with AI
- 數(shù)據(jù)庫(kù)設(shè)計(jì)與應(yīng)用(SQL Server 2014)(第二版)
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 活用數(shù)據(jù):驅(qū)動(dòng)業(yè)務(wù)的數(shù)據(jù)分析實(shí)戰(zhàn)
- Deep Learning with R for Beginners
- PostgreSQL高可用實(shí)戰(zhàn)
- AndEngine for Android Game Development Cookbook
- 區(qū)塊鏈應(yīng)用開(kāi)發(fā)指南:業(yè)務(wù)場(chǎng)景剖析與實(shí)戰(zhàn)
- 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)精粹
- 數(shù)據(jù)挖掘算法實(shí)踐與案例詳解