Programming terms Flashcards

You may prefer our related Brainscape-certified 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
Q

for (i in vec){

}

A

executes the code within {} for each element of the vector

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

you cant modify the vector you’re looping over

A

it is copied before the loop starts

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

y[-(10:20)]

A

vector without elements 10 though 20 inclusive

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

data.frame(Height = c(…….)
Weight = c(…..))

A

create a data frame manually

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

useful self explanatory functions for data frames

A

colMeans, rowMeans, colSums, rowSums, cov, cor, scale

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

hw = Height Weight data frame, hw$Height

A

hw[,1]

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

hw$Weight

A

hw[,2]

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

Interrogating data frames

A

names(hw)
dim(hw)
nrow(hw)
ncol(hw)
head(hw)
summary(hw)
str(hw)

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

hw[,1,drop = FALSE]

A

Keeps the data in an nx1 matrix rather than it becoming a vector

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

hw%BMI <- hw$Weight/(hw$Height/100)^2

A

making a new variable in a data frame

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

wq.red[order(wq.red$ph),]

A

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….

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

list

A

each variable can be completely different sixe and data type

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

lists [] = access an element of the list as a single item

A

[[]] access item directly
$ access item by name

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

my function <- function(arg){

return z
}

A

functions

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

install once

A

install.packages(“ “)

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

load many times

A

library(“ “)

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

Factor variables

A

have a value from a limited set of possible levels

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

nlevels(chickwts$feed)

A

number of levels

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

data loaded by read.csv() is loaded as a string

A

to correct this use
mydat$var<- as.factor(mydat$var)

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

forcats

A

package to solve common problems with factors

45
Q

changing the order of factors

A

chickwts$feed<- fct_inorder(chickwts$feed)
can also use fct_infreq, or fct_reorder(….,….)

46
Q

calculating mean with missing data

A

na.rm=TRUE

47
Q

plot(x,y,…)

A

scatter plot of points x vsy

48
Q

Common extra arguments for base r plot

A

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
Q

points(x,y….)

A

adds to an existing plot

50
Q

lines(x,y….)

A

adds a line to an existing plot

51
Q

lowess()

A

fits a smoothed line, f argument controls smoothness

52
Q

density()

A

fit a smoothed continuous version of histogram

53
Q

other base r plotting functions

A

hist()= histogram
boxplot()
barplot() = for categorical bar charts
abline() = add straight lines to existing plot

54
Q

pairs()

A

get a grid of all pairwise scatter plots

55
Q

tidy data

A

= third normal form,
- each variable is in a column
- each observation is in a row
- each type of observational unit forms a table

56
Q

too wide

A

one variable is spread across multiple columns

57
Q

too long

A

one observation is spread across multiple rows

58
Q

pivot_longer()

A
  • 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
Q

pivot_wider()

A

-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
Q

other useful tidyr functions

A

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
Q

main dplyr functions

A

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
Q

joining data frames

A

rbind() = paste rows together (above/below)
cbind()= paste cols together (left/right)

63
Q

left_join(x,y)

A

add new variables from y to x keeping all x

64
Q

right_join(x,y)

A

add new variables from x to y keeping all y

65
Q

inner_join(x,y)

A

keep only matching rows

66
Q

full_join(x,y)

A

keep all rows in both x and y

67
Q

ggplot(diamonds, aes(x=carat,y= price)

A

mapping to specify what variables map to the x axis,y axis, color legend etc
mapping specified by aes()

68
Q

use geoms to specify how data is plotted

A

adding + to the plot ;
+ geom_point()

69
Q

geoms

A

inherit data and mapping from the original ggplot() but can be overridden (or added to with aes)

70
Q

geom functions

A

geom_point
geom_smooth
geom_hex

71
Q

can make a plot a variable and then literally add geom to it

A

p +geom_smooth(method = “lm”)

72
Q

stats

A

stat_bin_hex(bins = 60)
stat_ecdf()

73
Q

faceting enables splitting data into multiple plots according to categorical variable

A

facet_wrap() = single variable split
facet_grid() = two variable split

74
Q

r markdown styles

A

italic text
bold text
~~strikeout text~~

75
Q

r markdown sections

A

section heading
##subsection heading
###sub sub section heading

76
Q

r markdown lists

A

4 spaces needed to create the indent
dont need to increment the numbering manually

77
Q

including r code in r markdown

A

‘r 1+1’ gives value 2 ‘1+1’ gives text 1+1

78
Q

including r in r markdown chunks

A

’’‘{r}

’’’

79
Q

more including r in r markdown, outputs

A

echo =FALSE just shows the output not the code
eval = FALSE just shows code but doesnt run it

included in ‘’‘{r,echo=}

80
Q

r markdown and latex

A

can use display style with double dollar signs

81
Q

ui hierarchy

A

pages >layouts>panels>inputs/outputs

82
Q

pages shiny

A

-just 1 per ui
-define overall page structure

83
Q

layouts and panels shiny

A

-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
Q

inputs/outputs shiny

A

-create the visible content of the page
-enable user to interact with your app
-provide placeholders you can programmatically update

85
Q

ui pages

A

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
Q

titlePanel

A

create full width title

87
Q

sidebarLayout()

A

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
Q

ui inputs

A

inputId = must be unique
accessed by input$name

89
Q

ui inputs text

A

textInput() = single line text input
passwordInput() = hides the input text on screen
textAreaInput() = allows multiline inputs

90
Q

ui inputs numeric

A

numericInput() = type the number directly
sliderInput() = to drag to choose a number - can choose a range too

91
Q

ui inputs categorical

A

selectInput() = drop down list, single selection default (multiple = TRUE for more)
radioButtons() = single selection radio buttons
checkboxGroupInput() = multiple selection checkboxes

92
Q

ui outputs

A

all outputs also take the same first arguments
outputId = must be unique

93
Q

ui output text

A

textOutput() and renderText()
verbatimTextOutput() renderPrint()

94
Q

ui outputs plots

A

plotOutput() and renderPlot()
argument res= 96 is recommended for the plot to look as close to scale as r studio

95
Q

variables outside plots

A

need to wrap any calculations in reactive() and then access those new variables like a function

96
Q

lubridate

A

part of tidyverse but not loaded automatically, date and time

97
Q

today()

A

current date

98
Q

now()

A

current date-time

99
Q

constructing dates/date-times

A

ymd()
mdy()
dmy()
from either a string or a number
ymd_hms()
mdy_hm()
make_date()
make_datetime()

100
Q

timezones

A

now(tz=”America/New_York”)

101
Q

changing timezone

A

force_tz(x, “America/New_York”) = forces zone without converting
with_tz(x, “America/New_York”) = converts to new timezone

102
Q

extractingfrom dates/datetimes

A

year()
month()
month(datetime, label = TRUE ) gives names of month
mday()
yday()
wday()
hour()
minute()
second()

103
Q

rounding up/down dates

A

floor_date(datetime, unit= “ “)
ceiling_date(datetime, unit = “ “)

104
Q

stringr string lengths

A

str_length()

105
Q

combining strings

A

str_c(“Data”,”Science”,”and”, sep = “ “)
str_c(c(“Data”,”Science”,”and”,collapse=” “)

106
Q

sub setting strings

A

str_sub(data,start, end)
can be negative

can also do str_sub(data,1,2)<- “Zo”

107
Q

trimming strings

A

str_trim()
str_squish()

108
Q

exact matching using regex

A

str_view(x,”an”)

for any single character str_view(x,”.a.”)

to match the character . you must provide \.

109
Q

regex anchoring

A

anchoring the start = str_view(x,”^a”)
and the end str_view(x,”a$”)