Data Types

Introduction

There are different data modes used in R. The mode of a variable will for example determine what kind of operators that can be done on it. At the end of this exercise you should know:

Data types

From the lecture you might remember that all elements in any data stuctures found in R will be of a certain type (or have a certain mode). The four most commonly used data types in R are: logical, integer, double (often called numeric), and character. The names hints at what they are.

In many cases the mode of on entry is determined by the content so if you save the value 5.1 as a variable in R, the variable will automatically be recognised as numeric. If you instead have a text string like “hello world” it will have the mode character. Below you will also see examples of how you can specify the mode and not rely on R inferring the right mode based on content.

Exercise: Basic operations and data types in R

In all exercises on this course it is important that you prior to running the commands in R, try to figure out what you expect the result to be. You should then verify that this will indeed be the qresult by running the command in an R session. In case there is a discrepency between your expectations and the actual output make sure you understand why before you move forward. If you can not figure out how to, or which command to run you can click the key to reveal example code including expected output. If you after peaking at the code and trying out things on your own have a hard time understanding what is going on, ask the TAs or or your someone sitting next to you who might have wrapped their head around the issue.

Also note that in many cases there multiple solutions that solve the problem equally well.

We do recommend to write all code in a R markdown document in R-studio as that will at the end of the course be your own R tutorial with comments and code solutions.

Create and retrieve information about variables in R

Open R-studio and make sure to set your working directory. Double check that you do not have stored objects in your current session with the following command. This will list all objects that you have in your current R session.

ls()

In case you have objects that you want to remove from the current session you can do so with the rm function. NB! This command will remove all objects available in your current session.

rm(list = ls())

This command uses commands that we have not talked about yet. If you do not understand how it works now, you will do so after tomorrows lectures and exercises.

  1. Create variables var1 and var2 and initialize them with two integers of choice.
    :key: Click to see how
    var1 <- 11  
    var2 <- 34  
    


  2. Add the two variables and save them as a new variable named var3 and print the result.

    :key: Click to see how
    var3 <- var1 + var2  
    var3  
    
    [1] 45  
    


  3. Check the class, mode, and type for var1, var2, var3 and π (is found under the variable name pi in R)
    :key: Click to see how for var1
    mode(var1)  
    class(var1)  
    typeof(var1)  
    	
    [1] "numeric"  
    [1] "numeric"  
    [1] "double"  
    
    :key: Click to see how for π
    mode(pi)  
    class(pi)  
    typeof(pi)  
        
    [1] "numeric"  
    [1] "numeric"  
    [1] "double"  
    


  4. Create two character variables containing a text of choice.
    • check mode, class, and type of the first one
      :key: Click to see how create character variables
      text1 <- "test1"  
      text2 <- "test2"  
      
    • add var1 to it and report the result
      :key: Click to see how to add variables
      text1 + var1  
      	
      Error in text1 + var1 : non-numeric argument to binary operator  
      


  5. Cast var3 to integer, cast an integer variable to double, cast a string to a double.
    :key: Click to see how
    as.integer(var3)  
    i <- 175  
    as.double(i)  
    as.double(text1)  
        
    [1] 45  
    [1] 175  
    [1] NA  
    Warning message:  
    NAs introduced by coercion  
    


  6. Report floor and ceiling of π and round π to 3 decimal places.
    :key: Click to see how
    floor(pi)  
    ceiling(pi)  
    round(pi, digits = 3)  
        
    [1] 3  
    [1] 4  
    [1] 3.142  
    


  7. Is floor of π an integer?
    :key: Click to see how
    	
    is.integer(floor(pi))  
    
    [1] FALSE  
    


  8. Treat ‘3.56437’ string as number.
    :key: Click to see how
    	
    as.numeric('3.56437')  
    


  9. Divide ∞ by - ∞
    :key: Click to see how
    	
    -Inf/Inf  
        
    [1] NaN  
    


  10. Create two freely chosen complex numbers.
    • Check that they are complex indeed.
    • Add, multiply and divide one by another.
    • Add an integer to their sum.
    :key: Click to see how
    c1 <- 23 + 4i  
    c2 <- -15 - 7i  
    is.complex(c1)  
    is.complex(c2)  
    c1 + c2  
    c1 / c2  
    c1 + c2 + 7  
    [1] TRUE  
    [1] TRUE  
    [1] 8-3i  
    [1] -1.361314+0.368613i  
    [1] 15-3i  
    


  11. Print a truth table for OR (for three distinct logical values). Read about truth tables here https://en.wikipedia.org/wiki/Truth_table
    :key: Click to see how
    	
    x <- c(NA, FALSE, TRUE)  
    names(x) <- as.character(x)  
    outer(x, x, "|")  
    	
    NA FALSE TRUE  
    NA      NA    NA TRUE  
    FALSE   NA FALSE TRUE  
    TRUE  TRUE  TRUE TRUE  
    


  12. Multiply a logical TRUE by a logical FALSE. Rise the logical true to the 7-th power.
    :key: Click to see how
    TRUE * FALSE  
    T^7  
    [1] 0  
    [1] 1  
    


  13. Create two character variables containing two verses of your favorite song.
    • concatenate the two variables,
    • paste the variables with ‘*’ as separator.
    • find if ‘and’ occurs in the second line,
    • substitute a word for another,
    • extract substring starting at the 5th character and 5 characters long.
    :key: Click to see how
    	
    line1 <- "Hello darkness my old friend"  
    line2 <- "I've come to talk to you again"  
    paste(line1, line2, sep = "")  
    paste(line1, line2, sep = "*")  
    grep('and', line2)  
    sub('Hello', 'Goodbye', line1)  
    substr(line1, 5, 5 + 5)  
        
    [1] "Hello darkness my old friendI've come to talk to you again"  
    [1] "Hello darkness my old friend*I've come to talk to you again"  
    integer(0)  
    [1] "Goodbye darkness my old friend"  
    [1] "o dark"  
    


R Environment