6 R Syntax
翻译的一个初稿,翻译速度较量快、较量粗拙,但愿各人看完挑挑短处,提提意见!6 R Syntax\chapter{R语法}This chapter contains an overview of R syntax. It’s not intended to be a formal orcomplete description of all valid syntax in R, but just a readable description of validR expressions.本章将对R语法举办概述。但并不规划生硬可能繁缛地描写R语言中的所有牵涉语法的内容,只是略述一下部门有用的R语言表达式。It is possible to write almost any R expression as a function call. However, it’s confusing readinglots of embedded function calls, so R provides some special syntaxto make code for common operations more readable.*R语言中,所有表达式都可以写成函数呼吁。然而,大量的嵌套式的函数呼吁很容易引起阅读上的杂乱。基于此,R语言提供了一些非凡的语法以使得执行普通操纵的R代码更具有可读性\footnote{你可以将R代码写成一系列嵌套大量函数呼吁的函数呼吁,这样看起来很像LISP代码,包罗所有的括号。顺便说一下,S语言的灵感来自于LISP,其借用了LISP编译器所用的很大都据布局和执行技能。}。Constants常量Let’s start by looking at constants. Constants are the basic building blocks for dataobjects in R: numbers, character values, and symbols.先看一看常量。常量是R中数据工具:数字,字符和标记等的基石。Numeric Vectors数值向量Numbers are interpreted literally in R:R中的数字观念很容易领略:\begin{Verbatim}> 1.1[1] 1.1> 2[1] 2> 2^1023[1] 8.988466e+307\end{Verbatim}You may specify values in hexadecimal notation by prefixing them with 0x:在数字前面加上前缀0x可以用来暗示六进制的数字。\begin{Verbatim}> 0x1[1] 1> 0xFFFF[1] 65535\end{Verbatim}* You could write R code as a series of function calls with lots of function calls. This would look alot like LISP code, with all the parentheses. Incidentally, the S language was inspired by LISP anduses many of the same data structures and evaluation techniques that are used by LISP interpreters.你可以将R代码写成一系列嵌套大量函数呼吁的函数呼吁,这样看起来很像LISP代码,包罗所有的括号。顺便说一下,S语言的灵感来自于LISP,其借用了LISP编译器所用的很大都据布局和执行技能。%By default, numbers in R expressions are interpreted as double-precision floating-%point numbers, even when you enter simple integers:默认环境下,R表达式的数字被表明为双精度浮点数,纵然输入的是极为简朴的整数:\begin{Verbatim}> typeof(1)[1] “double”\end{Verbatim}%If you really want an integer, you can use the sequence notation or the as function%to obtain an integer:假如你想要一个整数,你可以利用序列标记可能as函数来得到:\begin{Verbatim}> typeof(1:1)[1] “integer”> typeof(as(1,”integer”))[1] “integer”\end{Verbatim}%The sequence operator a:b will return a %vector of integers between a and b.%To combine an arbitrary set of numbers %into a vector, use the c function:序列运算符a:b将返回一个a、b之间的整数向量。c函数可以将任意一组数字归并为一个向量:\begin{Verbatim}> v <- c(173,12,1.12312,-93)\end{Verbatim}%R allows a lot of flexibility when entering %numbers. However, there is a limit to the%size and precision of numbers that R can %represent:R输入数字时具有很大的机动性。然而,R对所输入数字的巨细和精度有所限制:%# limits of precision\begin{Vertavim}#精度的限制> (2^1023 + 1) == 2^1023[1] TRUE%# limits of size#巨细的限制> 2^1024[1] Inf\end{Verbatim}%In practice, this is rarely a problem. Most R users will load data from other sources%on a computer (like a database) that also can’t represent very large numbers.在实践中,这险些不算是一个问题。大大都用户从计较机上面的其他来历(如数据库)载入数据,而这些数据源凡是也无法暗示超大的数据。%R also supports complex numbers. %Complex values are written as real_part%+imaginary_parti. For example: