This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing built in data | |
data(iris) # This imports the built-in Iris dataset | |
print(iris$Species) # Example code, printing the species of Iris in the dataset | |
# List all built-in datasets | |
data() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download data from the internet | |
# If the file is in .csv format | |
tmp <- tempfile(fileext = ".csv") | |
download.file(url = "https://data.cdc.gov/resource/9bhg-hcku.csv", | |
destfile = tmp) | |
covid_data <- read.csv(tmp, header = TRUE, stringsAsFactors = TRUE) | |
head(covid_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to import local data files | |
# For .csv files | |
data_name <- read.csv("/path/to/file_name.csv", head = TRUE, | |
stringsAsFactors = TRUE) | |
head(data_name) | |
# For .xlxs | |
library(readxl) | |
data_name <- read_excel("/path/to/file/_name.xlsx") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculating mean, and other summary statistics in R with examples | |
# Set up | |
library(palmerpenguins) | |
data(penguins) | |
penguins <- na.omit(penguins) | |
# Mean | |
mean(penguins$bill_length_mm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dealing with uncertainty in R | |
library(palmerpenguins) | |
data(penguins) | |
penguins <- na.omit(penguins) | |
# If we want to calculate 95% CI using the 2SE convention: | |
species <- NULL | |
means <- NULL | |
std_devs <- NULL | |
sizes <- NULL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Six-sided die flipped 24 times and lands on three exactly 6 times | |
binom.test(x = 6, n = 24, p = 1 / 6, alternative = "greater") | |
# Flip coin 30 times and it lands on heads exactly 19 times | |
binom.test(x = 19, n = 30, p = 1 / 2, alternative = "greater") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example chi-square | |
chi_square <- ((50 - 56.25)^2 / 56.25) + | |
((20 - 18.75)^2 / 18.75) + | |
((20 - 18.75)^2 / 18.75) + | |
((10 - 6.25)^2 / 6.25) | |
print(chi_square) | |
## Chi-square distribution | |
pchisq(3.11, df = 3, lower.tail = FALSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# One sample t assuming a population mean of 15 | |
data <- c(13, 14, 13, 12, 14, 15, 16, 13, 14, 12) | |
t.test(data, mu = 15) | |
# Paired t test | |
group1 <- c(14, 12, 13, 12, 15, 12, 15, 15, 12, 13) | |
group2 <- c(12, 15, 14, 12, 13, 13, 14, 13, 12, 12) | |
t.test(group1, group2, paired = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data(iris) | |
df <- iris | |
# Box plots | |
boxplot(Petal.Width ~ Species, data = df) | |
# Strip plot | |
library(lattice) | |
stripplot(Petal.Width ~ Species, data = df, jitter = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data(iris) | |
df <- subset(iris, iris$Species == "setosa") | |
wilcox.test(df$Sepal.Width, df$Sepal.Length) |
OlderNewer