R Flashcards
How to check the type?
is. numeric(MyFirstVector)
is. integer(MyFirstVector)
is. double(MyFirstVector)
is. character(v3)
How to create a vector?
x – c(1, 5, 4, 9, 0)
x – c(1, 5.4, TRUE, “hello”)
x – 1:7
seq(1, 3, by=0.2)
Brackets
w[-1]
x[c(2, 4)] # access 2nd and 4th element
w[-1] # access all but 1st element
for loop
for (x in 1:10) {
print(x)
}
fruits
type
typeof()
How to get some info. about functions?
?rnorm()
Install and activate a package
install.packages(“ggplot2”)
library(ggplot2) #activate
Matrix Indexation
[2,3]
[2,]
[.3]
How to remove a vector?
How to remove a dataframe?
rm(vector1, vector2,vector3)
rm(mydf)
Building a matrix, 3 ways
rbind()
cbind()
matrix(my.data, 4, 5) , byrow =T
How to name a vector? +remove name
vector – c(“John Doe”, “poker player”)
names(vector) – c(“Name”, “Profession”)
unname(vector)
How to name rows and cols of a matrix?
rownames(baskets.team) – c(“Granny”, “Geraldine”)
colnames(baskets.team) – c(“1st”, “2nd”, “3th”, “4th”, “5th”, “6th”)
matplot
legend
matplot(t(FieldGoals), type=”b”, pch=15:18, col=c(1:4,6))
legend(“bottomleft”, inset=0.01, legend = Players, pch=15:18, col=c(1:4,6), horiz=F)
Subsetting
Games[1:3,6:10]
Games[c(1,10),]
Games[,c(“2008”,”2009”)]
Games[1,]
Games[1,,drop=F]
Create a function and set default values
myplot