print("Hello, World!") # Welcome to my corner of the digi-verse.
## [1] "Hello, World!"
This document exists to
Practice using RStudio,
Practice using RMarkdown (both the style and file-type),
To document my process of learning R and create a future-reference resource, and
To preserve the best of my understanding and resources for my friends who are also learning R (now and in the future).
Checkout the RStudio RMarkdown Cheat Sheet, which I referenced while creating this document.
#Example:
Checkout the [RStudio RMarkdown Cheat Sheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf), which I referenced while creating this document.
All of this is subject to change. I started writing these in .R and now I’m writing in RMarkdown. *
Learn_R_ref_Intro_to.R - That’s where you are now
Learn_R_ref_Resources.html - This will be links to Videos, Blogs, and Websites about R
Learn_R_ref_Basics_of.R - This will be covering the very basic elements of R syntax, vocabulary, etc.
Learn_R_4_Packages_.R - Information on important or interesting packages I find along the way.
Learn_R_ref_HowTo_TBC -
Interesting - To create that list, I used an asterisk, which is what RMarkdown Cheat Sheet suggested. It didn’t take. When I wrote it in the Visual vs Source, using the buttons, and went back to the code, it showed a new format. This can happen when updates come through.
This is what I used:
# This is what I used:
* Learn_R_ref_Intro_to.R - That's where you are now
* Learn_R_ref_Resources.html - This will be links to Videos, Blogs, and Websites about R
* Learn_R_ref_Basics_of.R - This will be covering the very basic elements of R syntax, vocabulary, etc.
* Learn_R_4_Packages\_.R - Information on important or interesting packages I find along the way.
* Learn_R_ref_HowTo_TBC -
This is what it was corrected to:
# This is what it was corrected to:
- Learn_R_ref_Intro_to.R - That's where you are now
- Learn_R_ref_Resources.html - This will be links to Videos, Blogs, and Websites about R
- Learn_R_ref_Basics_of.R - This will be covering the very basic elements of R syntax, vocabulary, etc.
- Learn_R_4_Packages\_.R - Information on important or interesting packages I find along the way.
- Learn_R_ref_HowTo_TBC -
I’m creating this as I’m learning R through Coursera. For information on the course I’m taking to kick-off this project:
Data Analysis with R Programming
[Data Analysis with R Programming](https://www.coursera.org/learn/data-analysis-r/home/welcome)
I will also be implementing strategies I find from any helpful resources, including the native help documentation, YouTube videos, TikTok posts, articles, and of course: GPT-4! I’ll link to anything I find that is useful.
One perfect example of a resource is the YouTube video: “Learn R in 39 minutes” by Equitable Equations. This video was comprehensive but not too long. The last portion of the video informed me about RMarkdown documents (which I’m using to write this now). Definitely subscribe to his channel.
With all that established, let’s get to learning R.
To get started on this reference guide, I asked ChatGPT-4 to summarize.
The “R” in R programming doesn’t officially stand for anything. The language was named “R” by its creators, Ross Ihaka and Robert Gentleman, from the University of Auckland, New Zealand. It can be viewed as a play on their names’ initial letter.
However, the language is also considered as an implementation of the S programming language, so sometimes “R” is referred to as a sort of play on the name “S”.
S was created by John Chambers and colleagues at Bell Laboratories.
R has become a go-to language for statistical computing and graphics, widely used among statisticians and data analysts.
IF you are reading this in RStudio Cloud / Posit Cloud or in GitHub or online in some way…but you want to use this reference guide to follow along and repeat the processes in your own, you will really want RStudio. While you can use RStudio Cloud/Posit Cloud, the native desktop environment is good to use and get used to.
If you are a GitHub user and you’re not reading this there, my profile is darrellwolfe, and my repository for this project is R_Analysis.
If you choose to download, you will want to install both: 1. R - That is the code and operating system information you need running in the background. 2. RStudio - That is the Graphic User Interface (GUI) that you will primarily interact with.
#### Download: [Download_R_And_RStudio_Here](https://posit.co/download/rstudio-desktop/)
# Inside a Doc.R document, use: Ctrl + (Mouse Left Click)
print("RStudio")
## [1] "RStudio"
To run code, place cursor in the same line as the code, press Ctrl+Enter (or, click “Run” icon at the top right of the .R docment box). Additionally, highlight multiple lines of code and then do either of the above.
In your session of RStudio (or RStudio Cloud) type:
To see which version of RStudio you are on, type ‘version’ (without the quotes) place cursor in the same line as the code, press Ctrl+Enter. You should get a result like this:
version
## _
## platform x86_64-w64-mingw32
## arch x86_64
## os mingw32
## crt ucrt
## system x86_64, mingw32
## status
## major 4
## minor 3.0
## year 2023
## month 04
## day 21
## svn rev 84292
## language R
## version.string R version 4.3.0 (2023-04-21 ucrt)
## nickname Already Tomorrow
Here are the things you NEED to know to get started. This won’t be comprehensive, just an overview of the basic syntax, vocab, etc.
NOTE: MAC vs PC - I am going to say Ctrl+, if you use Mac, you know to use Command instead.
To run a command, place your cursor in the line of code (or highlight if multiple lines) B. Press Ctrl+Enter (or Command+Enter for Mac)
Except website links, for those [Ctrl+(Left Click with Mouse)]
Example:
print("Hello, World!") # Type this in your session, place your cursor on the line, Press Ctrl+Enter
## [1] "Hello, World!"
This is based on the default layout, you can also move these around:
This text-box-area you may do most of your active writing within. You may have several files/windows open at once, writing code in each. You can have files/windows of different types (R, SQL, HTML, Text, VBA, etc.) open at once.
If you use Ctrl+F - a Find and Replace capability will work just like it does in Word or Excel.
If you run the following three in order, the “view” function will open a tab in this section of RStudio
library(tidyverse) # Cursor on this line, Press Ctrl+Enter
library("palmerpenguins") # Cursor on this line, Press Ctrl+Enter
view(penguins) # Cursor on this line, Press Ctrl+Enter
Below the Source is the “Console” area. Commands can be typed directly into the console too; however, they don’t get saved that way. Commands may give a Warning or Error, read those. Command outputs show in the console, that is its primary function.
NOTE: Commands ARE case sensitive library() and Library() are not the same.
Environment tab will tell you which libraries and datasets you have open (loaded)
History will tell you what you have been doing.
Connections will tell if you have a live connection to the database.
Git (if a GitHub project is loaded) is to create a new Project with an existing GitHub repo.
To do this: File > New Project > Version Control > Git (then paste link from GitHub)
To open an existing GitHub Project File > Open Project (navigate to the folder, click on the “NameOfFile.R” file) Tutorials - will walk you through the basics of R.
Lets you navigate all the files on your PC.
Note: You can import a .csv or .xls file from this section.
To do this: Navigate to the file from within the Files window, right click file, “Import”
Plots is where your visuals will appear once you create them.
Packages - Lists your packages, click the name to read about the package.
Help - Type a “?” in front of any command or name, run the command, info will appear in Help.
This shows datasets and values and stored values. If you set x <- 10, it will show that x equals ten.
My files are constantly evolving. Even since taking this screenshot last night, it changed. However, this is where you fill find all the files you have written. You can code in R, RMarkdown, SQL, Python, vba and others, and have them all linked to your GitHub code base.
Plots is where your visuals will appear once you have run them.
Optional but recommended. If you create a GitHub Repository, then link that repo to RStudio, you can push all your code to the cloud easily. This way, you can also recover it from anywhere and share it with others. This my R_Analysis repo here.
At its core, R is a place to build and run RStudio R code. However, RStudio has evolved to be a full-service code editor (R, SQL, Python, HTML, and more). I have grown to enjoy it more than Visual Studio.
Even more basic than R, RStudio can operate as a calculator.
Here are few intro to RStudio items that you can use right now on your own session just to get used to running commands or math in the editor.
1+1 #Now run this calculation, place cursor on this line and then Ctrl+Enter
## [1] 2
Note: To assign a variable use (<-). While you can use (=), don’t for reasons I don’t fully understand yet.
x <- 10 #This assigns the variable, place cursor on this line and then Ctrl+Enter
10 * x #Now run this calculation, place cursor on this line and then Ctrl+Enter
## [1] 100
GPT-4 on vectors: ” In R, a vector is a sequence of data elements of the same basic type. Members in a vector are officially called components. However, we will often use the term “elements” interchangeably.
There are two key points here:
The “c()” function in R is used to create vectors. The “c” stands for concatenate, which means to join or link things together in a chain or series.
Let’s take your example, c(10,1,5,60). This code is
creating a vector in R with four elements: 10, 1, 5, and 60. This vector
is of numeric type, meaning all elements are numbers.
You can assign this vector to a variable like this:
my_vector <- c(10,1,5,60)
Now, my_vector holds your vector and you can use it in
calculations, pass it to functions, etc. For example, if you want to
calculate the sum of all elements in the vector, you would use the
sum() function:
sum(my_vector)
This would output 76, the sum of 10, 1, 5, and 60.
R has many functions to manipulate vectors, including mathematical operations, sorting, and statistical analysis. These are one of the basic data structures of R and fundamental to how it operates. ”
my_vector <- c(10,1,5,60) #Now run this calculation, place cursor on this line and then Ctrl+Enter
sum(my_vector) #Now run this calculation, place cursor on this line and then Ctrl+Enter
## [1] 76
If you need to understand something or how a code works you can place a question mark in front of any code and run that. It will show up in your Help tab at the bottom right.
?print() #Now run this calculation, place cursor on this line and then Ctrl+Enter
?c() #Now run this calculation, place cursor on this line and then Ctrl+Enter
?library(tidyverse) #Now run this calculation, place cursor on this line and then Ctrl+Enter
There are thousands!
Packages are built either by Posit directly or by various third parties and users. One of the (if not the) first packages you will install is “tidyverse”, which has become an industry standard package, providing several functionalities that are used frequently.
Packages come with datasets, new functionality, and other items. In some rare cases, packages can come with conflicting functions (meaning two packages could assign the same name to different functions within the package), but most of this is circumvented by which library you have loaded.
On your machine, run the following:
install.packages(“tidyverse”) then Ctrl+Enter
You should see this:
install.packages("tidyverse") # You can place your cursor anywhere on this line and it would run in your RStudio
To see which packages are already installed, place cursor in the same line as the code, press Ctrl+Enter: installed.packages()
installed.packages() #Ctrl+Enter}
# Example 
The print out is long on installed.packages, but it will tell you every package you have on your system.
To install packages, place cursor in the same line as the code, press Ctrl+Enter: “tidyverse” is the first package you should install, it is one of the first everyone installs after the basic load-in.
install.packages("tidyverse")
To load the library from installed packages, place cursor in the same line as the code, press Ctrl+Enter: This is your first installed library, you will use this often. More on that later.
library(tidyverse)
Once packages are installed, they are on your machine unless you delete them. However, the individual functions of any given package won’t be available unless you load them into the current session. Consider each R file-type “Document_Name.R” to be a new blank slate.
Once you load the library, those functions and datasets are available to use within the session. This may be the first thing you want to do after naming your .R file.
You will load the packages you want to use by using “library”.
Note: CAPITAL vs lowercase matters in R. library() <is not equal to> Library()
Example:
Library(tidyverse) #Ctrl+Enter This probably gives you an error.
vs
library(tidyverse) #Ctrl+Enter This probably works.
## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
To change the appearance of your desktop, or theme do the following:
From the Tools menu at the top of the page, select “Global Options” at the bottom of the list.
From the left hand menu, select “Appearance”.
Select the editor themes until you find one you like.
Click Apply, then click OK to save.
….
2. COMMENTS
The hash-tag/number sign (#) is a comment marker, R ignores everything after Comments #