You are on page 1of 2

R Codes

c(variable(A), variable(B)) = concatenate. Join into a group

Summary

dataframe = sn, one variable = sn$variable(A)


summary(sn$Age) # Summary for one variable
summary(sn)
# Summary for entire table (inc. categories)

Bar plot
# Split data by region, create new data frame
dataframe <- split(variable(A), variable(B)
barplot(dataframe,
col = "beige",
main = "Average Google Search Share of\n\"Data Visualization\" by
Region of US")
abline(h = 0)

Scatterplot

plot(variable(A), variable(B),
main = "Title of Scatterplot",
xlab = "X-axis label",
ylab = "Y-axis label",
pch = 20,
col = "grey")

Scatterplot Matrix
# Basic Scatterplot Matrix
pairs(~data_viz + degree + facebook + nba,
data = google,
pch = 20,
main = "Simple Scatterplot Matrix")

Linear Regression

lm(variable(A) ~ variable(B))

Adding Regression Line to plot


abline(lm(google$data_viz ~ google$degree), col="red")

Correlations

dataframe[c(x,y,z)] where x,y,z are numbers


Individual/Single correlation
cor(variable(A), variable(B))
Matrix (Quick, use hmisc)

# Need to coerce g.quant from data frame to matrix


# to get correlation matrix and p-values
rcorr(as.matrix(g.quant)), where g.quant is dataframe
Multiple Correlations
cor(cbind(Sales, Age, HS, Income, Price))

Contingency Tables

table(dataframe$variable(A), dataframe$variable(B))
Marginal Frequencies
margin.table(table(dataframe$variable(A), dataframe$variable(B)),1)
row sum
margin.table(table(dataframe$variable(A), dataframe$variable(B)),2)
column sum
Proportion Table
# With rounding to get just 2 decimal places
round(prop.table(table(dataframe$variable(A),
dataframe$variable(B)), 2) # cell %
round(prop.table(table(dataframe$variable(A),
dataframe$variable(B)), 1), 2) # row %
round(prop.table(table(dataframe$variable(A),
dataframe$variable(B)), 2), 2) # column %

You might also like