The article is about viewing the source code of R Method. There are different ways to view the source code of an R method or function. It will help to know how the function is working.
Table of Contents
Source Code of R Method (Internal Functions)
If you want to see the source code of R method or the internal function (functions from base packages), just type the name of the function at the R prompt such as;
rowMeans
Functions or Methods from the S3 Class System
For S3 classes, the methods function can be used to list the methods for a particular generic function or class.
methods(predict)
Note that “Non-Visible functions are asterisked” means that the function is not exported from its package’s namespace.
One can still view its source code via the ::: function such as
stats:::predict.lm
or by using getAnywhere()function, such as
getAnywhere(predict.lm)
Note that the getAnywhere() function is useful as you don’t need to know from which package the function or method comes from.
Functions or Methods from the S4 Class System
The S4 system is a newer method dispatch system and is an alternative to the S3 system. The package ‘Matrix’ is an example of S4 function.
library(Matrix)
chol2inv
The output already offers a lot of information. The standardGeneric is an indicator of an S4 function. The method to see defined S4 methods is to use showMethods(chol2inv), that is;
showMethods(chol2inv)
The getMethod can be used to see the source code of one of the methods, such as,
getMethod ("chol2inv", "diagonalMatrix")
View Source Code of Unexported Functions
In the case of unexported functions such as ts.union, .cbindts, and .makeNamesTs from the stats namespace, one can view the source code of these unexported functions using the ::: operator or getAnywhere() function, for example;
In R, plot symbols (Greek Letters in R Plot) are used to represent data points in scatter plots and other types of plots. These symbols can be customized to suit your preferences, making your data visualization more effective and aesthetically pleasing graphs or plots in R.
Table of Contents
Common Plot Symbols in R
R Language uses numeric values to represent different symbols. The following is a list of the most commonly used plot symbols and their corresponding numbers:
Symbol
Code
Description
Circle
1
Solid circle (default)
Square
15
Solid square
Triangle
2
Solid triangle
Diamond
18
Solid diamond
Plus Sign
3
Plus sign
X
4
X marks the spot
Open Circle
1
Circle with no fill
Open Square
0
Square with no fill
Open Triangle
17
Triangle with no fill
Introduction to R Plot Symbols (Greek Letters)
The post is about writing (Greek Letters in) R plot symbols, their labels, and the title of the plots. There are two main ways to include Greek letters in your R plot labels (axis labels, title, legend):
Using the expression Function This is the recommended approach as it provides more flexibility and control over the formatting of the Greek letters and mathematical expressions.
Using raw Greek letter Codes This method is less common and requires memorizing the character codes for each Greek letter.
Question: How can one include Greek letters (symbols) in R plot labels? Answer: Greek letters or symbols can be included in titles and labels of a graph using the expression command. Following are some examples
Note that in these examples, random data is generated from a normal distribution. You can use your own data set to produce graphs that have symbols or Greek letters in their labels or titles.
Greek Letters in R Plot
The following are a few examples of writing Greek letters in R plot.
Example 1: Draw Histogram
mycoef <- rnorm (1000)
hist(mycoef, main = expression(beta) )
where beta in expression is the Greek letter (symbol) of $\beta$. A histogram similar to the following will be produced.
The post is about how to import data using read.table() function in R. You will also learn what a file path is and how to get and set the working directory in the R language. The read.table() function in R is a powerful tool for importing tabular data, typically from text files, into the R environment. The read.table function converts the tabular data from a flat-file format into a more usable data structure called the data frame.
Table of Contents
Question: How can I check my Working Directory so that I would be able to import my data in R? Answer: To find the working directory, the command getwd() can be used, that is
getwd()
Question: How can I change the working directory to my path? Answer: Use function setwd(), that is
data <- read.table(file,
header = FALSE,
sep = "",
dec = ".",
stringsAsFactors = FALSE)
Key Paramters of read.table in R
Key Parameters Explained
Parameter
Description
Default
Common Values
The first row as column names
File path/URL
–
“data.txt”, “https://example.com/data.csv”
header
First row as column names
FALSE
TRUE/FALSE
sep
Field separator
“” (whitespace)
“,”, “\t”, “;”
dec
Decimal separator
“.”
“,”, “.”
na.strings
Missing value codes
“NA”
“N/A”, “”, “999”
stringsAsFactors
Convert strings to factors
FALSE
TRUE/FALSE
colClasses
Specify column types
NA
“numeric”, “character”, “factor”
nrows
Number of rows to read
-1 (all)
100, 1000
skip
Lines to skip at start
0
1, 5
Import Data using read.table Function in R
Question: I have a data set stored in text format (ASCII) that contains rectangular data. How can I read this data in tabular form? I have already set my working directory. Answer: As the data is already in a directory set as the working directory, use the following command to import the data using read.table() command.
The mydata is a named object that will have data from the file “data.dat” or “data.txt” in data frame format. Each variable in the data file will be named by default V1, V2,…
Question: How can this stored data be accessed? Answer: To access the stored data, write the data frame object name (“mydata”) with the $ sign and the name of the variable. That is,
mydata$V1
mydata$V2
mydata["V1"]
mydata[ , 1]
Question: My data file has variable names in the first row of the data file. In the previous question, the variable names were V1, V2, V3, … How can I get the actual names of the variables stored in the first row of the data.dat file? Answer: Instead of reading a data file with default values of arguments, use
read.table("data.dat", header = TRUE)
Question: I want to read a data file that is not stored in the working directory. Answer: To access the data file that is not stored in the working directory, provide a complete path of the file, such as.
Note that read.table() is used to read the data from external files that normally have a special form:
The first line of the file should have a name for each variable in the data frame. However, if the first row does not contain the name of a variable, then the header argument should not be set to FALSE.
Each additional line of the file has its first item a row label and the values for each variable.
In R it is strongly suggested that variables need to be held in the data frame. For this purpose,e read.table() function in R can be used. For further details about read.table() function use,
help(read.table)
Important Arguments of read.table Function:
file: (required argument) it is used to specify the path to the file one wants to read.
header: A logical value (TRUE or FALSE) indicating whether the first line of the file contains column names. The default value is set to FALSE.
sep: The separator that segregates values between columns. The default is set to white space. One can specify other delimiters like commas (“,”) or tabs (“\t”).
as.is: A vector of logical values or column indices specifying which columns to read as characters and prevent conversion to numeric or factors.
colClasses: A vector specifying the data type for each column. Useful for ensuring specific data formats during import. This can be useful to ensure the data is read in the correct format (e.g., numeric, character).
read.table vs Similar Functions
Function
Best For
Speed
Packages
read.table()
General text files
Slow
Base R
read.csv()
CSV files
Slow
Base R
fread()
Large files
Very Fast
data.table
read_delim()
Tidyverse workflow
Fast
readr
read_excel()
Excel files
Medium
readxl
Best Practices when using read.table Function in R
Always specify column types (colClasses) for large files
Handle missing values explicitly with na.strings
Use faster alternatives (fread, readr) for files >100MB
Check encoding for international character sets
Validate imports with str(), summary(), and head()
Note that
While read.table() is rarely the fastest option today, it remains the most flexible text file importer in base R. For modern workflows, consider data.table::fread() or readr::read_delim() for better performance, but understanding read.table() is essential for handling special cases and legacy code.