Programming terms Flashcards
vector
c
myData[2]
second element
myData[-3]
all elements apart from the 3rd
myData[c(1,4)]
only 1st and 4th elements
myData[2:4]
2nd 3rd 4th elements
rep(3,4)
3333
4:7
4 5 6 7
seq(1,3)
1 2 3
seq(start,end,by = 2)
step of 2
seq(start,end length.out = 7)
has total of 7 elements evenly spaced
sum(1:10)
sum of all integers from 1 to 10
sum(seq(2,100,by =2))
sum all even integers between 1 and 100
x< - 1:4, x*x
1 4 9 16
x%*% x
matrix multiplication
x<- 1:4 , x+c(0,10)
1 12 3 14 recycles vectors when lengths are different
sort
sorts a vector
rank
provides the rank of each element
order
gives the indices of the elements in order
unique
returns just the unique values in the vector
table
provide counts of the occurrence of each element
length
total number of elements in the vector
sample
randomly sample from the elements of a vector
paste
concatenate a textual representation of vectors together
essential stats functions
mean, median, sd, var, min, max, range, quantile, cumsum
for (i in vec){
}
executes the code within {} for each element of the vector
you cant modify the vector you’re looping over
it is copied before the loop starts
y[-(10:20)]
vector without elements 10 though 20 inclusive
data.frame(Height = c(…….)
Weight = c(…..))
create a data frame manually
useful self explanatory functions for data frames
colMeans, rowMeans, colSums, rowSums, cov, cor, scale
hw = Height Weight data frame, hw$Height
hw[,1]
hw$Weight
hw[,2]
Interrogating data frames
names(hw)
dim(hw)
nrow(hw)
ncol(hw)
head(hw)
summary(hw)
str(hw)
hw[,1,drop = FALSE]
Keeps the data in an nx1 matrix rather than it becoming a vector
hw%BMI <- hw$Weight/(hw$Height/100)^2
making a new variable in a data frame
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….
list
each variable can be completely different sixe and data type
lists [] = access an element of the list as a single item
[[]] access item directly
$ access item by name
my function <- function(arg){
return z
}
functions
install once
install.packages(“ “)
load many times
library(“ “)
Factor variables
have a value from a limited set of possible levels
nlevels(chickwts$feed)
number of levels
data loaded by read.csv() is loaded as a string
to correct this use
mydat$var<- as.factor(mydat$var)