Hello! This page is created to assist in grasping the fundamentals of R. I have curated some essential concepts that are crucial for comprehending the utilization of R. I hope you enjoy!Â
Download R: https://cran.r-project.org/ Â
Download R Studio: https://www.rstudio.com/products/rstudio/
R Studio has four main panels:
The source editor
The source editor is where you write most (if not all) of your code. You can have multiple tabs for the source editor for different .R and markdown files
The Console
If you type code here it will be executed immediately and not saved. This is normally only used for short commands.
Mainly, you will use the console to see the output of your code
Workspace Browser
Here you find your “environment” which is how you can see all of your objects
Files/plots/viewer/help
There are many different tabs here. The most important one is the plots tab, this is where any plots you create will be displayed
Additionally, you have the files tab which shows you the files in your current working directory
Basic FunctionalityÂ
Although R is frequently used to accomplish amazing feats of statistical analysis and data visualization, when starting out it is best to learn to use R as though it were a fancy calculator.Â
R can do everything that a calculator can do:
Furthermore, it has the ability to place the results of these calculations into an "object", allowing you to save the value for later.
Object Oriented Programming
These objects are extremely important as R is an object oriented programming language. In short, object-oriented programming languages provide a way to store data within objects, by encapsulating data and functions that operate on that data together. This allows for a clear and organized structure for the code, making it easier to understand, maintain, and reuse.
Data Types
There are five fundamental data types in R that you should be aware of:Â
1.) Logical: TRUE, FALSE
Also denoted T or F logical data types can be used in conditional statements and can be used to filter or index data in a data frame or vector
2.) Double: 12.333, 40.3913, 1.0Â
In R a double is one of the most common data types you will encounter. It is used to denote any real decimal number, also known as a float.
Similarly, the numeric data type also denotes any number. The integer data type denotes any non-floating point integer.Â
Mostly, however, R will denote any numeric value as a double
3.) Character: “Hello”, “1”, “FALSE”
The character data type is used to represent text or strings of data and is enclosed in either double or single quotes.
It is important to note that if you place logical or a numeric data type in quotes, it becomes a characterÂ
Data Structures
VectorsÂ
A vector is a list of the same type of data type. You can use the c() function to create a vector.Â
Matrices
A matrix in R is a 2-dimensional data structure that contains elements of the same data type (e.g. numeric, character, logical).
A quick note about notation...Â
Matrices in R follow the format where the row number is listed first, followed by the column number
Data FramesÂ
A data frame in R is a two-dimensional data structure that is used to store data, with rows representing observations and columns representing variables. It can handle a variety of data types including numeric, character, and logical values, and is one of the most common types of data structures in social science research. A few notes about data frames
They are similar to matrices in that they have rows and columns, but unlike matrices, data frames can have different data types for each column
They can be created from a variety of sources, such as CSV files, spreadsheets, or databases.