# Steem 和 R 语言：跟大鹏学 R 语言（3）变量和向量

in cn •  9 months ago

``````exp(1)  # 计算 e
``````
``````## [1] 2.718282
``````

``````e = exp(1)
``````

``````e <- exp(1)
``````

``````exp(1) -> e
``````

``````e
``````
``````## [1] 2.718282
``````

``````x <- round(e)^2
x
``````
``````## [1] 9
``````

:) 可以是一个或多个字母，如 ‘e’, ‘x’, ‘mydata’；

:) 可以包括数字，如 ‘a1’, ‘a2’;

:) 可以包括句点和下划线，如 ‘temperature_air’, ‘humidity.max’。

:-( 不可以包含空格，如 ‘my data’；

:-( 不可以用数字或小数开头，如 ‘2x’，‘.3y’；

:-( 不可以用中文。

``````    不要给你的矩阵变量取名为“矩阵”。你会给你的狗狗起名字叫“狗狗”吗？
``````

— Barry Rowlingson, October 2004

``````x <- c(61, 45, 55, 46, 56, 79, 86, 57, 56, 56, 57, 71)
x
``````
``````##  [1] 61 45 55 46 56 79 86 57 56 56 57 71
``````

``````x[4]
``````
``````## [1] 46
``````

``````y <- co2 # 转存
y[10] # 看看第十个数据
``````
``````## [1] 313.18

``````

R 支持向量运算。试试输入：

``````x + 100

``````
``````##  [1] 161 145 155 146 156 179 186 157 156 156 157 171

``````

x 里的每一个数都加上了 100 。这就是向量运算的好处：简单的代码，避免逐个计算。

Sort Order:

Congratulations! This post has been selected by @justyy as today's (2018-04-11) 【Good Posts You May Miss】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^

·

·

R语言居然还支持箭头赋值