1. Summary
• Data Structures: vector, matrix, list, and data.frame
• Importing data into R/RStudio
• Selecting of rows and columns from a dataset
• Adding and removing columns and rows
• Categorizing continuous variables
• Replacing values within data frames
• #Replace "Male" with 1 in the Gender column
• data$Gender[data$Gender == "Male"] <- 1
2. Other functions
• cor(x, y)
• #Correlation between age and total spending
• cor(churn_data$Age, churn_data$Total_Spend)
3. Frequency Distribution
• table() - used to create frequency tables
• table(churn_data$Satisfaction_Score)
• table(churn_data$Satisfaction_Score, churn_data$Target_Churn)
• prop.table() - used to create frequency tables of proportions
• prop.table(table(churn_data$Satisfaction_Score))
• prop.table(table(churn_data$Satisfaction_Score,
churn_data$Target_Churn))
4. Basic Plots
Scatterplot
• plot()
• plot(churn_data$Age,
churn_data$Average_Transaction_Am
ount)
Frequency Histogram
• hist()
• hist(churn_data$Age)
• hist(churn_data$Age, main = "Histogram
of Online Customer Age", xlab = "AGE")
5. Other plot functions
Graph type Base R function
scatterplot plot()
frequency histogram hist()
boxplot boxplot()
Cleveland dotplot dotchart()
scatterplot matrix pairs()
conditioning plot coplot()
Editor's Notes
Removing observations (na.omit() function).
df$Gender[df$Gender == "Male"] <- 1
A histogram is very common plot. It plots the frequencies that data appears within certain ranges. A scatter plot provides a graphical view of the relationship between two sets of numbers.
Simple base R plots
There are many functions in R to produce plots ranging from the very basic to the highly complex. It’s impossible to cover every aspect of producing graphics in R in this introductory book so we’ll introduce you to most of the common methods of graphing data and describe how to customise your graphs later on in this Chapter.