There are five types of constants: logical,numeric,integer,complex and string. In addition, there are four special constants, NULL, NA,Inf, and NaN. NULL is used to indicate the empty object. NA is used for absent (“Not Available”) data values. Inf denotes infinity and NaN is not-a-number
Logical constants are either TRUE or FALSE T or F
class(TRUE)
## [1] "logical"
mode(TRUE)
## [1] "logical"
typeof(TRUE)
## [1] "logical"
storage.mode(TRUE)
## [1] "logical"
Numeric constants follow a similar syntax to that of the C language. They consist of an integer part consisting of zero or more digits, followed optionally by ‘.’ and a fractional part of zero or more digits optionally followed by an exponent part consisting of an ‘E’ or an ‘e’, an optional sign and a string of one or more digits. Either the fractional or the decimal part can be empty, but not both at once.
Valid numeric constants: 1 10 0.1 .2 1e-7 1.2e+7
Numeric constants can also be hexadecimal, starting with ‘0x’ or ‘0x’ followed by zero or more digits, ‘a-f’ or ‘A-F’. Hexadecimal floating point constants are supported using C99 syntax, e.g.’0x1.1p1’.
class(1)
## [1] "numeric"
class(1.)
## [1] "numeric"
class(1. )
## [1] "numeric"
class(1.0)
## [1] "numeric"
class(.1)
## [1] "numeric"
class( .1)
## [1] "numeric"
class(0.1)
## [1] "numeric"
class(1e7)
## [1] "numeric"
class(1E7)
## [1] "numeric"
class(1e-7)
## [1] "numeric"
class(1e+7)
## [1] "numeric"
class(1.2e7)
## [1] "numeric"
class(0x1)
## [1] "numeric"
class(0xa)
## [1] "numeric"
class(0x1.1p1)
## [1] "numeric"
mode(1)
## [1] "numeric"
mode(1.)
## [1] "numeric"
mode(1. )
## [1] "numeric"
mode(1.0)
## [1] "numeric"
mode(.1)
## [1] "numeric"
mode( .1)
## [1] "numeric"
mode(0.1)
## [1] "numeric"
mode(1e7)
## [1] "numeric"
mode(1E7)
## [1] "numeric"
mode(1e-7)
## [1] "numeric"
mode(1e+7)
## [1] "numeric"
mode(1.2e7)
## [1] "numeric"
mode(0x1)
## [1] "numeric"
mode(0xa)
## [1] "numeric"
mode(0x1.1p1)
## [1] "numeric"
typeof(1)
## [1] "double"
typeof(1.)
## [1] "double"
typeof(1. )
## [1] "double"
typeof(1.0)
## [1] "double"
typeof(.1)
## [1] "double"
typeof( .1)
## [1] "double"
typeof(0.1)
## [1] "double"
typeof(1e7)
## [1] "double"
typeof(1E7)
## [1] "double"
typeof(1e-7)
## [1] "double"
typeof(1e+7)
## [1] "double"
typeof(1.2e7)
## [1] "double"
typeof(0x1)
## [1] "double"
typeof(0xa)
## [1] "double"
typeof(0x1.1p1)
## [1] "double"
storage.mode(1)
## [1] "double"
storage.mode(1.)
## [1] "double"
storage.mode(1. )
## [1] "double"
storage.mode(1.0)
## [1] "double"
storage.mode(.1)
## [1] "double"
storage.mode( .1)
## [1] "double"
storage.mode(0.1)
## [1] "double"
storage.mode(1e7)
## [1] "double"
storage.mode(1E7)
## [1] "double"
storage.mode(1e-7)
## [1] "double"
storage.mode(1e+7)
## [1] "double"
storage.mode(1.2e7)
## [1] "double"
storage.mode(0x1)
## [1] "double"
storage.mode(0xa)
## [1] "double"
storage.mode(0x1.1p1)
## [1] "double"
The commands that gave error are mode( . )
storage.mode( . )
class(0x)
typeof(0x)
mode(0x)
storage.mode(0x)
Integer ConstantsThere is now a separate class of integer constants. They are created by using the qualifier L at the end of the number. For example, 123L gives an integer value rather than a numeric value. The suffix L can be used to qualify any non-complex number with the intent of creating an integer. So it can be used with numbers given by hexadecimal or scientific notation. However, if the value is not a valid integer, a warning is emitted and the numeric value created. The following shows examples of valid integer constants, values which will generate a warning and give numeric constants and syntax errors.
Valid integer constants: 1L, 0x10L, 1000000L, 1e6L Valid numeric constants: 1.1L, 1e-3L, 0x1.1p-2 Syntax error: 12iL 0x1.1
A warning is emitted for decimal values that contain an unnecessary decimal point, e.g. 1.L. It is an error to have a decimal point in a hexadecimal constant without the binary exponent.
Note also that a preceding sign (+ or -) is treated as a unary operator, not as part of the constant.
class(1L)
## [1] "integer"
class(1.L)
## [1] "integer"
class(0x10L)
## [1] "integer"
class(1e6L)
## [1] "integer"
class(1.1L)
## [1] "numeric"
class(1e-3L)
## [1] "numeric"
class(0x1.1p-2)
## [1] "numeric"
mode(1L)
## [1] "numeric"
mode(1.L)
## [1] "numeric"
mode(0x10L)
## [1] "numeric"
mode(1e6L)
## [1] "numeric"
mode(1.1L)
## [1] "numeric"
mode(1e-3L)
## [1] "numeric"
mode(0x1.1p-2)
## [1] "numeric"
typeof(1L)
## [1] "integer"
typeof(1.L)
## [1] "integer"
typeof(0x10L)
## [1] "integer"
typeof(1e6L)
## [1] "integer"
typeof(1.1L)
## [1] "double"
typeof(1e-3L)
## [1] "double"
typeof(0x1.1p-2)
## [1] "double"
storage.mode(1L)
## [1] "integer"
storage.mode(1.L)
## [1] "integer"
storage.mode(0x10L)
## [1] "integer"
storage.mode(1e6L)
## [1] "integer"
storage.mode(1.1L)
## [1] "double"
storage.mode(1e-3L)
## [1] "double"
storage.mode(0x1.1p-2)
## [1] "double"
The commands that gave error are
class(1. L)
class(12iL 0x1.1)
mode(12iL 0x1.1)
typeof(12iL 0x1.1)
storage.mode(12iL 0x1.1)
Complex constants have the form of a decimal numeric constant followed by ‘i’. Notice that only purely imaginary numbers are actual constants, other complex numbers are parsed a unary or binary operations on numeric and imaginary numbers.
Valid complex constants: 2i 4.1i 1e-2i
class(1i)
## [1] "complex"
class(1.0i)
## [1] "complex"
class(.1i)
## [1] "complex"
class( .1i)
## [1] "complex"
class(0.1i)
## [1] "complex"
class(1e7i)
## [1] "complex"
class(1E7i)
## [1] "complex"
class(1e-7i)
## [1] "complex"
class(1e+7i)
## [1] "complex"
class(1.2e7i)
## [1] "complex"
class(0x1i)
## [1] "complex"
class(0xai)
## [1] "complex"
class(0x1.1p1i)
## [1] "complex"
mode(1i)
## [1] "complex"
mode(1.0i)
## [1] "complex"
mode(.1i)
## [1] "complex"
mode( .1i)
## [1] "complex"
mode(0.1i)
## [1] "complex"
mode(1e7i)
## [1] "complex"
mode(1E7i)
## [1] "complex"
mode(1e-7i)
## [1] "complex"
mode(1e+7i)
## [1] "complex"
mode(1.2e7i)
## [1] "complex"
mode(0x1i)
## [1] "complex"
mode(0xai)
## [1] "complex"
mode(0x1.1p1i)
## [1] "complex"
typeof(1i)
## [1] "complex"
typeof(1.0i)
## [1] "complex"
typeof(.1i)
## [1] "complex"
typeof( .1i)
## [1] "complex"
typeof(0.1i)
## [1] "complex"
typeof(1e7i)
## [1] "complex"
typeof(1E7i)
## [1] "complex"
typeof(1e-7i)
## [1] "complex"
typeof(1e+7i)
## [1] "complex"
typeof(1.2e7i)
## [1] "complex"
typeof(0x1i)
## [1] "complex"
typeof(0xai)
## [1] "complex"
typeof(0x1.1p1i)
## [1] "complex"
storage.mode(1i)
## [1] "complex"
storage.mode(1.0i)
## [1] "complex"
storage.mode(.1i)
## [1] "complex"
storage.mode( .1i)
## [1] "complex"
storage.mode(0.1i)
## [1] "complex"
storage.mode(1e7i)
## [1] "complex"
storage.mode(1E7i)
## [1] "complex"
storage.mode(1e-7i)
## [1] "complex"
storage.mode(1e+7i)
## [1] "complex"
storage.mode(1.2e7i)
## [1] "complex"
storage.mode(0x1i)
## [1] "complex"
storage.mode(0xai)
## [1] "complex"
storage.mode(0x1.1p1i)
## [1] "complex"
The commands that gave error are class(1. i), mode(1. i), typeof(1. i), storage.mode(1. i)
String constants are delimited by a pair of single (‘’’) or double (‘"’) quotes and can contain all other printable characters. Quotes and other special characters within strings are specified using escape sequences ' single quote
" double quote
newline (aka ‘line feed’, LF)
return (CR)
character
bell
feed
tab
\ backslash itself
character with given octal code – sequences of one, two or three digits in the range 0 … 7 are accepted.
character with given hex code – sequences of one or two hex digits (with entries 0 … 9 A … F a … f).
(where multibyte locales are supported, otherwise an error). Unicode character with given hex code – sequences of up to four hex digits. The character needs to be valid in the current locale.
(where multibyte locales are supported, otherwise an error). Unicode character with given hex code – sequences of up to eight hex digits.
A single quote may also be embedded directly in a double-quote delimited string and vice versa.
class('abc')
## [1] "character"
class("abc")
## [1] "character"
class('2')
## [1] "character"
class("2")
## [1] "character"
class('"')
## [1] "character"
class("'")
## [1] "character"
class('\'')
## [1] "character"
class("\"")
## [1] "character"
mode('abc')
## [1] "character"
mode("abc")
## [1] "character"
mode('2')
## [1] "character"
mode("2")
## [1] "character"
mode('"')
## [1] "character"
mode("'")
## [1] "character"
mode('\'')
## [1] "character"
mode("\"")
## [1] "character"
typeof('abc')
## [1] "character"
typeof("abc")
## [1] "character"
typeof('2')
## [1] "character"
typeof("2")
## [1] "character"
typeof('"')
## [1] "character"
typeof("'")
## [1] "character"
typeof('\'')
## [1] "character"
typeof("\"")
## [1] "character"
storage.mode('abc')
## [1] "character"
storage.mode("abc")
## [1] "character"
storage.mode('2')
## [1] "character"
storage.mode("2")
## [1] "character"
storage.mode('"')
## [1] "character"
storage.mode("'")
## [1] "character"
storage.mode('\'')
## [1] "character"
storage.mode("\"")
## [1] "character"
The commands that gave error are class(’’’), class(""“), mode(’’’), mode(”"“), typeof(’’’), typeof(”"“), storage.mode(’’’), storage.mode(”"").
special constants there are four special constants, NULL, NA,Inf, and NaN. NULL is used to indicate the empty object. NA is used for absent (“Not Available”) data values. Inf denotes infinity and NaN is not-a-number
class(NULL)
## [1] "NULL"
class(NA)
## [1] "logical"
class(Inf)
## [1] "numeric"
class(NaN)
## [1] "numeric"
mode(NULL)
## [1] "NULL"
mode(NA)
## [1] "logical"
mode(Inf)
## [1] "numeric"
mode(NaN)
## [1] "numeric"
typeof(NULL)
## [1] "NULL"
typeof(NA)
## [1] "logical"
typeof(Inf)
## [1] "double"
typeof(NaN)
## [1] "double"
storage.mode(NULL)
## [1] "NULL"
storage.mode(NA)
## [1] "logical"
storage.mode(Inf)
## [1] "double"
storage.mode(NaN)
## [1] "double"
Additional Note/Postscript/points to ponder
charToRaw("Hi")
## [1] 48 69
class(charToRaw("Hi"))
## [1] "raw"
mode(charToRaw("Hi"))
## [1] "raw"
typeof(charToRaw("Hi"))
## [1] "raw"
storage.mode(charToRaw("Hi"))
## [1] "raw"
as.raw ( as.hexmode ( "4e" ) )
## [1] 4e
as.Date('2010-01-01')
## [1] "2010-01-01"
class(as.Date('2010-01-01'))
## [1] "Date"
mode(as.Date('2010-01-01'))
## [1] "numeric"
typeof(as.Date('2010-01-01'))
## [1] "double"
storage.mode(as.Date('2010-01-01'))
## [1] "double"
unclass(as.Date('2010-01-01'))
## [1] 14610
print(pi)
## [1] 3.141593
class(pi)
## [1] "numeric"
mode(pi)
## [1] "numeric"
typeof(pi)
## [1] "double"
storage.mode(pi)
## [1] "double"
print(LETTERS)
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
## [20] "T" "U" "V" "W" "X" "Y" "Z"
class(LETTERS)
## [1] "character"
mode(LETTERS)
## [1] "character"
typeof(LETTERS)
## [1] "character"
storage.mode(LETTERS)
## [1] "character"
print(letters)
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
class(letters)
## [1] "character"
mode(letters)
## [1] "character"
typeof(letters)
## [1] "character"
storage.mode(letters)
## [1] "character"
print(month.abb)
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
class(month.abb)
## [1] "character"
mode(month.abb)
## [1] "character"
typeof(month.abb)
## [1] "character"
storage.mode(month.abb)
## [1] "character"
print(month.name)
## [1] "January" "February" "March" "April" "May" "June"
## [7] "July" "August" "September" "October" "November" "December"
class(month.name)
## [1] "character"
mode(month.name)
## [1] "character"
typeof(month.name)
## [1] "character"
storage.mode(month.name)
## [1] "character"
print(2)
## [1] 2
print(2L)
## [1] 2
2==2L
## [1] TRUE
all.equal(2,2L)
## [1] TRUE
identical(2,2L)
## [1] FALSE
identical(T,TRUE)
## [1] TRUE
The commands that gave error are
as.raw(256)
as.raw(“Hi”)
class(48 69)
Inf, Nan, NA, Null
7/0 #Division by zero is Infinite
## [1] Inf
-7/0##Division by zero is Infinite
## [1] -Inf
0/0 #Zero divided by zero is undefined
## [1] NaN
x<-1:4
length(x)<-5 #
print(x)
## [1] 1 2 3 4 NA
print(cat())#concatenate function default return value is null
## NULL