Looks like no one added any tags here yet for you.
citation()
To get some information about how to cite R
+-*l^
Basic arithmetic operations in R
← or →
Variable assignment
if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_compleX_, NA_character_
Reserved keywords that R needs to keep “safe” so that you can’t use them as variable names
sqrt()
Square root function
abs()
Absolute value function
round(x,# of decimal places) or round(x, digits = )
Round some value
c()
Combine function
Example: sales.by.month ← c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0)
[ ]
To get information out of a vector
Example: sales.by.month[2]
february.sales ← sales.by.month[2]
edit() or fix()
Edit variables
length(x=, )
To find out how many elements there are in a vector
Example: length(x=sales.by.month)
Output = 12
“word”
Storing text data
Example: greeting ← “hello”
Output: hello
nchar(x=)
To count the number of individual characters that make up a string
For example: nchar(x=greeting)
Output = 5
==
Equality operator to force R to make a “true or false” judgment
Example: 2+2==4
Output = 4
<, <=, >, >=, ==, !=
Operators, whereby < is less than, <= is less than or equal to, > is greater than, >= is greater than or equal to, == is equal to, and != is not equal to
!
not
|
or
&
and
TRUE (or T)
True
FALSE (or F)
False
[( )]
To extract multiple elements of a vector
For example: sales.by.month[ c(2,3,4) ]
x:x
Shorthand for c()
For example: 2:8
Output = 2 3 4 5 6 7 8
q()
To quit R
#
To tell R to ignore everything else you’ve written on this line. Handy for commenting
print()
To get R to display information
library(“your package of choice”)
To load a desired package
exists(“function within a package”)
To see if R knows about the existence of a function within a package
detach(“package:package of choice”, unload=TRUE)
To get rid of / to unload a package
install.packages()
To install packages
Example: install.packages(“psych”)
update.packages()
To update packages
lsr, psych, car, gplots, sciplot, foreign, effects, R.matlab, gdata, lmtest, reschape, compute.es, HistData, and multcomp, sem, ez, nlme, and lm34
Useful packages
objects() or ls() or ls.str() or who()
To view the objects in your data environment
Preferable to use the who() command
Example: objects(“packages:stats”)
rm( ,)
To remove variables
getwd()
To get the current working directory
setwd(“C:/Users/liam”)
To specify the working directory
list.files()
To list the contents of the directory
load(file=”the file you are looking for”)
To load a data file
Example: load(“chapek9.Rdata)
setwd(“../data”) and load(“the file you want”)
Easier way is to change the working directory first and then load the file
read.csv
To read a csv file
(data file) ← read.csv(file = “data file.csv”)
To import a CSV file
Example: books ← read.csv(file = ”booksales.csv”)
save.image(file = “desired file”)
To save all of the variables in your workspace into the data file
Example: save.image(file = ”myfile.Rdata”)
save(variable1, variable2, file = “file name”)
To save some (but not all) of your variables
Example: save(data, handy, file = “myfile.Rdata”)
The data for the variable junky is not being saved here
save(file = “file name”, list = list of variables you intend to save)
To save variables. Perhaps easier than the other save option
Inf, NaN, NA, NULL
Special values that may appear in situations where you were expecting a number
Inf
Infinity
Example: 1 / 0
Output = Inf
NaN
“Not a number”
Example: 0 / 0
Output = NaN
NA
“Not available”. Usually when there are missing values
NULL
The variable genuinely has no value whatsoever
names(variable name) ← c(“value1”, “value2”, value3”, “value4”)
To assign names to each element of a variable
Example: names(profit) ← c(“Q1”, “Q2”, “Q3”, “Q4”)
class(x)
A “high level” classification to capture psychologically or statistically meaningful distinctions
Example: x ← “hello world”
class(x)
Output = “character”
Example 2: x ← TRUE
class(x)
Output = “logical”
Example 3: x ← 100
class(x)
Output = “numeric”
mode(x)
The format of the information that a variable stores
type(x)
To see the distinction between integer data, double precision numeric, etc.)
group ← as.factor(group)
To group a variable by its factors
Example: group ← c(1, 1, 1, 2, 2, 2, 3, 3, 3)
group ← as.factor(group)
group
Output: 1 1 1 2 2 2 3 3 3
Levels: 1 2 3
data.frame(var1, var2, var3, var4)
To convert your variables into a data frame
Example: experiment1 ← data.frame(age, gender, group, score)
$
To extract / pull out the contents of a data frame you’re interested in
Example: experiment1$score
Outpurt = 12 10 11 15 16 14 25 21 29
who(expand = TRUE)
To expand any data frames that you’ve got in the workspace
~
To specify a formula
Example1: formula1 ← out ~ pred
Example2: formula2 ← out ~ pred1 + pred2
Example3: formula3 ← out ~ pred1 * pred2
Example4: formula 4 ← ~ var1 + var2
print.default(x)
(Come back to)
summary(object=variable)
To get a summary of your data
Example1: summary(object=afl.margins)
Example2: summary(chapek9)
plot()
To plot your data
?
To get help for a function
Example1: ?load
help(“load”)
Example2: ??load
help.search(“load”)
help.request
To request help from the R help mailing list
sum(variable)
To calculate the sum of a variable
Example1: sum(afl.margins)
Output = 6213
Example2: sum(afl.margins[1:5]) / 5
Output = 183
mean(x=variable)
To calculate the mean of a variable
Example1: mean(x=afl.margins)
Output = 35.30
Example2: mean(afl.margins[1:5])
Output = 36.6
sort(x=variable)
Puts the data points in numerical order
median(x=variable)
To calculate the median of a variable
Example: median(x=afl.margins)
mean(x=variable, trim=number to be entered)
To trim/”drop” the extreme values (outliers) on either side of the mean
Example1: mean(x=dataset, trim=.1)
Output = 5.5
Example2: mean(x=afl.margins, trim = .05)
Output = 33.75
table(dataset)
To create a frequency table of the dataset
Example: xtab.3d ← table(speaker, utterance, dan.awake)
xtab.3d
modeOf(variable)
To calculate the mode of a variable
Example: modeOf(x=afl.finalists)
Output = “Geelong”
maxFreq()
To calculate the modal frequency
Example: maxFreq(x=afl.finalists)
Output = 39
max(variable)
To get the maximum value of a variable
Example: max(afl.margins)
min(variable)
To get the minimum value of a variable
Example: min(afl.margins)
range(variable)
To get the range of a variable
Example: range(afl.margins)
Output = 0 116
quantile(x=variable, probs=number to be entered)
To calculate the percentiles/quantiles of a variable. The median of a set is its 50th quantile/percentile
Example1: quantile(x=afl.margins, probs=.5)
Output = 50%
30.5
Example2: quantile(x=afl.margins, probs=c(.25, .75))
Output = 25% 75%
12.75 50.50
IQR(x=variable)
To calculate the interquartile range (25%-75%) of a variable
Example: IQR(x = afl.margins)
aad(x)
To calculate the mean (average) absolute deviation of a variable
Example: aad(x)
var(x)
To calculate the variance of a variable
sd(variable)
To calculate the standard deviation of a variable
mad(x=variable, constant=1)
To calculate the median absolute deviation
Example: mad(x=afl.margins, constant=1)
skew(x=variable)
To calculate the skewness of a variable
Example: skew(x = afl.margins)
kurtosi(x=variable)
To calculate the kurtosis of a variable
Example: kurtosi(x=afl.margins)
as.character(variable)
To convert something to a character vector
Example: f2 ← as.character(afl.finalists)
summary(object=f2)
Output = Length 400 Class character Mode character
var, n, mean, sd, median, trimmed, mad, min, max, range, skew, kurtosis, se
The descriptive statistics included in the summary()
describeBy(x=variable1, group=variable1$variable2)
Very similar to the describe() function, except that it has an additional argument called group which specifies a grouping variable
Example: describeBy(x = clin.trial, group = clin.trial$therapy)
by(data=dataset, INDICES=dataset$variable, FUN=summary)
The command groups the dataset by the variable and applies the summary function to each subset of data
Example: by(data=clin.trial, INDICES=clin.trial$therapy, FUN=summary)
aggregate(formula = var1 ~ var2 + var3, data = dataset, FUN = mean)
This command calculates the mean of var1 for each combination of var2 and var3 groups in the dataset
Example1: aggregate(formula = mood.gain ~ drug + therapy, data = clin.trial, FUN = mean)
Example2: aggregate(mood.gain ~ drug, clin.trial, sd)
cor(x = dataset$var1, y = dataset$var2))
To calculate correlations
cor(x = dataset)
To calculate all correlations of a dataset
Example: cor(parenthood)
cor(dataset$var1, dataset$var2, method = “spearman”)
This command calculates the Spearman correlation coefficient between var1 and var2 in the dataset to measure their monotonic relationship
Example: cor(effort$hours, effort$grade, method = “spearman”)
correlate(dataset)
Automatically ignores variables in a data frame, making it more convenient for computing correlations only between numeric variables without needing manual data preprocessing
Example: correlate(work)
correlate(x, corr.method = “spearman”)
The same as correlate(x), but now species which correlation method to use
na.rm=TRUE
To ignore missing (NA) values when performing calculations
Example: mean(x = partial, na.rm = TRUE)
cor(datatset, use “complete.obs”)
To ensure that only rows with no missing values across all variables are used in the correlation calculation
Example: cor(parenthood2, use = “complete.obs”)
cor(dataset, use = “pairwise.complete.obs”)
To compute correlations using all available data for each pair of variables, ignoring missing values only for the specific variables being correlated rather than removing entire rows
Example: cor(parenthood2, use = “pairwise.complete.obs”)
plot.default()
To make a basic plot
main = “title of plot”
To specify title of plot
sub = “subtitle of plot”
To specify subtitle of plot
xlab = “x-axis label”
To specify x-axis label
ylab = “y-axis label”
To specify y-axis
font.main, font.sub, font.lab, font.axis
To specify font style of plot, where 1 = plain text, 2 = boldface, 3 = italic, 4 = bold italic