Basic R

Basic expressions

An expression is a set of commands that returns a value. For example let’s use R to convert a mass of 50 kg into pounds by multiplying it by 2.2.

Click Run Code to run the following R code.


    
  • 50 * 2.2 is an expression
  • That number 110 that results from running the code is the resulting value
  • Try changing the code above by changing the number 50 to other weights in pounds to find out the equivalent weight in kilograms

Variables

  • To save the values we calculate for later use we use variables
  • A variable is a name that has a value associated with it
  • We can assign a value to a variable using <-

    
  • No value is shown when we run this code because the value is stored in the variable
  • To see the value we can run the name of the variable on it’s own

    
  • The variable works just like the value itself

    
  • The value associate with the variable won’t change unless you assign a new value to it directly
  • If we do a calculation with the variable and check it’s value again

    
  • We can see that it doesn’t change
  • If we want to update the variable we have to store a new value

    
Exercise

Here is a small program that converts a mass in kilograms to a mass in grams and then prints out the resulting value.


    

Create similar code to convert a mass in pounds to a mass kilograms. * Create a variable to store a body mass in pounds. Assign this variable a value of 3.5 (an appropriate mass for a Sylvilagus audubonii). * Convert the variable from body mass in pounds to body mass in kilograms (by dividing it by 2.2046), and assign it to a new variable. * Print the value of the new variable to the screen.

Functions

  • A function is a complicated expression.
  • Command that returns a value

    
  • A function call is composed of two parts.
    • Name of the function
    • Arguments that the function requires to calculate the value it returns.
    • sqrt() is the name of the function, and 49 is the argument.
  • We can also pass variables as the argument

    
  • Another function that we’ll use a lot is str()
  • All values and therefore all variables have types
  • str, short for “structure”, lets us look at them

    
  • Another data type is for text data
  • We right text inside of quotation makes

    
  • If we look at the structure of some text we see that it is type character

    
  • Functions can take multiple arguments.
    • Round weight_lb to one decimal place
    • Typing round() shows there are two arguments
    • Number to be rounded and number of digits

    
  • Functions return values, so as with other values and expressions, if we don’t save the output of a function then there is no way to access it later
  • It is common to forget this when dealing with functions and expect the function to have changed the value of the variable
  • But looking at weight_lb we see that it hasn’t been rounded

    
  • To save the output of a function we assign it to a variable.

    

Do Exercise 4.1-4.3 - Built-in Functions