Commands and Functions Flashcards

(65 cards)

1
Q

?

A

pull up a help page

Ex: ?seq

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

:

A
colon operator (generate regular sequence)
Ex: 1.5:5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

[ ]

A

subset a vector

Ex: x[c(2, 6, 1, 3)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

[[ ]]

A

extract an element in a list
Ex: x[[“bar”]]
or extract a column in a data frame
Ex: data_frame[[“age”]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

$

A

extract an element in a list
Ex: x$bar
or extract a column in a data frame
data_frame$age

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A

assignment

Ex: x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

c

A

concatenate

Ex: c(1.1, 9. 3.14)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

seq, seq_along

A

sequence generation

Ex: seq(1, 10, 0.5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

length

A

return the length of a vector

Ex: length(pi:100)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

rep

A

replicate elements of vectors

Ex: rep(c(1, 2, 3), 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

print

A

prints its argument

Ex: print(5:10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

#

A

comment character

Ex: #ignore the rest of the line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

class

A
return the class of an object
Ex: class(pi)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

as.numeric, as.integer

A

explicit coercion

Ex: as.integer(pi)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

matrix

A

create a matrix

Ex: matrix(1:6, nrow=2, ncol=3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

dim

A

return the dimension attribute of object

Ex: dim(matrix(1:6, nrow=2, ncol=3) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

cbind, rbind

A

create matrix by column-/row- binding vectors

Ex: cbind(1:3, 4:6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

list

A

create a list

Ex: list(name = “John”, age = 20)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

factor

A

create a factor

Ex: factor(“blue”, “green”, levels = c(“red”, “green”, “blue”) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

is.na, is.nan

A

check if the argument is NA (or NaN)

Ex: is.na(as.numeric(“abc”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

data.frame

A

create a data frame

data.frame(foo = 1:4, bar = c(T, T, F, F) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

nrow, ncol

A

return the number of rows/columns of object

Ex: nrow(data.frame(foo = 1:4, bar = c(T, T, F, F) ) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

names

A

names of elements of a vector

Ex: names(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

colnames, rownames

A

column/row names of a matrix/data frame

Ex: colnames(m)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
paste
concatenate strings | Ex: paste(“aa”, “bb”, “cc”, sep = “:”)
26
sum
summation of elements in a vector | Ex: sum(c(1, 5, 12) )
27
prod
product of elements in a vector | Ex: prod(c(3, 15, -2) )
28
max, min
maximum/minimum value in a vector | Ex: max(c(23, 12, 12, 3) )
29
mean, median
mean/median of a vector | Ex: mean(c(1, 5, 1, 2, 7) )
30
sort
sorting | Ex: sort(c(1, 5, 1, pi, 2, 7) )
31
var, sd
sample variance/standard deviation of a vector | Ex: sd(c(1, 5, 1, 3, 2) )
32
cor
correlation | Ex: cor(c(1, 4, 6, 1), c(2, 5, 1, 9) )
33
which.min, which.max
first index of minimum/maximum value | Ex: which.min(c(4, 1, 3, 4, 1) )
34
set.seed
set a seed for random number generation | Ex: set.seed(13218)
35
>,
greater/less than | Ex: 1:5 > seq(0, 8, 2)
36
==
equality operator | Ex: 1:5 == seq(0, 8, 2)
37
>=, <=
greater or equal to/less than or equal to | Ex: 1:5 <= seq(0, 8, 2)
38
!=
not equal to | Ex: 1:5 != seq(0, 8, 2)
39
!
NOT | Ex: !(1 > 2)
40
&
AND | Ex: (3:5 > 5:7) & (4:6 == 4:6)
41
|
OR | Ex: (3:5 > 5:7) | (4:6 == seq(2, 6, 2) )
42
abs
absolute value | Ex: abs(3 - 6) = 3
43
sqrt
square root | Ex: sqrt(16) = 4
44
^
exponentiation | Ex: 3^10 = 59049
45
exp
exponential function | Ex: exp(1.7) = e^1.7 = 5.473947
46
log
``` log function (base e) Ex: log(10) = 2.302585 ```
47
log10
base 10 log (log sub 10) | Ex: log10(100) = 2
48
pi
mathematical constant π | Ex: pi = 3.141593
49
sin, cos, tan
``` trigonometric functions (argument in radians) Ex: sin(pi/2) = 1 ```
50
asin, acos, atan
inverse trigonometric functions | Ex: acos(1) = 0
51
sinh, cosh, tanh
hyperbolic functions | Ex: cosh(0) = 1
52
asinh, acosh, atanh
inverse hyperbolic functions | Ex: atanh(tanh(12)) = 12
53
round(x, n)
round x to n decimal places | Ex: round(pi, 2) = 3.14
54
floor
rounds down | Ex: floor(14.7) = 14
55
ceiling
rounds up | Ex: ceiling(14.7) = 15
56
dnorm, pnorm, qnorm, rnorm
``` Normal Distribution [p for “probability”, the cumulative distribution function (cdf) q for “quantile”, the inverse cdf d for “density”, the probability density function (pdf) r for “random”, a random variable having the specified distribution] Ex: dnorm(-3:3) curve(dnorm(x), xlim=c(-3,3)) pnorm(1.65) pnorm(-1.65) qnorm(0.05,lower.tail=FALSE) set.seed(107281) rnorm(10) ```
57
dchisq, pchisq, qchisq, rchisq
χ^2 (Chi-Squared) Distribution [p for “probability”, the cumulative distribution function (cdf) q for “quantile”, the inverse cdf d for “density”, the probability density function (pdf) r for “random”, a random variable having the specified distribution] Ex: curve(dchisq(x,1),xlim=c(0,15),ylim=c(0,0.6),ylab="Chi Square Density") pchisq(21.78,10,lower.tail=FALSE) qchisq(0.05,10,lower.tail=FALSE)
58
dt, pt, qt, rt
Student t Distribution [p for “probability”, the cumulative distribution function (cdf) q for “quantile”, the inverse cdf d for “density”, the probability density function (pdf) r for “random”, a random variable having the specified distribution] Ex: curve(dt(x,1),xlim=c(-5,5),ylim=c(0,0.4),ylab="Student's t Density")
59
read.table, read.csv
read data from a text file | Ex: data
60
write.table, write.csv
write data to a text file | Ex: write.csv(data, “foo.csv”)
61
readLines
read text file line by line | Ex: data
62
writeLines
write text to file | Ex: writeLines(“write something…”, “foo.txt”)
63
hist
plot a histogram | Ex: hist(rnorm(1e5), freq = FALSE, breaks = 100)
64
curve
plot a function | Ex: curve(x^2, xlim = x(-1, 1) )
65
legend
add a legend to a plot | Ex: legend(1, 2, c(“text1”, “text2”), col = 1:2, lty=1:2)