R语言读书笔记-02

R语言读书笔记-02

It’s all very well calculating things, but most of the time we want to store the results for reuse. We can assign a (local) variable using either <- or =, though for historical reasons, <- is preferred:
一般我们使用“<-”符号来给变量赋值

> x <- c(1,2,3,4,5)
> x
[1] 1 2 3 4 5
> y <- 1:5
> y
[1] 1 2 3 4 5
> z = 5:10
> z
[1]  5  6  7  8  9 10
更加详细的计算
> x + 2 * y - 3
[1]  0  3  6  9 12

Notice that we didn’t have to declare what types of variables x and y were going to be before we assigned them (unlike in most compiled languages). In fact, we couldn’t have declared the type, since no such concept exists in R.
在R中没有给变量声明类型的原则,也就是说R中不需要给变量声明类型。
Variable names can contain letters, numbers, dots, and underscores, but they can’t start with a number, or a dot followed by a number (since that looks too much like a number). Reserved words like “if ” and “for” are not allowed. In some locales, non-ASCII letters are allowed, but for code portability it is better to stick to “a” to “z” (and “A” to “Z”). The help page ?make.names gives precise details about what is and isn’t allowed.
The spaces around the assignment operators aren’t compulsory, but they help readability, especially with <-, so we can easily distinguish assignment from less than:
在给变量起名字的时候,不要取一些超出正常逻辑的名字,变量的名称组成可以是数字、字母、点号(.)、可以是下划线(_),不要以数字开头或者点号后边接数字这种情况,当然关键字也不可以作为变量名。至于其他的语言体系的命名规范,这里不赘述。在变量前后插入空格虽然不是必须得动作,但是在”<-“前后要加上,要不然分不清楚小于与赋值的区别,当然加上空格也会提升代码的可读性。
We can also do global assignment using <<-.
使用”<<-“进行全局声明
There is one more method of variable assignment, via the assign function. It is much less common than the other methods, but very occasionally it is useful to have a function syntax for assigning variables. Local (“normal”) assignment takes two arguments—the name of the variable to assign to and the value you want to give it:
assign(“my_local_variable”, 9 ^ 3 + 10 ^ 3)
Global assignment (like the <<- operator does) takes an extra argument:
assign(“my_global_variable”, 1 ^ 3 + 12 ^ 3, globalenv())
赋值的方法不止一种,有一种是不经常使用的就是assign()函数,但偶尔也是很有用的。比如局部变量的赋值,可以使用assign()函数,第一个变量写变量名,第二个写变量值;如果是全局变量,要多加一个变量,第三个变量是globalenv()。
Using the assign function makes your code less readable compared to <-, so you should use it sparingly. It occasionally makes things easier in some advanced programming cases involving environments, but if your code is filled with calls to assign, you are probably doing something wrong.
Also note that the assign function doesn’t check its first argument to see if it is a valid variable name: it always just creates it.
在一些高级编程中使用assign函数对环境进行设置,但是如果程序中出现大量的assign函数,那么你的代码肯定哪里出了问题。还有一个问题值得注意的是,对于第一个参数的命名规范,assign函数是不会检查的,只会去创建它。
Notice that when you assign a variable, you don’t see the value that has been given to it. To see what value a variable contains, simply type its name at the command prompt to print it:
查看一个变量的值,只需要在命令行中键入变量的名字即可。
Under some systems, for example running R from a Linux terminal, you may have to explicitly call the print function to see the value. In this case, type print(x).
但是根据平台的不同,可能查看的方式也不太一样,例如运行在LINUX平台上的R,要使用print函数来输出
If you want to assign a value and print it all in one line, you have two possibilities. Firstly, you can put multiple statements on one line by separating them with a semicolon, ;. Secondly, you can wrap the assignment in parentheses, (). In the following examples, rnorm generates random numbers from a normal distribution, and rlnorm generates them from a lognormal distribution:
假如你想给变量分配值,但是又希望在一行中输出,那么现有两种方法可选,第一种,就是在一行中使用分号将多条语句进行分割,第二种就是使用括号把它括起来。在下面的例子中,rnorm()函数可以随机生成一些具有服从正态分布特征的数值,也可以生成一些服从逻辑正态分布特征的数值。

> z <- rnorm(5)
> z
[1] -0.3727211 -1.2429035  0.5266008 -1.8795326 -0.5661726
> z <- rnorm(5) ; z
[1] -1.0077836  0.2346902 -1.0159042  0.9884582 -1.4384939
> (zz <- rnorm(5))
[1]  0.7764133  1.3931782  1.6976818 -0.2963533  1.0964890
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值