Getting to know the base commands in R

infoart.ca
2 min readJun 11, 2023

--

Photo by Rajan Alwan on Unsplash

The base commands in R refer to the built-in functions that are available in the language without having to install additional packages.

These are pre-existing functions that perform simple yet fundamental operations, such as loading datasets, doing basic cleaning tasks, and calculating mean or median.

Here are some of the most commonly used base commands in R:

ls(): Lists objects in the environment
args(): Shows arguments of a function
str(): Shows the structure of an object
summary(): Shows a summary of an object
head(): Shows first few rows of a data frame
tail(): Shows last few rows of a data frame
dim(): Shows dimensions of an object
length(): Shows length of a vector
names(): Shows names of an object
class(): Shows class of an object
attr(): Shows attributes of an object
read.csv(): Reads a CSV file into R
write.csv(): Writes an R object to a CSV file
load(): Loads an R object from a file
save(): Saves an R object to a file
rbind(): Binds rows of data frames
cbind(): Binds columns of data frames
subset(): Subsets a data frame
apply(): Applies a function over margins of an array
t(): Transposes a matrix
diag(): Extracts or replaces the diagonal of a matrix
sort(): Sorts a vector or data frame
mean(): Calculates the mean of a vector
sd(): Calculates the standard deviation of a vector

While these commands can handle a lot of handy tasks, they make up only the tip of the iceberg. So let’s now see how to print all commands in the base R:

ls("package:base", all.names = T)

[990] "prod"
[991] "prop.table"
[992] "proportions"
[993] "provideDimnames"
[994] "psigamma"
[995] "pushBack"
[996] "pushBackLength"
[997] "q"
[998] "qr"
[999] "qr.coef"
[1000] "qr.default"
[ reached getOption("max.print") -- omitted 377 entries ]

Way too many names to paste here. So let’s just print those that start with a specific letter:

> ls("package:base", all.names = T, pattern = "^m")
[1] "make.names" "make.unique" "makeActiveBinding"
[4] "mapply" "margin.table" "marginSums"
[7] "mat.or.vec" "match" "match.arg"
[10] "match.call" "match.fun" "matrix"
[13] "max" "max.col" "mean"
[16] "mean.Date" "mean.default" "mean.difftime"
[19] "mean.POSIXct" "mean.POSIXlt" "mem.maxNSize"
[22] "mem.maxVSize" "memCompress" "memDecompress"
[25] "memory.profile" "merge" "merge.data.frame"
[28] "merge.default" "message" "mget"
[31] "min" "missing" "mode"
[34] "mode<-" "month.abb" "month.name"
[37] "months" "months.Date" "months.POSIXt"
[40] "mostattributes<-" "mtfrm" "mtfrm.default"

43 functions starting with m alone. Long live R!

--

--

infoart.ca
infoart.ca

Written by infoart.ca

Center for Social Capital & Environmental Research | Posts by Bishwajit Ghose, BI consultant and lecturer at the University of Ottawa

No responses yet