a <-“Hello” b <-“world” paste(a, b, sep = “, “) ## [1] “Hello, world” c <-“A ‘valid’ string” paste()
Note – Paste
In many cases we do not need anything between strings that are concatenated. We can of course supply an empty string as separator ( sep = “
), but it is also possible to use the custom function pate0()
:
paste0(12, ‘%’) ## [1] “12%”
past0()
format()
In many cases, it will be useful to format a date or number consistently and neatly in plot and tables. The function format()
is a great tool to start formatting.
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.
a <-format(100000000,big.mark=” “, nsmall=3, width=20, scientific=FALSE, justify=“r”) print(a) ## [1] “ 100 000 000.000”
Further information – format()
More information about the format-function can be obtained via ?format
or help(format)
.
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.
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
Warning – Element-wise operations in R
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 <v2 # smaller than## [1] FALSE FALSE FALSE TRUE v1 <=v2 # smaller or equal## [1] FALSE FALSE TRUE TRUE v1 >=v2 # bigger or equal## [1] TRUE TRUE TRUE FALSE v1 ==v2 # equal## [1] FALSE FALSE TRUE FALSE v1 !=v2 # not equal## [1] TRUE TRUE FALSE TRUE
bigger than
smaller than
bigger or equal
equal
not equal
Logical Operators combine vectors element by element. While logical operators can be applied directly on composite types, theymust be able to act on numeric, logical or complex types in order to produce understandable results.
operator – logical
v1 <- c(TRUE, TRUE, FALSE, FALSE) v2 <- c(TRUE, FALSE, FALSE, TRUE) v1 &v2 # and## [1] TRUE FALSE FALSE FALSE v1 |v2 # or## [1] TRUE TRUE FALSE TRUE !v1 # not## [1] FALSE FALSE TRUE TRUE v1 &&v2 # and applied to the first element## [1] TRUE v1 ||v2 # or applied to the first element## [1] TRUE v1 <- c(TRUE,FALSE,TRUE,FALSE,8,6 +3i, -2,, NA) class(v1) # v1 is a vector or complex numbers## [1] “complex” v2 <- c(TRUE) as.logical(v1) # coerce to logical (only 0 is FALSE)## [1] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE NA v1 &v2 ## [1] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE NA v1 |v2 ## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Note – Numeric equivalent and logical evalutation
Note that numbers different from zero are considered as TRUE, but only zero is considered as FALSE. Further, NA is implemented in a smartway. For example, in order to assess TRUE & NA
we need to know what the second element is, hence it will yield NA. However, TRUE | NA
will be true regardless what the second element is, hence R will show the result.
Читать дальше