years <- c(1980, 1980, 1985, 1990)
scores <- c(34, 44, 56, 83)
df <- data.frame(years, scores)
#This creates a data frame df with 2 columns years and scores
df[,1] #will access the values for scores
## [1] 1980 1980 1985 1990
#[,x]: Return all values in column x
df$years
## [1] 1980 1980 1985 1990
df[df$scores < 50,]
## years scores
## 1 1980 34
## 2 1980 44
df[df$year==1980, "scores"]
## [1] 34 44
subject_name <- c("John Doe", "Jane Doe", "Steve Graves")
temperature <- c(98.1, 98.6, 101.4)
flu_status <- c(FALSE, FALSE, TRUE)
temperature[2]
## [1] 98.6
temperature[2:3]
## [1] 98.6 101.4
temperature[-2]
## [1] 98.1 101.4
gender <- factor(c("MALE", "FEMALE", "MALE"))
gender
## [1] MALE FEMALE MALE
## Levels: FEMALE MALE
blood <- factor(c("O", "AB", "A"),
levels = c("A", "B", "AB", "O"))
symptoms <- factor(c("SEVERE", "MILD", "MODERATE"),
levels = c("MILD", "MODERATE", "SEVERE"), ordered = TRUE)
symptoms
## [1] SEVERE MILD MODERATE
## Levels: MILD < MODERATE < SEVERE
symptoms > "MODERATE"
## [1] TRUE FALSE FALSE
pt_data <- data.frame(subject_name, temperature, flu_status, gender, blood, symptoms, stringsAsFactors = FALSE)
pt_data$subject_name
## [1] "John Doe" "Jane Doe" "Steve Graves"
pt_data[1,2]
## [1] 98.1
pt_data[, 1]
## [1] "John Doe" "Jane Doe" "Steve Graves"
pt_data[c(1,3), c(2, 4)]
## temperature gender
## 1 98.1 MALE
## 3 101.4 MALE
pt_data[2 ,]
## subject_name temperature flu_status gender blood symptoms
## 2 Jane Doe 98.6 FALSE FEMALE AB MILD
pt_data[,]
## subject_name temperature flu_status gender blood symptoms
## 1 John Doe 98.1 FALSE MALE O SEVERE
## 2 Jane Doe 98.6 FALSE FEMALE AB MILD
## 3 Steve Graves 101.4 TRUE MALE A MODERATE
pt_data[c(1,3),c("temperature", "gender")]
## temperature gender
## 1 98.1 MALE
## 3 101.4 MALE
pt_data$temp_c<-(pt_data$temperature - 32)*(5/9)
pt_data[c("temperature", "temp_c")]
## temperature temp_c
## 1 98.1 36.72222
## 2 98.6 37.00000
## 3 101.4 38.55556
#Graphics
x <- rnorm(100)
y <- rnorm(100)
plot(x, y)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = cty))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = cyl))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 2)
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 3)
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ cyl, nrow = 3)