class: title-slide, bottom, right background-image: url(https://images.unsplash.com/photo-1467545159547-1b93b24406ae?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1623&q=80) background-size: cover ### Welcome to R # ### **Miriam Lerma**<br> April 2023 --- name: index class: title-slide, inverse # Index - [Install or update R](#install-r) - [What is R](#parts) - [RStudio and identify the parts](#r-parts) - [Data types](#data-types) - [Errors](#errors) - [Packages](#packages) - [Contact](#out) --- class: title-slide, inverse # Today .pull-left[ **Your profile** - Interest in learning R (or other ways to use it). **Goals of today** - Identify the parts of RStudio - Be able to load and download data - Feel comfortable using R - Hopefully we will cover all the topics **Pauses and questions** - There will be several pauses - You can stop me to ask questions or - Questions can be made [at this link too
]( https://docs.google.com/document/d/1uG7a2_hkdaKQm5gKXRBFf6gcyoUBan2e69gL3ZKcwg8/edit?usp=sharing) ] -- .pull-right[ <br> <br> <br> <br> <br> <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/001Approximate.png?raw=true" height="200" /> ] --- class: title-slide, inverse # References - R for Data Science [
R4DS](https://r4ds.had.co.nz/) - Data Carpentries [
Carpentries](https://datacarpentry.org/genomics-r-intro/00-introduction/index.html) - From Zero to Shero by RLadies [
Zero to Hero](https://github.com/rladies/meetup-presentations_freiburg) - Images from [
Unsplash](https://unsplash.com/) [
Allison horst](https://twitter.com/allison_horst?lang=es) - Other resources [YaRrr](https://bookdown.org/ndphillips/YaRrr/the-four-rstudio-windows.html)<br> [Data structure](https://swcarpentry.github.io/r-novice-inflammation/13-supp-data-structures/)<br> [Assignment operators](https://stackoverflow.com/questions/1741820/what-are-the-differences-between-and-assignment-operators-in-r)<br>[R inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf)<br> [Why they are not the same](https://www.roelpeters.be/the-difference-between-arrow-and-equals-assignment-in-r/)<br> [TidyTuesday](https://github.com/rfordatascience/tidytuesday) --- class: title-slide, inverse, bottom background-image: url(https://images.unsplash.com/photo-1538766017398-415434a31a5b?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80) background-size: cover # Lets begging! --- name: install-r # 1. Install R First time installing R To install R and Rstudio you can visit the page [posit
](https://posit.co/download/rstudio-desktop/) <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/002Posit.png?raw=true" height="280" /> --- ## First time installing R Once you click download and install R, you will find several links. If you are the first time installing you likely need to choose the first link. <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/003LoadR.png?raw=true" height="300" /> --- ## Update R If you already have R and R-Studio Check which version of R you are using: ```r sessionInfo() ``` To see if you need to update you can use: ```r install.packages("installr") library(installr) updateR() ``` **Do you need an update?** <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/003UpdateR.png?raw=true" height="120" /> --- ## Update R If you opt to update R, one way to do it is going to the [installation page
](https://www.r-project.org/) You will likely select >4.3 (in 2023) <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/004UpdateR.png?raw=true" height="300" /> --- ## Update R If you decide to update your R, its better to do it in the Rgui and not R-Studio .pull-left[ Open Rgui <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/005UpdateR.png?raw=true" height="150" /> Once in the Rgui, write updateR() <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/006UpdateR.png?raw=true" height="180" /> ] -- .pull-right[ Give it some time <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/007UpdateR.png?raw=true" height="200" /> ] --- ## Update R Select YES if you already had R packages installed in your previous version, and do not want to install each package one by one again. <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/008UpdateR.png?raw=true" height="100" /> <br> <br> By March 2023 4.2.3 was the newest version of R. <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/009UpdateR.png?raw=true" height="100" /> **Did you notice the difference?** --- ## Update R-Studio One thing is updating R and another one to update R-Studio If you already have Rstudio and want to see if you need to update you can go to: .pull-left[ Help>Check for updates <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/010UpdateRStudio.png?raw=true" height="300" /> ] .pull-right[ Outdated <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/011UpdateRStudio.png?raw=true" height="100" /> Up to date <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/012UpdateR.png?raw=true" height="100" /> ] --- name:parts class: title-slide, bottom background-image: url(https://images.unsplash.com/photo-1517056338492-cb43f18f72a0?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80) background-size: cover # <span style=" font-weight: bold; color: #e5e5e5 !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #003049 !important;" >Parts of R</span> --- ## 2. What is R? R is a programming language developed for statistical analyses by the Ross Ihaka and Robert Gentleman (New Zealand) in 1996. .center[ <img src="https://www.r-project.org/logo/Rlogo.svg" width="50" height="50" /> ] -- .center[ <img src="https://www.nature.com/news/polopoly_fs/7.22632.1418912921!/image/TB2.jpg_gen/derivatives/fullsize/TB2.jpg" height="300" /> [Sylvia Tippmann]("https://www.nature.com/articles/517109a") ] --- ## 2.2 Why R? .pull-left[ **Advantages** - Its free - A lot of tools - Flexible data visualization **For science, is reproducible** - You can save all the steps you followed, making it verifiable - Instead of clicking on buttons the instructions can be save in sequence, helping save time and reduce errors ] -- .pull-right[ **Disadvantages** - Has a learning curve - It is like learning a new language <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/014LearningCurve.jpg?raw=true" height="200" /> ] --- ## 2.3. R vs RStudio. What are the differences between R and RStudio? **R** is the programming language. **RStudio** is an IDE: Integrated Development Environment. R is the engine and RStudio is the steering wheel <br> .center[ <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/018Car.png?raw=true" height="300" /> ] --- ## 2.4 Using R First time opening RStudio looks like this: <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/019Rparts.png?raw=true" height="350" /> To have a code editor: Open: File > New File > Rscript --- ## 2.4. Using RStudio. In the **source** section the code can be edited and saved. <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/020Rparts.png?raw=true" height="350" /> --- ## 2.4. Using RStudio. To change the order of the panes, you can change the pane layout .pull-left[ Click on the squares > Or in Tools> Global Options select **Pane Layout** <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/021PaneLayout.png?raw=true" height="300" /> ] .pull-right[ Select your preferred order <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/022PaneLayout.png?raw=true" height="300" /> ] --- ## 2.4 Using RStudio Testing the console R is like a calculator - Write in the console ```r 3*4 ``` - Write in the editor ```r 3*4 ``` -- In the editor: ctrl + enter -- - Now write in the editor, dont forget to click ctrl+enter ```r Result<-3*4 ``` What happend in the **environment**? --- name: data-types ## 2.5. Data types Some data types: - Numeric ```r class(1.4) ``` ``` [1] "numeric" ``` - Characters ```r class("Miriam") ``` ``` [1] "character" ``` --- ## 2.5. Data types Why if we use quotes ""? ```r class("1") ``` ``` [1] "character" ``` - Logical ```r 3 == 4 ``` ``` ## [1] FALSE ``` ```r 3 < 5 ``` ``` ## [1] TRUE ``` There are many others, that would be relevant later on. Source: [Data structures by the Carpentries](https://swcarpentry.github.io/r-novice-inflammation/13-supp-data-structures/) --- ## 2.6. Data frame Assign symbol ```r <- ``` This symbol is frequently use, and is important that you find it in your keyboard as soon as possible. - The equal **=** might behave differently than the assign symbol <br> See here: [Assignment operators](https://stackoverflow.com/questions/1741820/what-are-the-differences-between-and-assignment-operators-in-r), [R inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf), [Why they are not the same](https://www.roelpeters.be/the-difference-between-arrow-and-equals-assignment-in-r/) .center[ <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/024Assign.jpg?raw=true" height="200" /> ] --- ## 2.6. Data frame Lets use the arrow symbol. ```r Names<-c("Miriam","Jette","Kai","Nele","Jana","Kristin","Karena","Nico","Philipp","Mads") class(Names) ``` ``` ## [1] "character" ``` ```r Names<-as.data.frame(Names) class(Names) ``` ``` ## [1] "data.frame" ``` Now in the **environment** click twice and look at your data frame. --- ## 2.6. Data frame Assign vectors to a column or check a column contents The symbol $ is in R ```r $ ``` Lets create a new column ```r Names$LastName<-c("G","A","T","D","L","S","H",NA,"M","L") ``` ```r head(Names,5) ``` ``` Names LastName 1 Miriam G 2 Jette A 3 Kai T 4 Nele D 5 Jana L ``` --- ## 2.7. Force classes Force column type ```r my_number<-'1' ``` ```r class(my_number) ``` ``` ## [1] "character" ``` ```r my_number<-as.numeric(my_number) ``` ```r class(my_number) ``` ``` ## [1] "numeric" ``` --- ## 2.7. Force classes Our favorite class ```r my_timestamp<-'2015-06-12 05:00:00' ``` ```r class(my_timestamp) ``` ``` ## [1] "character" ``` ```r my_timestamp <- as.POSIXct(strptime(my_timestamp, "%d/%m/%Y %H:%M:%S "), "GMT") ``` ```r class(my_timestamp) ``` ``` ## [1] "POSIXct" "POSIXt" ``` Timestamp should be a "POSIXct" "POSIXt" for R to understand the are date and time --- ## 2.8. Code editor To add text in the **editor** we use the symbol **#** (hashtag) ```r #Here you created a data frame with names, but there is NA ``` R will ignore the text that has a # but these are very important for us to make annotations. It is a **good practice** to write comments. Either for us to share the script, or for you to remember what you were doing. This is specially important if you get corrections, or if you want to repeat the analyses. --- name:errors class: title-slide, inverse, bottom background-image: url(https://images.unsplash.com/photo-1555861496-0666c8981751?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80) background-size: cover # Errors --- ## 2.9. Type of errors Errors are part of life. R very often is trying to tell us how to solve the error. - For example: ```r 3 = 4 ``` Error in 3 = 4 :left-hand side to assignment (do_set) Other messages that might be red or orange are: - Warnings, I will do it, but Im not responsible of what you are doing. - Messages, hey I just want to let you know this happend. --- ## 2.9. The most common errors Whats going on here Remove the # and find the error. ```r #c("Miriam","Bety","Angel","Denise""Pamela") ``` -- - Missing a comma -- ```r #c("Alvaro","Gabriela","Juan",,"Lisset") ``` -- - Too many commas -- ```r #c("Majo","Irma","Paulina" ``` -- - Missing a parenthesis -- ```r #c("Majo","Irma,"Paulina") ``` -- - Missing a quote (") --- ## 2.9. Debugging bingo <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/023DebuggingBingo.jpg?raw=true" height="450" /> Source: Dr. Ji Son --- ## 2.10. Name consistency Many errors are going to be marked by a red x in the **code editor**. -- But many will not. What is wrong with these? ```r #name ``` -- - If our goal is to use of data frame from the environment it needs to be written exactly the same **Names**. -- To reduce this source of error, is important to be consitent on how we name the objects. Some considerations are: - R will not take objects that start with a number (Eg."1Names"). - But the number can be at the end of the name (Eg."Names1"). - Try to be consistent E.g. ("my_names"). E.g. ("my.names"). --- class: title-slide # Pause - Open RStudio
<br> - Write something in the console<br> - Create a data frame with characters and numbers<br> .pull-left[ Until here: - [Index](#index) - [What is R](#r-story) - [RStudio and identify the parts](#r-parts) - [Data types](#data-types) - [Errors](#errors) ] .pull-right[ Next part: - [Packages](#packages) - [Directories](#directories) - [Contact](#out) ] --- name: packages class: title-slide, inverse, bottom background-image: url(https://images.unsplash.com/reserve/oIpwxeeSPy1cnwYpqJ1w_Dufer%20Collateral%20test.jpg?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1015&q=80) background-size: cover # <span style=" font-weight: bold; color: #e5e5e5 !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #003049 !important;" >Packages</span> --- ## 3.1. Packages - Packages are a collection of functions - Packages are installed one time, but need to be loaded every time you need them. An analogy is that install.packages() is downloading an app, and library() is like opening the app every time you want to use it. To install packages using the **workspace** Go to Packages>Install and write tha name of the package <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/020Rparts.png?raw=true" height="300" /> --- ## 3.1. Packages - To install in the **console** write: install.packages(""). To load a package: library(""). Some packages that we will use: ```r install.packages("cowsay") install.packages("here") install.packages("tidyverse") ``` 🕙 Tidyverse is a compilation of packages, this one might take some time. Note: It probably will give us a warning, remember that a warning might be just information. In this case is telling us what the package contains. --- ## 3.1. Packages ```r say(what = "Now you can download packages!", by = "cat") ``` nothing happend? We should load the library first. --- ## 3.1. Packages ```r library("cowsay") say(what = "Now you can download packages!", by = "cat") ``` ``` -------------- Now you can download packages! -------------- \ \ \ |\___/| ==) ^Y^ (== \ ^ / )=*=( / \ | | /| | | |\ \| | |_|/\ jgs //_// ___/ \_) ``` --- ## 3.2. Directories Where are my files?. By hand: Click on the **workspace** > Files > More > Set As Working directory <img src="https://github.com/MiriamLL/R_intro/blob/master/Images/019Rparts.png?raw=true" height="350" /> This is practical, but needs to be done every time that we enter R and therefore be time consuming. --- ## 3.2. Directories Where I am working? ```r getwd() ``` ```r setwd("paste your directory name here") ``` Note, that you will need to copy and paste the directory. If you share your script or are working in another computer, everyone will have different directories. --- ## 3.2. Directories Where I am? ```r install.packages('here') ``` ```r library(here) ``` ```r here() ``` Advantages: - **here** allows to use the folder where we saved the script - You can share your script and your collaborators dont need to copy and paste their directory - If the folder name is changed or the folder is change from place it would still work --- class: title-slide # Pause - Open RStudio
<br> - Install and load packages<br> - Check your directory<br> Make comments or suggestions [here
]( https://docs.google.com/document/d/1uG7a2_hkdaKQm5gKXRBFf6gcyoUBan2e69gL3ZKcwg8/edit?usp=sharing) .pull-left[ Until here: - [Index](#index) - [What is R](r-story) - [RStudio and identify the parts](r-parts) - [Data types](data-types) - [Errors](errors) - [Packages](packages) - [Directories](directories) ] .pull-right[ Next part: - [Contact](#out) ] --- name: out class:inverse background-image: url(https://images.unsplash.com/photo-1557075877-3e112375a834?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80) background-size: cover # Back to - [Index](#index) - [What is R](#r-story) - [RStudio and identify the parts](#r-parts) - [Data types](#data-types) - [Errors](#errors) - [Packages](#packages) - [Directories](#directories) .right[ Make comments or suggestions [here
]( https://docs.google.com/document/d/1uG7a2_hkdaKQm5gKXRBFf6gcyoUBan2e69gL3ZKcwg8/edit?usp=sharing) This materials are free of use <br> Download the presentation here: [
github](https://github.com/MiriamLL/R_intro) and [
webpage](https://www.miriam-lerma.com/posts/2023-04-18-intro-to-r/) ] ] <br> .center[ <h3>
[Home ](https://www.miriam-lerma.com/teaching.html) ]