R Strings - gizotso/R GitHub Wiki
as.character(), is.character()
-
str = "North Face": [1] "North Face" -
str = c("tic", "tac toe 3D"): [1] "tic" "tac toe 3D"-
length(str): nb elements ## [1] 2 -nchar(str): nb char in each string ## [1] 3 10
-
Special char vectors : letters, LETTERS
> letters
[1] "a" "b" "c" "d" "e" ... "y" "z"-
cat: concatenate and print -
paste: concatenate vectors after converting to character
c('North', 'Face') ##[1] "North" "bar"
paste('North', 'Face') ##[1] "North Face"
paste0('North', 'Face') ##[1] "NorthFace"
paste('North', 'Face', sep='') ##[1] "NorthFace"
> s = c('North', 'Face')
[1] "North" "Face"
> paste(x, collapse='-')
[1] "North-Face"
paste("Today is", date()) ##[1] "Today is Wed Sep 13 20:40:11 2017"
paste(1:12) # same as as.character(1:12)
paste('file',c(1:5), sep='_')
[1] "file_1" "file_2" "file_3" "file_4" "file_5"
y = paste("y", 1:5, sep="")
[1] "y1" "y2" "y3" "y4" "y5"# join a vector into a string
paste(letters, collapse='')
[1] "abcdefghijklmnopqrstuvwxyz"
x = 1; y = 2
toString(c(x,y))
[1] "1, 2"
iteration= 4
cat('iteration= ', i, '\n')
iteration= 4
wd=getwd()
cat("Current working dir: ", wd)
-
print: -
sprintf: C-style formatting function -
message: same formatting that Error/Warning message
sprintf("Current working dir: %s", wd)
[1] "Current working dir: C:/Users/family/Documents"
cat(sprintf("Current working dir: %s\n", wd))
Current working dir: C:/Users/family/Documents
print(paste("Current working dir: ", wd))
[1] "Current working dir: C:/Users/family/Documents
strsplit("foo, bar",", ") # returns a list
[[1]]
[1] "foo" "bar"
unlist(strsplit("foo, bar",", "))
[1] "foo" "bar"
strsplit("a.b.c", "[.]") #returns a list
# [[1]]
# [1] "a" "b" "c"
unlist(strsplit("a.b.c", "[.]")) ##[1] "a" "b" "c"
txt = " blue
red
x"
## [1] " blue\nred\nx"
unlist(strsplit(txt, "[\n]"))
##[1] " blue" "red" "x"
substring('hello world', 7, 11) # start pos, <end pos>
[1] "world"
substring('hello world',1, 1:3) # vectorial substring
[1] "h" "he" "hel"
substring("hello",2,3) ##[1] "el"
substring("Hello",1:3,3) ##[1] "Hel" "el" "l"
s=substring("Hello", 1:5, 1:5) ##[1] "H" "e" "l" "l" "o" s[2] -> [1] "e"
Substitutions : sub et gsub (global: all occurences)
s = 'un pour tous et tous pour un'
sub('un',1,s)
[1] "1 pour tous et tous pour un"
gsub('un',1,s)
[1] "1 pour tous et tous pour 1"
-
http://www.inside-r.org/r-doc/base/chartr
toupper(),tolower()
> chartr("abcd", "1234", 'a cat')
[1] "1 31t"
cit <- "She said: \"Double quotes can be included in R’s strings.\""
## [1] "She said: \"Double quotes can be included in R’s strings.\""
cat(cit, "\n")
## She said: "Double quotes can be included in R’s strings."
cit <- 'Double quotes " delimitate R\'s strings.'
## [1] "Double quotes \" delimitate R's strings."
cat(cit)
## Double quotes " delimitate R's strings.
quote(expr) quote simply returns its argument. The argument is not evaluated and can be any R expression
x = 2
quote(x+2) ## x + 2 mode:"call"
deparse(quote(x+2)) ## [1] "x + 2"
e = quote(`foo bar`) ## `foo bar` mode:"name"
deparse(e) ## [1] "foo bar"
deparse(e, control = "all") ## [1] "quote(foo bar)"
e = quote(`foo bar` + 1) ## `foo bar` + 1 mode:"call"
deparse(e) ## [1] "foo bar + 1"
deparse(e, control = "all") ## [1] "quote(foo bar)"