The Big R-Book. Philippe J. S. De Brouwer
Чтение книги онлайн.
Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 34
![The Big R-Book - Philippe J. S. De Brouwer The Big R-Book - Philippe J. S. De Brouwer](/cover_pre848614.jpg)
format()
Function use for format()
format(x, trim = FALSE, digits = NULL, nsmall = 0L, justify = c(“left”, “right”, “centre”, “none”), width = NULL, na.encode = TRUE, scientific = NA, big.mark = “”, big.interval = 3L, small.mark = “”, small.interval = 5L, decimal.mark = getOption(“OutDec”), zero.print = NULL, drop0trailing = FALSE, …)
x is the vector input.
digits is the total number of digits displayed.
nsmall is the minimum number of digits to the right of the decimal point.
scientific is set to TRUE to display scientific notation.
width is the minimum width to be displayed by padding blanks in the beginning.
justify is the display of the string to left, right or center.
Formatting examples
a<-format(100000000,big.mark=” “, nsmall=3, width=20, scientific=FALSE, justify=“r”) print(a) ## [1] “ 100 000 000.000”
More information about the format-function can be obtained via ?format
or help(format)
.
Other string functions
nchar(): returns the number of characters in a string
nchar()
toupper(): puts the string in uppercase
toupper()
tolower(): puts the string in lowercase
tolower()
substring(x,first,last): returnsa substring from x starting with the “first” and ending with the “last”
substring()
strsplit(x,split): splitthe elements of a vector into substrings according to matches of a substring “split.”there is also a family of search functions: grep(),
strsplit()
grep()
grepl()
,
grepl()
regexpr()
,
regexpr()
gregexpr()
,
gregexpr()
and regexec()
regexec()
that supply powerful search and replace capabilities.
sub()
sub()
will replace the first of all matches and gsub()
gsub()
will replace all matches.
4.4 Operators
While we already encountered operators in previous sections when we introduced the data types, here we give a systematic overview of operators on base types.
operators
4.4.1 Arithmetic Operators
arithmetic – operators
Arithmetic operators act on each element of an object individually.
operator – arithmetic
v1 <- c(2,4,6,8) v2 <- c(1,2,3,5) v1 + v2 # addition ## [1] 3 6 9 13 v1 - v2 # subtraction ## [1] 1 2 3 3 v1 * v2 # multiplication ## [1] 2 8 18 40 v1 / v2 # division ## [1] 2.0 2.0 2.0 1.6 v1 %% v2 # remainder of division ## [1] 0 0 0 3 v1 %/% v2 # round(v1/v2 -0.5) ## [1] 2 2 2 1 v1 ∧ v2 # v1 to the power of v2 ## [1] 2 16 216 32768
addition
substraction
multiplication
division
power
While the result of the sum will not surprise anyone, the result of the multiplicationmight come as a surprise for users of matrix oriented software such as Mathlab or Octave for example. In R an operations is always element per element – unless explicitly requested. For example, the dot-product can be obtained as follows.
v1 %*% v2 ## [,1] ## [1,] ss 68
4.4.2 Relational Operators
Relational Operators compare vectors element by element
relational operators
operator – relational
v1 <- c(8,6,3,2) v2 <- c(1,2,3,5) v1 > v2 # bigger than ## [1] TRUE TRUE FALSE FALSE v1