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
Main diff between matrix and dataframe?
All elements of the matrix have same type
Select it manually and read a csv file
stats
Set working directory
getwd()
setwd(“C:/Users/266507/OneDrive - American Airlines, Inc/Documents/R”)
Read a csv file
stats
find no. of rows and columns:
nrow(stats)
ncol(stats)
head and tail:
head(stats, n=10)
tail(stats)
summary of the data:
str(stats)
summary(stats)
Using the $ sign:
stats$Country.Name
stats[,”Country.Name”]
stats$Country.Name[2]
Categories:
levels(stats$Country.Name)
A diff between Matrix and DataFrame:
When we get one row of a matrix, it become a vector but it’s not the case in DataFrame
But it’s gonna be a vector when we get a col in a dataframe
How to check if it’s a dataframe
is.data.frame(stats[1,])
How to preserve a col of a dataframe as a dataframe when subsetting:
stats[,1,drop=F]
Add and remove a col to a dataframe
stats$MyCalc
Filtering dataframes
stats[stats$Birth.rate>40 & stats$Internet.users<2, ]
stats[stats$Income.Group == “High income”, ]
qplot
qplot(data=stats, x=Internet.users)
qplot(data=stats, x=Income.Group, y=Birth.rate)
qplot(data=stats, x=Income.Group, y=Birth.rate, size=I(3), color=I(“blue”))
qplot(data=stats, x=Internet.users, y=Birth.rate, size=I(5), color=Income.Group)
qplot(data=stats, x=Income.Group, y=Birth.rate, geom=”boxplot”)
Creating DataFrames
mydf
Merging Data Frames
merged
Change the shape on qplot
qplot(data=merged, x=Internet.users, y=Birth.rate, color=Region, size=I(5), shape=I(19))
Control the transparency on qplot
qplot(data=merged, x=Internet.users, y=Birth.rate, color=Region, size=I(5), shape=I(19), alpha=I(0.6))
Set the title in qplot
qplot(data=merged, x=Internet.users, y=Birth.rate, color=Region, size=I(5), shape=I(19), alpha=I(0.6), main=”Birth Rate vs Internet Users”)