R Coding Basics

studied byStudied by 0 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 8

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

9 Terms

1

Variables can be created using <- or =.

name <- "Girlypop"    # String
age <- 16 # Numeric
is_student <- TRUE # Boolean (TRUE/FALSE)

# Let's print these out!
print(name)
print(age)
print(is_student)

New cards
2

R has multiple data structures like vectors, data frames, and lists.

Vectors

Vectors are like arrays in other languages, holding elements of the same type.

# Vector of numbers
numbers <- c(1, 2, 3, 4, 5)
print(numbers)

# Vector of characters (strings)
fruits <- c("apple", "banana", "strawberry")
print(fruits)

New cards
3

Data frames are used to store tabular data. Think of them as little spreadsheets.

# Create a data frame
students <- data.frame(
Name = c("Maya", "Rosa", "Ava"),
Grade = c(90, 85, 95),
Passed = c(TRUE, TRUE, TRUE)
)

# View the data frame
print(students)

New cards
4

If-Else Statements

Control the flow of your program with conditions.

# If-else example
grade <- 95

if (grade >= 90) {
print("A+ girlypop!")
} else {
print("Keep working hard!")
}

New cards
5

For Loops Repeat actions a specific number of times.

# For loop to print numbers
for (i in 1:5) {
print(paste("This is loop number", i))
}

New cards
6

While Loops Repeat until a condition is false.

# While loop example
counter <- 1

while (counter <= 3) {
print(paste("Counter is", counter))
counter <- counter + 1
}

New cards
7

Functions in R allow you to write reusable code.

# Define a function
sparkle <- function(name, mood) {
paste(name, "is feeling", mood, "and ready to conquer the world!")
}

# Call the function
message <- sparkle("Girlypop", "fabulous")
print(message)

New cards
8

You can read in data from CSV files.

# Install ggplot2 if not already
install.packages("ggplot2")
library(ggplot2)

# Create a simple plot
ggplot(students, aes(x = Name, y = Grade)) +
geom_bar(stat = "identity", fill = "lightpink") +
ggtitle("Student Grades") +
theme_minimal()

New cards
9

If you’re into data science, R is great for machine learning! Here's a super quick intro.

# Install caret package for ML
install.packages("caret")
library(caret)

# Example: Splitting data into training and test sets
data(iris)
set.seed(123)
trainIndex <- createDataPartition(iris$Species, p = .8, list = FALSE)
trainSet <- iris[trainIndex, ]
testSet <- iris[-trainIndex, ]

# Train a decision tree model
install.packages("rpart")
library(rpart)
model <- rpart(Species ~ ., data = trainSet)

# Predict on the test set
predictions <- predict(model, testSet, type = "class")
print(predictions)

New cards

Explore top notes

note Note
studied byStudied by 21 people
611 days ago
5.0(1)
note Note
studied byStudied by 64 people
665 days ago
5.0(4)
note Note
studied byStudied by 31 people
926 days ago
5.0(1)
note Note
studied byStudied by 9 people
842 days ago
5.0(1)
note Note
studied byStudied by 2 people
21 days ago
5.0(1)
note Note
studied byStudied by 52 people
659 days ago
5.0(1)
note Note
studied byStudied by 41 people
915 days ago
5.0(1)

Explore top flashcards

flashcards Flashcard (39)
studied byStudied by 9 people
792 days ago
5.0(1)
flashcards Flashcard (80)
studied byStudied by 92 people
515 days ago
5.0(2)
flashcards Flashcard (44)
studied byStudied by 1 person
428 days ago
5.0(1)
flashcards Flashcard (33)
studied byStudied by 2 people
75 days ago
5.0(1)
flashcards Flashcard (53)
studied byStudied by 15 people
296 days ago
5.0(1)
flashcards Flashcard (71)
studied byStudied by 11 people
860 days ago
5.0(2)
flashcards Flashcard (43)
studied byStudied by 5 people
748 days ago
5.0(2)
flashcards Flashcard (266)
studied byStudied by 137 people
284 days ago
5.0(1)
robot