R language characters function and other utility functions

A character handler
functions described
NCHAR (x)
of x in number of characters
substr (x, start, stop)
to extract or alternatively in a character substring vector
x <- "abcdef"
substr (x, 2,4) returns value "BCD"
substr (X, 2,4) <- "22222" (X becomes "a22222ef")
function grep ()
grep (pattern, X, ignore.case = FALSE, Fixed = FALSE, FALSE value = , perl = FALSE)
Looking element contains a specific string (pattern parameter) in the vector x, returns to its index in x;
returns the corresponding element when the value = TRUE
if fixed = FALSE, then the pattern is a regular expression type, when the fixed = TRUE, then the pattern is a string of text
grep ( "a", c ( "b", "a", "c"), fixed = TRUE) # 2 returns a value of

Function Sub ()
Sub (pattern, replacement, x, ignore.case = FALSE, FALSE = fixed)
search pattern in x, and the replacement to replace, if fixed = FALSE, then the pattern is a regular expression, when the fixed = TRUE, the pattern is a text string
example
Note "\ s" is used to find a regular expression blank. The reason for using "\ s" is "\ s" is an escape character in the R
sub ( "\ s", " .", "Hello There") returns a value Hello.There
function strsplit () function
strsplit (x , split, fixed = FALSE)
dividing the character elements in the vector x at the split. If fixed = FALSE, then the pattern is a regular expression, when the fixed = TRUE, then the pattern is a string literal
example
y <- strsplit ( "abc" , "") # 1 returns a component comprising three elements content list "A", "B", "C"
Y
function, connection function
paste (..., sep = "" )
connection string delimiter is On Sep
Paste ( "X",. 1:. 3, On Sep = "" ) returns a value of C ( "X1", "X2", "X3")
Paste ( "X",. 1:. 3, On Sep = "M") returns a value of c ( "xM1", "xM2 ",



tolower()

Second, other useful functions
required length of the object x
length (x)
to generate a sequence represented by the interval
seq (from, to, by)
the example
seq (1,10,2) # 13579 returns

rep (x, n)
x-repeated n times
cut (x, n)
the continuous variable x is divided into two levels with n factors, practical options odered_result = TRUE to create an orderly factor, cut () function as well as other parameters, re-encoding of this function is useful column
pretty (x, n)
to create aesthetic point of division. By selecting a value rounded n + 1 equally spaced, to a continuous variable x is divided into n sections. When drawing used
cat (..., file = "myfile ", append = FALSE)
connection object in ..., and outputs it to the screen or a file
example
firstname <- c ( "Jane" , "jack")
when the output cat after an object is connected, it sends each object separated by a space
CAT ( "the Hello", FirstName, "\ n-")
\ n-escape character is represented by a new line, \ T to tabs' single quotes , \ b is a backspace. ? By Quotes can learn more about the escape character
example
name <- "Bob"
CAT ( "the Hello", name,. "\ B \ the n-", "Is is not R", "\ t", "CREAT \? n ")

发布了39 篇原创文章 · 获赞 11 · 访问量 1万+

Guess you like

Origin blog.csdn.net/weixin_42712867/article/details/95578333