android r中的变量
Variables in R are the same as the notion of variables in any other programming language. Variable is a name we assign for the storage space location that stores our data. A variable in R can be of any datatype, simple or complex. There are certain rules and conventions when working with variables in R.
R中的变量与任何其他编程语言中的变量概念相同。 变量是我们为存储数据的存储空间位置分配的名称。 R中的变量可以是任何数据类型,简单或复杂。 在R中使用变量时,有某些规则和约定。
R中的变量–命名规则 (Variables in R – Naming Rules)
The name of a variable is known as an identifier. Not every string can be an identifier in R.
变量的名称称为标识符。 并非每个字符串都可以是R中的标识符。
- Variables in R are case sensitive. The names
Car
,car
andCAR
are all treated as different variables in spite of the same spelling.
R中的变量区分大小写 。 尽管拼写相同,但名称Car
,car
和CAR
都被视为不同的变量。
- Variables can never begin with symbols or numbers.
1car
and&car
are not valid variable names.
变量永远不能以符号或数字开头 。1car
和&car
不是有效的变量名称。
- Variable names can begin with a period (
.
). However, if a name starts with a period, it must be followed by a letter instead of a number.
变量名可以以句点 (.
) 开头 。 但是,如果名称以句点开头,则必须在其后加上字母而不是数字。
- For defining complex names, period or underscore can be used as the seperator. For example,
a.one
anda_one
are both valid variable names.
为了定义复杂的名称,可以使用句点或下划线作为分隔符。 例如,a.one
和a_one
都是有效的变量名。
- However, since underscore was used as an assignment operator in earlier versions of R, a period is preferred over underscore.
但是,由于下划线在R的早期版本中用作赋值运算符,因此, 下划线优先于下划线 。
- Variable names can never contain a blank space. 变量名绝不能包含空格 。
变量分配 (Variable Assignment)
In the previous post on data types in R, we have introduced the assignment operator in R <-
. This is knows as the gets operator. When we write a statement a <- b
, it can be verbally expressed as, a gets the value of b.
在上一篇有关R中的数据类型的文章中 ,我们在R <-
引入了赋值运算符。 这就是gets运算符。 当我们编写语句a <- b
,可以将其口头表达为a获得b的值 。
Never put a space between the less than and hyphen in our gets operator. The statement a <- 10
is an assignment statement, whereas a < -10
is interpreted as a less than minus 10, resulting in a Boolean output.
切勿在我们的gets运算符中的小于和连字符之间放置空格。 语句a <- 10
是赋值语句,而a < -10
被解释为小于负10,从而导致布尔输出。
In addition to the gets operator, there is also the right assignment operator, that is a reverse of gets ->
. The statement 10 -> a
is equivalent to a <- 10
.
除了gets运算符外,还有一个正确的赋值运算符,它与gets- ->
的相反。 语句10 -> a
等效于a <- 10
。
Also, the equals sign can be used for assignment of variables in R just like C or C++. The statement a = b
is a valid one.
同样, 等号可以像C或C ++一样用于R中的变量赋值。 语句a = b
是有效的语句。
一些有用的变量提示 (Some Useful Tips with Variables)
- When working with complex projects, it sometimes gets tedious to remember all the variable names. Therefore you can type in
ls()
in the console to get a list of all active variables. 在处理复杂项目时,记住所有变量名有时会很麻烦。 因此,您可以在控制台中键入ls()
以获取所有活动变量的列表。 - To remove a specific variable from the environment the command is
rm(<variablename>)
. The space gets freed and can be reassigned to another variable now. To remove a variable named x, simply type inrm(x)
. 要从环境中删除特定变量,命令为rm(<variablename>)
。 该空间已释放,现在可以将其重新分配给另一个变量。 要删除名为x的变量,只需键入rm(x)
。 - The code snippet below displays the ls() utility. All the variables and function names in the enivornment get displayed. 下面的代码片段显示了ls()实用程序。 显示环境中的所有变量和函数名称。
> ls()
[1] "a" "a_one" "average" "b" "c" "code"
[7] "division" "elfunction" "fs" "myname" "myname2" "myvector"
[13] "name
- Suppose that we need to remove a variable named average, it can be done by typing in
rm(average)
. Usingls
again, we see that the variable in no longer in the environment. 假设我们需要删除一个名为average的变量,可以通过输入rm(average)
来完成。 再次使用ls
,我们看到该变量不再存在于环境中。
> rm(average)
> ls()
[1] "a" "a_one" "b" "c" "code" "division"
[7] "elfunction" "fs" "myname" "myname2" "myvector" "name"
android r中的变量