Created
October 19, 2022 08:55
-
-
Save alejandro-ao/d984b31e0e47b98395242206a8e182eb to your computer and use it in GitHub Desktop.
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
# vectors | |
vector <- c(10,20,30, "hello") | |
# lists | |
list <- list(1, "hello", 0.5, list(1,2,3), c(2,3,4)) | |
# data types | |
# logical | |
class(FALSE) | |
# numeric | |
class(10.5) | |
# integetr | |
class(10L) | |
# complex | |
class(2+3i) | |
# dataframe | |
patientID <- c(1, 2, 3, 4) | |
age <- c(25, 34, 28, 52) | |
diabetes <- c("Type1", "Type2", "Type1", "Type1") | |
status <- c("Poor", "Improved", "Excellent", "Poor") | |
patientdata <- data.frame(patientID, age, diabetes, status) | |
# exploring your dataset | |
str(patientdata) | |
str(titanic) | |
min(na.omit(titanic$Age)) | |
# subsetting vector | |
vector1 <- c(5,6,3,4) | |
vector1[c(1,2)] | |
vector1[c(TRUE, TRUE, FALSE, TRUE)] | |
vector1 < 4.5 | |
vector1[vector1 < 4.5] | |
vector2 <- c(first=1, second=2, etc=3) | |
vector2["etc"] | |
# subsetting lists | |
list[[1]] | |
class(list[[4]]) | |
# subsetting matrices | |
a <- matrix(1:9, nrow=3) | |
a[2,3] # 8 | |
a[2:3,2:3] | |
a[2:3,c(1,3)] | |
# data frames | |
print(titanic) | |
titanic$Name | |
titanic['Name'] | |
titanic[3,3] | |
titanic[3:9,3] | |
all_columns <- titanic[3:9,] | |
View(all_columns) | |
all_rows <- titanic[,c(2,3)] | |
View(all_rows) | |
subset <- titanic[3:9,c(3,5)] | |
View(subset) | |
# functions | |
product = function(a,b) { | |
return(a*b) | |
} | |
product(3,6) | |
# anonymous function & lapply | |
titanic$Age <- lapply(titanic$Age, function(x) x*2) | |
# loops | |
names = c("mary", "joseh", "jesus") | |
for (name in names) { | |
print(name) | |
} | |
count = 0 | |
while (count < 5){ | |
count = count + 1 | |
print(count) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment