print(nchar(c("I'm a little teapot","short and stout")))
print(nchar(c("I'm a little teapot", 14)))
print(nchar("I the only string"))
str_vector <- c("I'm a little teapot","short and stout",14,
FALSE)
print(toupper(str_vector))
print(tolower(str_vector))
substr
and substring
take in 3 arguments, any of which can be vectorssubstr(strings, start, end)
substring(strings, first, last)
substring
repeats the stringsprint(substr("Hello World",3,5))
print(substr("Hello World",1:3,1:3))
print(substring("Hello World",1:3,1:3))
print(substring("Hello World",c(1,2,3),c(1,2,3)))
print(substring("Hello World",5:20,10))
print(substring("Hello World",4,10:15))
str_vector <- c("I'm a little teapot","short and stout",14,FALSE)
print(substr(str_vector,2,1000L))
cat("\n")
print(substr(str_vector,1:5,1000L))
cat("\n")
print(substring(str_vector,1:5,1000L))
cat("\n")
print(substring(str_vector,1:15,1000))
strings_with_spaces <- c("I am a string",
"I am one too",
"This also has spaces")
print(strsplit(strings_with_spaces,split=' '))
strings_with_spaces <- c("I am a string",
"I am one too",
"This also has spaces")
print(strsplit(strings_with_spaces,split="\\s",perl=TRUE))
strings_with_spaces <- c("I am a string","I am one too","This also has spaces")
print(strsplit(strings_with_spaces,split="\\W",perl=TRUE))
strings_with_spaces <- c("I am a string",
"I am one too",
"This also has spaces")
idx <- grep('I',strings_with_spaces,perl=TRUE)
print(strings_with_spaces[idx])
grep('I',strings_with_spaces,perl=TRUE,ignore.case=TRUE)
grep('\\bI\\b',strings_with_spaces,perl=TRUE,ignore.case=TRUE)
grepl('\\bI\\b',strings_with_spaces,perl=TRUE,ignore.case=TRUE)
library(stringr)
print(str_length(str_vector))
print(str_sort(str_vector))
print(str_to_title(str_vector))
print(str_pad(str_vector,40))
str_vector <- c("\n\rI am a string\t\t",
"I am one\ntoo",
"This also has spaces")
print(str_trim(str_pad(str_vector,40)))
str_c(str_vector,",")
str_c(str_vector,collapse=", ")
str_detect(str_vector,'o')
str_count(str_vector,'o')
Sys.glob
(note the uppercase)print(Sys.glob("*.html"))
read_file
and write_file
functionslibrary(readr)
contents <- read_file("index.html")
print(contents)
print(str_extract_all(contents,'<a href=".*?">.*</a>'))
microbenchmark
library provides the microbenchmark
functionprofvis
packagelibrary(microbenchmark)
nums <- matrix(c(1:5000),nrow=100)
print(
microbenchmark(
colMeans(nums),
apply(nums,2,mean)
)
)
## Needs to be run in RStudio
library(profvis)
print(
profvis(
{
nums <- matrix(c(1:50000),nrow=100)
apply(nums,2,mean)
}
))
parallel
provides a mutlicore versions of mapply and lapply,mclapply
mcmapply
mclapply(vector,function,mc.cores=N_CORES)
mclapply(vector,function,axis,mc.cores=N_CORES)
library(parallel)
print(detectCores())
seed_strings <- c("asdf","ghhjk",'qerwet',
'uopi','zxcv','asdgf')
lots_of_strings <- rep(seed_strings,20000)
print(
microbenchmark(
lapply(lots_of_strings,str_length),
mclapply(lots_of_strings,str_length,mc.cores=7)
)
)
cl <- makeCluster(8)
print(
microbenchmark(
colMeans(nums),
apply(nums,2,mean),
parCapply(cl,nums,mean)
)
)
stopCluster(cl)
knitR
allows a document to be written in library(knitr)
knit('005-latex.Rtex')
lib
parameter to install.packages
library
functionR_LIBS_USER
in your shell before calling R