R Statistics commands

studied byStudied by 0 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 322

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

323 Terms

1

citation()

To get some information about how to cite R

New cards
2

+-*l^

Basic arithmetic operations in R

New cards
3

← or →

Variable assignment

New cards
4

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

New cards
5

sqrt()

Square root function

New cards
6

abs()

Absolute value function

New cards
7

round(x,# of decimal places) or round(x, digits = )

Round some value

New cards
8

c()

Combine function

Example: sales.by.month ← c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0)

New cards
9

[ ]

To get information out of a vector

Example: sales.by.month[2]

february.sales ← sales.by.month[2]

New cards
10

edit() or fix()

Edit variables

New cards
11

length(x=, )

To find out how many elements there are in a vector

Example: length(x=sales.by.month)

Output = 12

New cards
12

“word”

Storing text data

Example: greeting ← “hello”

Output: hello

New cards
13

nchar(x=)

To count the number of individual characters that make up a string

For example: nchar(x=greeting)

Output = 5

New cards
14

==

Equality operator to force R to make a “true or false” judgment

Example: 2+2==4

Output = 4

New cards
15

<, <=, >, >=, ==, !=

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

New cards
16

!

not

New cards
17

|

or

New cards
18

&

and

New cards
19

TRUE (or T)

True

New cards
20

FALSE (or F)

False

New cards
21

[( )]

To extract multiple elements of a vector

For example: sales.by.month[ c(2,3,4) ]

New cards
22

x:x

Shorthand for c()

For example: 2:8

Output = 2 3 4 5 6 7 8

New cards
23

q()

To quit R

New cards
24

#

To tell R to ignore everything else you’ve written on this line. Handy for commenting

New cards
25

print()

To get R to display information

New cards
26

library(“your package of choice”)

To load a desired package

New cards
27

exists(“function within a package”)

To see if R knows about the existence of a function within a package

New cards
28

detach(“package:package of choice”, unload=TRUE)

To get rid of / to unload a package

New cards
29

install.packages()

To install packages

Example: install.packages(“psych”)

New cards
30

update.packages()

To update packages

New cards
31

lsr, psych, car, gplots, sciplot, foreign, effects, R.matlab, gdata, lmtest, reschape, compute.es, HistData, and multcomp, sem, ez, nlme, and lm34

Useful packages

New cards
32

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”)

New cards
33

rm( ,)

To remove variables

New cards
34

getwd()

To get the current working directory

New cards
35

setwd(“C:/Users/liam”)

To specify the working directory

New cards
36

list.files()

To list the contents of the directory

New cards
37

load(file=”the file you are looking for”)

To load a data file

Example: load(“chapek9.Rdata)

New cards
38

setwd(“../data”) and load(“the file you want”)

Easier way is to change the working directory first and then load the file

New cards
39

read.csv

To read a csv file

New cards
40

(data file) ← read.csv(file = “data file.csv”)

To import a CSV file

Example: books ← read.csv(file = ”booksales.csv”)

New cards
41

save.image(file = “desired file”)

To save all of the variables in your workspace into the data file

Example: save.image(file = ”myfile.Rdata”)

New cards
42

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

New cards
43

save(file = “file name”, list = list of variables you intend to save)

To save variables. Perhaps easier than the other save option

New cards
44

Inf, NaN, NA, NULL

Special values that may appear in situations where you were expecting a number

New cards
45

Inf

Infinity

Example: 1 / 0

Output = Inf

New cards
46

NaN

“Not a number”

Example: 0 / 0

Output = NaN

New cards
47

NA

“Not available”. Usually when there are missing values

New cards
48

NULL

The variable genuinely has no value whatsoever

New cards
49

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”)

New cards
50

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”

New cards
51

mode(x)

The format of the information that a variable stores

New cards
52

type(x)

To see the distinction between integer data, double precision numeric, etc.)

New cards
53

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

New cards
54

data.frame(var1, var2, var3, var4)

To convert your variables into a data frame

Example: experiment1 ← data.frame(age, gender, group, score)

New cards
55

$

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

New cards
56

who(expand = TRUE)

To expand any data frames that you’ve got in the workspace

New cards
57

~

To specify a formula

Example1: formula1 ← out ~ pred

Example2: formula2 ← out ~ pred1 + pred2

Example3: formula3 ← out ~ pred1 * pred2

Example4: formula 4 ← ~ var1 + var2

New cards
58

print.default(x)

(Come back to)

New cards
59

summary(object=variable)

To get a summary of your data

Example1: summary(object=afl.margins)

Example2: summary(chapek9)

New cards
60

plot()

To plot your data

New cards
61

?

To get help for a function

Example1: ?load

help(“load”)

Example2: ??load

help.search(“load”)

New cards
62

help.request

To request help from the R help mailing list

New cards
63

sum(variable)

To calculate the sum of a variable

Example1: sum(afl.margins)

Output = 6213

Example2: sum(afl.margins[1:5]) / 5

Output = 183

New cards
64

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

New cards
65

sort(x=variable)

Puts the data points in numerical order

New cards
66

median(x=variable)

To calculate the median of a variable

Example: median(x=afl.margins)

New cards
67

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

New cards
68

table(dataset)

To create a frequency table of the dataset

Example: xtab.3d ← table(speaker, utterance, dan.awake)

xtab.3d

New cards
69

modeOf(variable)

To calculate the mode of a variable

Example: modeOf(x=afl.finalists)

Output = “Geelong”

New cards
70

maxFreq()

To calculate the modal frequency

Example: maxFreq(x=afl.finalists)

Output = 39

New cards
71

max(variable)

To get the maximum value of a variable

Example: max(afl.margins)

New cards
72

min(variable)

To get the minimum value of a variable

Example: min(afl.margins)

New cards
73

range(variable)

To get the range of a variable

Example: range(afl.margins)

Output = 0 116

New cards
74

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

New cards
75

IQR(x=variable)

To calculate the interquartile range (25%-75%) of a variable

Example: IQR(x = afl.margins)

New cards
76

aad(x)

To calculate the mean (average) absolute deviation of a variable

Example: aad(x)

New cards
77

var(x)

To calculate the variance of a variable

New cards
78

sd(variable)

To calculate the standard deviation of a variable

New cards
79

mad(x=variable, constant=1)

To calculate the median absolute deviation

Example: mad(x=afl.margins, constant=1)

New cards
80

skew(x=variable)

To calculate the skewness of a variable

Example: skew(x = afl.margins)

New cards
81

kurtosi(x=variable)

To calculate the kurtosis of a variable

Example: kurtosi(x=afl.margins)

New cards
82

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

New cards
83

var, n, mean, sd, median, trimmed, mad, min, max, range, skew, kurtosis, se

The descriptive statistics included in the summary()

New cards
84

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)

New cards
85

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)

New cards
86

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)

New cards
87

cor(x = dataset$var1, y = dataset$var2))

To calculate correlations

New cards
88

cor(x = dataset)

To calculate all correlations of a dataset

Example: cor(parenthood)

New cards
89

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”)

New cards
90

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)

New cards
91

correlate(x, corr.method = “spearman”)

The same as correlate(x), but now species which correlation method to use

New cards
92

na.rm=TRUE

To ignore missing (NA) values when performing calculations

Example: mean(x = partial, na.rm = TRUE)

New cards
93

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”)

New cards
94

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”)

New cards
95

plot.default()

To make a basic plot

New cards
96

main = “title of plot”

To specify title of plot

New cards
97

sub = “subtitle of plot”

To specify subtitle of plot

New cards
98

xlab = “x-axis label”

To specify x-axis label

New cards
99

ylab = “y-axis label”

To specify y-axis

New cards
100

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

New cards
robot