Programming terms Flashcards

1
Q

vector

A

c

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

myData[2]

A

second element

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

myData[-3]

A

all elements apart from the 3rd

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

myData[c(1,4)]

A

only 1st and 4th elements

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

myData[2:4]

A

2nd 3rd 4th elements

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

rep(3,4)

A

3333

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

4:7

A

4 5 6 7

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

seq(1,3)

A

1 2 3

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

seq(start,end,by = 2)

A

step of 2

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

seq(start,end length.out = 7)

A

has total of 7 elements evenly spaced

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

sum(1:10)

A

sum of all integers from 1 to 10

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

sum(seq(2,100,by =2))

A

sum all even integers between 1 and 100

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

x< - 1:4, x*x

A

1 4 9 16

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

x%*% x

A

matrix multiplication

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

x<- 1:4 , x+c(0,10)

A

1 12 3 14 recycles vectors when lengths are different

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

sort

A

sorts a vector

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

rank

A

provides the rank of each element

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

order

A

gives the indices of the elements in order

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

unique

A

returns just the unique values in the vector

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

table

A

provide counts of the occurrence of each element

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

length

A

total number of elements in the vector

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

sample

A

randomly sample from the elements of a vector

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

paste

A

concatenate a textual representation of vectors together

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

essential stats functions

A

mean, median, sd, var, min, max, range, quantile, cumsum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
for (i in vec){ }
executes the code within {} for each element of the vector
26
you cant modify the vector you're looping over
it is copied before the loop starts
27
y[-(10:20)]
vector without elements 10 though 20 inclusive
28
data.frame(Height = c(.......) Weight = c(.....))
create a data frame manually
29
useful self explanatory functions for data frames
colMeans, rowMeans, colSums, rowSums, cov, cor, scale
30
hw = Height Weight data frame, hw$Height
hw[,1]
31
hw$Weight
hw[,2]
32
Interrogating data frames
names(hw) dim(hw) nrow(hw) ncol(hw) head(hw) summary(hw) str(hw)
33
hw[,1,drop = FALSE]
Keeps the data in an nx1 matrix rather than it becoming a vector
34
hw%BMI <- hw$Weight/(hw$Height/100)^2
making a new variable in a data frame
35
wq.red[order(wq.red$ph),]
this is an accessor, the original data frame is unchanged. To change it we would have to overwrite it using wq.red<- wq.red[order....
36
list
each variable can be completely different sixe and data type
37
lists [] = access an element of the list as a single item
[[]] access item directly $ access item by name
38
my function <- function(arg){ return z }
functions
39
install once
install.packages(" ")
40
load many times
library(" ")
41
Factor variables
have a value from a limited set of possible levels
42
nlevels(chickwts$feed)
number of levels
43
data loaded by read.csv() is loaded as a string
to correct this use mydat$var<- as.factor(mydat$var)
44
forcats
package to solve common problems with factors
45
changing the order of factors
chickwts$feed<- fct_inorder(chickwts$feed) can also use fct_infreq, or fct_reorder(....,....)
46
calculating mean with missing data
na.rm=TRUE
47
plot(x,y,...)
scatter plot of points x vsy
48
Common extra arguments for base r plot
col = colour pch = plotting symbol xlab,ylab = axis labels xlim,ylim = plotting range of x or y main = plot title type , p= points, l= line, b = both
49
points(x,y....)
adds to an existing plot
50
lines(x,y....)
adds a line to an existing plot
51
lowess()
fits a smoothed line, f argument controls smoothness
52
density()
fit a smoothed continuous version of histogram
53
other base r plotting functions
hist()= histogram boxplot() barplot() = for categorical bar charts abline() = add straight lines to existing plot
54
pairs()
get a grid of all pairwise scatter plots
55
tidy data
= third normal form, - each variable is in a column - each observation is in a row - each type of observational unit forms a table
56
too wide
one variable is spread across multiple columns
57
too long
one observation is spread across multiple rows
58
pivot_longer()
- gather multiple columns in to key-value pairs - makes wide data longer arguments: -data frame -columns to transform - name of column where previous column names should go -name of column where values from the column should go
59
pivot_wider()
-gather multiple columns into key-value pairs - makes long data wider arguments - data frame - name of column where previous column names should go - name of column where values from column should go
60
other useful tidyr functions
separate() = splits one columns of strings into multiple new columns unite()= combines many columns into one (as as string) extract() = uses regular expressions to pull out specific info from a string column
61
main dplyr functions
filter = focus on a subset of rows arrange = reorder the rows select = focus on a subset of variables mutate = create new derived variables summarise = create summary statistics (collapsing many rows) by groupings
62
joining data frames
rbind() = paste rows together (above/below) cbind()= paste cols together (left/right)
63
left_join(x,y)
add new variables from y to x keeping all x
64
right_join(x,y)
add new variables from x to y keeping all y
65
inner_join(x,y)
keep only matching rows
66
full_join(x,y)
keep all rows in both x and y
67
ggplot(diamonds, aes(x=carat,y= price)
mapping to specify what variables map to the x axis,y axis, color legend etc mapping specified by aes()
68
use geoms to specify how data is plotted
adding + to the plot ; + geom_point()
69
geoms
inherit data and mapping from the original ggplot() but can be overridden (or added to with aes)
70
geom functions
geom_point geom_smooth geom_hex
71
can make a plot a variable and then literally add geom to it
p +geom_smooth(method = "lm")
72
stats
stat_bin_hex(bins = 60) stat_ecdf()
73
faceting enables splitting data into multiple plots according to categorical variable
facet_wrap() = single variable split facet_grid() = two variable split
74
r markdown styles
*italic text* **bold text** ~~strikeout text~~
75
r markdown sections
section heading ##subsection heading ###sub sub section heading
76
r markdown lists
4 spaces needed to create the indent dont need to increment the numbering manually
77
including r code in r markdown
'r 1+1' gives value 2 '1+1' gives text 1+1
78
including r in r markdown chunks
'''{r} '''
79
more including r in r markdown, outputs
echo =FALSE just shows the output not the code eval = FALSE just shows code but doesnt run it included in '''{r,echo=}
80
r markdown and latex
can use display style with double dollar signs
81
ui hierarchy
pages >layouts>panels>inputs/outputs
82
pages shiny
-just 1 per ui -define overall page structure
83
layouts and panels shiny
-define how to place the arguments given to them on the page -layouts can have complex structure -panel often define the look of the added item
84
inputs/outputs shiny
-create the visible content of the page -enable user to interact with your app -provide placeholders you can programmatically update
85
ui pages
fluidPage() = every item passed to it is just placed straight on the page, wrapped where necessary navbarPage() = first argument is title, 1+ more arguments are calls to tabPanel() for each tabbed panel eg
86
titlePanel
create full width title
87
sidebarLayout()
creates a sidebar with styling, usually for inputs in sidebar and outputs in main area sidebarPanel() to set left menu mainPanel() set body of output fluidRow() defines a new row containing column() calls - first a number 1-12, indicating how much width to take up (must sum to 12), second+ arguments are outputs
88
ui inputs
inputId = must be unique accessed by input$name
89
ui inputs text
textInput() = single line text input passwordInput() = hides the input text on screen textAreaInput() = allows multiline inputs
90
ui inputs numeric
numericInput() = type the number directly sliderInput() = to drag to choose a number - can choose a range too
91
ui inputs categorical
selectInput() = drop down list, single selection default (multiple = TRUE for more) radioButtons() = single selection radio buttons checkboxGroupInput() = multiple selection checkboxes
92
ui outputs
all outputs also take the same first arguments outputId = must be unique
93
ui output text
textOutput() and renderText() verbatimTextOutput() renderPrint()
94
ui outputs plots
plotOutput() and renderPlot() argument res= 96 is recommended for the plot to look as close to scale as r studio
95
variables outside plots
need to wrap any calculations in reactive() and then access those new variables like a function
96
lubridate
part of tidyverse but not loaded automatically, date and time
97
today()
current date
98
now()
current date-time
99
constructing dates/date-times
ymd() mdy() dmy() from either a string or a number ymd_hms() mdy_hm() make_date() make_datetime()
100
timezones
now(tz="America/New_York")
101
changing timezone
force_tz(x, "America/New_York") = forces zone without converting with_tz(x, "America/New_York") = converts to new timezone
102
extractingfrom dates/datetimes
year() month() month(datetime, label = TRUE ) gives names of month mday() yday() wday() hour() minute() second()
103
rounding up/down dates
floor_date(datetime, unit= " ") ceiling_date(datetime, unit = " ")
104
stringr string lengths
str_length()
105
combining strings
str_c("Data","Science","and", sep = " ") str_c(c("Data","Science","and",collapse=" ")
106
sub setting strings
str_sub(data,start, end) can be negative can also do str_sub(data,1,2)<- "Zo"
107
trimming strings
str_trim() str_squish()
108
exact matching using regex
str_view(x,"an") for any single character str_view(x,".a.") to match the character . you must provide \\.
109
regex anchoring
anchoring the start = str_view(x,"^a") and the end str_view(x,"a$")