Introduction to R Programming and RStudio (copy)
Introduction to R Programming Language
R is an object-oriented programming language.
Primarily used for statistics-based research.
Ubiquitous in data science.
The course will focus on teaching through R, although similar tasks can be accomplished using other programming languages like Python.
For biomedical informatics students, this class serves to complement Python programming taught in other courses.
Open to assist students outside the BMI track with Python if needed.
R and RStudio
RStudio vs R:
RStudio is not R itself; it is an Interactive Development Environment (IDE) for R.
Encouraged to download R and RStudio separately for better coding experience.
RStudio interface:
Upon opening RStudio, the default interface includes:
New file options (R scripts, Quarto documents, R notebooks, etc.)
Console for executing R commands.
Environment/Dataset area showing variables in memory.
Files and plots area for managing working directory files and plot outputs.
Packages area for R packages installed.
Working with R
Variable initialization:
To initialize a variable, type:
variable_name = value(e.g.,variable1 = 1).You can also use the arrow operator
variable_name <- value(both methods are equivalent).
Environment updates:
Variables saved to memory will appear in the environment area after initialization.
Variable name restrictions in R:
Cannot use operators (e.g.,
$,+, etc.).Cannot use spaces or special characters at the beginning of the name.
Cannot start with a number or be a reserved word (like
true,false).Valid characters include letters, digits (not at the start), underscores (
_).
Comments in Code
Comments are created using the pound sign (
#):Everything after the
#is ignored during compilation.Comments can be used to document code, define sections or concepts, and describe functionality.
Comparison Operators
Types of comparison operators in R:
==: Equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
Data Types in R
Numeric: Numbers (e.g.,
4,3.14).Character/String: Textual data enclosed in quotation marks (e.g.,
"John").Boolean: Represents logical values (True or False) and can be represented as
TorF.Missing values: Represented as
NAor similar codes that denote lack of information.
Vectors as Data Structures
Vector: A collection of multiple values.
Creation:
vector_name = c(value1, value2, ...).
Vector operations are possible (e.g., arithmetic transformations).
Mixed Data Vectors: Mixing of data types may result in entire vector being of one type (commonly character).
Handle mixed types using appropriate conversion functions.
Data Frames
Data Frame: A collection of vectors organized into a table with rows and columns.
Created by binding vectors of the same length.
Allows for storing and manipulating structured data efficiently.
Functions in R
Functions are pre-written blocks of code meant to execute specific tasks.
Example:
table()used for generating frequency tables from categorical data.
Accessing help documentation: Use
?function_name(e.g.,?table) in R to bring up help documentation.
File Handling in R
Saving data: Using
write.csv()to save data frames to CSV files.Example syntax:
write.csv(dataframe, "filepath/filename.csv").
Working with file paths:
Absolute file paths: Complete path starting from the root directory (e.g.,
C:/Users/...).Relative file paths: Path relative to the current working directory.
Change working directory if necessary using
setwd("path").
R Packages
R packages are collections of R functions that extend the language's capabilities.
Install packages using
install.packages("package_name").Load packages into the current session using
library(package_name).
Access package function documentation through RStudio for easy reference.
Learning R
Recommended approach:
Identify a problem or dataset (e.g., from Kaggle, a real-life research scenario) to practice R programming.
Engage with the material, make attempts, and seek help when needed.
Learn through trial and error, leveraging online resources and guidance from instructors.
Conclusion
Continued focus on practical data analysis in upcoming classes, including a session on data modeling.
Encourage active participation and personal projects to enhance learning.