class: center, middle, inverse, title-slide .title[ # R basics ] .author[ ### S. Mason Garrison ] --- layout: true <div class="my-footer"> <span> <a href="https://psychmethods.github.io/coursenotes/" target="_blank">Methods in Psychological Research</a> </span> </div> --- --- # More Accessible Resources - [R Graph Catalog](http://shinyapps.stat.ubc.ca/r-graph-catalog/) - [intRo](http://www.intro-stats.com/) - .hand-pink[[Data Science for Psychologists](https://datascience4psych.github.io/DataScience4Psych/)] --- class: center, middle # Wrapping Up... --- # R Basics --- # R Basics - R is a powerful language for statistical computing and graphics - It's free, open-source, and has a large community of users and developers --- # R Installation and Setup - Download R from CRAN (Comprehensive R Archive Network): https://cran.r-project.org/ - Choose the version for your operating system - RStudio is a popular IDE for R, available at: https://www.rstudio.com/products/rstudio/download/ --- # R Console - After installation, you can start R or RStudio - You'll see a console where you can enter commands - Try a simple calculation: ``` r 2 + 3 ``` ``` ## [1] 5 ``` --- # Variable Assignment - Use `<-` or `=` to assign values to variables Variable names should be descriptive and use `under_scores` or `camelCase` ``` r x <- 10 y = 20 total_sum <- x + y totalSum <- x + y print(total_sum) ``` ``` ## [1] 30 ``` --- # Basic Data Types - Numeric: 1.5, 2, 3.14 - Character: "hello", 'world' - Logical: TRUE, FALSE --- # Vectors - Create vectors using `c()` function - Vectors can contain elements of the same type ``` r numeric_vector <- c(1, 2, 3, 4, 5) character_vector <- c("a", "b", "c") print(numeric_vector) ``` ``` ## [1] 1 2 3 4 5 ``` --- # Basic Plotting in R .pull-left[ - R has built-in plotting functions - These are useful for quick visualizations ] .pull-right[ ``` r x <- 1:10 y <- x^2 plot(x, y, main="Simple Plot", xlab="X axis", ylab="Y axis") ``` <img src="data:image/png;base64,#rbasics_files/figure-html/unnamed-chunk-6-1.png" width="90%" style="display: block; margin: auto;" /> ] --- # Using Functions .pull-left-narrow[ - R has many built-in functions - You can also create your own functions ] .pull-right-wide[ ``` r # Built-in function mean(c(1, 2, 3, 4, 5)) ``` ``` ## [1] 3 ``` ``` r # Custom function square <- function(x) { return(x^2) } square(4) ``` ``` ## [1] 16 ``` ] --- # Comments - All text after the pound sign "#" within the same line is considered a comment. > 1 + 1 # this is a comment [1] 2 --- # Getting Help - R provides extensive documentation. - Use `?` or `help()` to get information about functions - For example, entering `?c` or `help(c)` at the prompt gives documentation of the function `c` in R. - Example: ``` r ?mean help(plot) ``` --- # Getting Help > `help(c)` If you are not sure about the name of the function you are looking for, you can perform a fuzzy search with the apropos function. > `apropos("nova")` [1] "anova" "anova.glm" .... --- # Wrapping Up...