Here’s a bunch of interesting R stuff I put together for learning and fun. Bookmark or re-download periodically as I may keep updating this page with more attractions.
See which packages and dataframes are currently attached
Manually edit an R object, say, a function
See, save and load command history
Read a table with header
See first/last parts of an R object
See manuals and references of R itself
Check if an object exists
See all loaded packages with versions
Copy from clipboard
# Windows
a <- writeClipboard(as.character(factor.name))
a <- writeClipboard(as.character(numeric.variable))
# Mac
a <- read.table(pipe("pbpaste"), header = TRUE)
Shut down computer after a wait time
E-mail from within R
library(mailR)
send.mail(
from="your@gmail.com", to="another@gmail.com",
subject="Hi there", body="How are you?",
smtp=list(host.name = "smtp.gmail.com", port = 25,
user.name = "yourusername", passwd = "yourpass", ssl = T),
authenticate = T, send=T, attach.files="link-to-file")
Create animated graphics in R
Various tolerance limits in R
Make all possible concatenations out of two character vectors
## [1] "200-2" "400-2" "600-2" "200-3" "400-3" "600-3" "200-4" "400-4" "600-4"
Make all possible concatenations out of multiple(>=2) character vectors
a = c("a","b")
b = c("A","B","C")
d = c("X","Y")
e = c("1","2","3")
out = as.character( Reduce( function(X,Y) outer(X,Y,FUN = paste, sep = "."),
list(a, b, d, e) ) )
out
## [1] "a.A.X.1" "b.A.X.1" "a.B.X.1" "b.B.X.1" "a.C.X.1" "b.C.X.1" "a.A.Y.1" "b.A.Y.1" "a.B.Y.1"
## [10] "b.B.Y.1" "a.C.Y.1" "b.C.Y.1" "a.A.X.2" "b.A.X.2" "a.B.X.2" "b.B.X.2" "a.C.X.2" "b.C.X.2"
## [19] "a.A.Y.2" "b.A.Y.2" "a.B.Y.2" "b.B.Y.2" "a.C.Y.2" "b.C.Y.2" "a.A.X.3" "b.A.X.3" "a.B.X.3"
## [28] "b.B.X.3" "a.C.X.3" "b.C.X.3" "a.A.Y.3" "b.A.Y.3" "a.B.Y.3" "b.B.Y.3" "a.C.Y.3" "b.C.Y.3"
List all functions used in an R script
Print to a text file
# option 1
fileConn <- file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)
# option 2
sink("output.txt")
cat("hello")
cat("\n")
cat("world")
sink()
See size of an R object
x = rnorm(10000)
# Provides an estimate of the memory that is being used to store an R object
object.size(x) # size in byte
## 80048 bytes
## 80.05 kB
# They won't match for sequences because object.size does not correctly account
# for shared references and will return too large sizes. E.g., for sequences
# created with :, R versions >= 3.5.0 only store the first and last numbers.
object.size(1:2)
## 56 bytes
## 80048 bytes
## 680 B
## 680 B
Get size of the workspace
workspace.size <- function()
{
ws <- sum(sapply(ls(envir=globalenv()), function(x)object.size(get(x))))
class(ws) <- "object_size"
ws
}
workspace.size()
See internal functions which are not loaded
See all names or all variables used in an expression
## [1] "sin" "+" "x" "y"
## [1] "x" "y"
Change case of letters in a string
## [1] "hello"
## [1] "HELLO"
Treat a vector as a one-column matrix to get dimensions
## [1] 1
## [1] 3
## NULL
## [1] 0
Make duplicates in a character vector unique
## [1] "ab" "cd" "ef" "ab.1"
Gather a bunch of objects in a list
Don’t print out numbers in scientific format, keep decimal form
Create an empty list of given length
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
Show progress by increasing a counter (instead of printing i sequencially)
You cannot set the following names
_hi <- 2 #names cannot start with _
.hi <- 2 #names cannot start with .
?Reserved #names cannot be a reserved word like if, else, etc.
Dataset related to U.S. states
How much memory is currently used by R?
Define named vector in single line
Avoiding else
Avoiding multiple if-else
func <- function(x) {
if (x == "a") {
"option 1"
} else if (x == "b") {
"option 2"
} else if (x == "c") {
"option 3"
} else {
stop("Invalid `x` value")
}
}
# a more compact way to write the above
func <- function(x) {
switch(x,
a = "option 1",
b = "option 2",
c = "option 3",
stop("Invalid `x` value")
)
}
Evaluate a multivariate function over a grid of values
## [1] 12 15 18
Get list of all functions currently in global environment
Get list of all objects of an attached package
Find the index of the first/last vector element satisfying a condition
Piping
## [1] 14
## [1] 14
Function arguments can be defined interdependently
## [1] 9
Function arguments can be defined in terms of body variables
Set a variable in the global environment from within a function environment
## [1] 9
## [1] 5
Intersect all possible combinations of list elements
l <- list(A=c("one", "two", "three", "four"), B=c("one", "two"), C=c("two", "four", "five", "six"), D=c("six", "seven"))
l
## $A
## [1] "one" "two" "three" "four"
##
## $B
## [1] "one" "two"
##
## $C
## [1] "two" "four" "five" "six"
##
## $D
## [1] "six" "seven"
## ind
## ind A B C D
## A 4 2 2 0
## B 2 2 1 0
## C 2 1 4 1
## D 0 0 1 2
Turn a matrix into a list of its columns
Combinations of n elements of a vector, taken m at a time
\[\\[1in]\]
\[\\[3in]\]