--- title: "Learning R" author: Jason Dinh and Jesse Granger output: html_document: df_print: paged --- #Assigning Values to Objects Welcome to R Studio! In this worksheet we will practice creating and plotting variables. WPlease read through this worksheet carefully so that you will be well prepared to participate in class!R Recall that we write code within code chunks, and we provide helpful notes outside of the code chunks (like here!). Try running this chunk of code! ```{r} A <- 1 A ``` Congratulations! You just created an object! Look in the Environment/History panel and notice you have a new Value called A. We assigned this object a value of 1. The code is very simple: The ‘A’ is our object's name, the ‘<-‘ is our operator that assigns the value, and our value is ‘1’. In English, the code reads "A get's the value 1". THen, we recall A in line 18 to make sure we get the correct output printed below the code chunk. You can then later call on this variable and do calculations with it! For example, if we want to know what A plus 10 is, we can try: ```{r} A + 10 ``` We can also assign objects a series of numbers called a vector. For example, we can assign B a series of integers from 1 to 5 using the colon operator: ```{r} B <- 1:5 B ``` Vectors don't need to be an any particular pattern! We can manually assign values to a vector using c(): ```{r} C <- c(3, 66, 32.1, 3, 900) C ``` We can use functions to do math on our vectors! Function names are followed by parentheses, and inside the parentheses, we include arguments to tell the function what exactly to do. For example, we can use the function mean( to calculate the mean value of a vector. Inside the mean() function, we need to tell R which vector to calculate a mean on. Run the code below to calculate the mean of vector B. You should get 3. ```{r} mean(B) ``` #Installing and Loading Packages We will be using some special functions in our class, so the first step we need to do is download the functions we need. These functions can be found in special packages made by other smart R users. We will be using the package 'circular' for our statistics. To download this package, you need to use the install.packages() function. It may take a few minutes to download. Don't worry! ```{r} install.packages("circular") ``` Once you have downloaded the package, you still need to "activate" it in order for you to use the functions. To do this, you use the library() function in the code below. It will add the package into your library of functions. ```{r} library(circular) ``` We won't be using circular in this worksheet, but you will be using in class on Wednesday. #Plotting Data Finally, let's play with some data to make a basic plot. First, we will assign two new variables: X will be a series of numbers from 1 to 10, and Y will be a series of numbers from 10:1. ```{r} X <- 1:10 X Y <- 10:1 Y ``` If we build a scatter plot showing X plotted against Y, we should see a strong negative correlation. We create scatter plots by using the plot() function. Within the plot() function, we have first tell the function which vector to plot as the X axis and then which vector to plot as the Y axis. These values occurring within the function's parentheses are called arguments. For a complete explanation of the function and all of its arguments, type ?plot() into the console.This will open the documentation for the function in your help window. Run the following code chunk to plot X against Y. ```{r} plot(X, Y) ``` Hopefully, you got a plot that looks like 10 points occuring in a straight line in a negative relationship. Now that you know how to navigate RStudio, assign variables, use functions, and plot data, you should be able to successfully complete our activity on Wednesday!