Previous: R Objects Up: R Objects Next: Data Structures


Scalar Values

R uses several classes of scalar values, from which it constructs larger data structures. R is highly class-dependent: certain operations will only work on certain types of values or certain types of data structures. We list the three basic types of scalar values here for your reference:

  1. Numeric is the default value type for most numbers. An integer is a subset of the numeric class, and may be used as a numeric value. You can perform any type of math or logical operation on numeric values, including:
    > log(3 * 4 * (2 + pi))         # Note that pi is a built-in constant, 
       [1] 4.122270                 #   and log() the natural log function.
    > 2 > 3                         # Basic logical operations, including >,
       [1] FALSE                    #   <, >= (greater than or equals), 
                                    #   <= (less than or equals), == (exactly 
                                    #   equals), and != (not equals). 
    > 3 >= 2 && 100 == 1000/10      # Advanced logical operations, including
       [1] TRUE                     #   & (and), && (if and only if), | (or), 
                                    #   and || (either or).
    
    Note that Inf (infinity), -Inf (negative infinity), NA (missing value), and NaN (not a number) are special numeric values on which most math operations will fail. (Logical operations will work, however.)

  2. Logical operations create logical values of either TRUE or FALSE. To convert logical values to numerical values, use the as.integer() command:
    > as.integer(TRUE)
       [1] 1 
    > as.integer(FALSE)
       [1] 0
    
  3. Character values are text strings. For example,
    > text <- "supercalafragilisticxpaladocious"
    > text
    [1] "supercalafragilisticxpaladocious"
    
    assigns the text string on the right-hand side of the <- to the named object in your workspace. Text strings are primarily used with data frames, described in the next section. R always returns character strings in quotes.



Gary King 2011-11-29