Occurs when a number is too large for R to represent. This inf is case sensitive. It can only be associated with numeric values
foo<-Inf
foo
[1] Inf
bar<-c(3401,Inf,3.1,-555,Inf,43)
bar
[1] 3401.0 Inf 3.1 -555.0 Inf 43.0
bar<-90000^100
bar
[1] Inf
# example of negative Inf
qux <-c(42,-Inf,-Inf,Inf,-45623.3)
qux
[1] 42.0 -Inf -Inf Inf -45623.3
Note, you can calculate using Inf for example
Inf* -8
[1] -Inf
YOu will see that multiplying Inf with a negative number produces a negative Inf. If you add or multiply infinity you also get infinity as a result
Inf+1
[1] Inf
4*-Inf
[1] -Inf
-45.2-Inf
[1] -Inf
Inf-45.2
[1] Inf
Inf+Inf
[1] Inf
Inf/23
[1] Inf
Zero and infinity go hand in hand. Any finite number dived by infinity will result in zero.
-59/Inf
[1] 0
59/Inf
[1] 0
-59/-Inf
[1] 0
59/-Inf
[1] 0
Any number divided by zero becomes Inf
56/0
[1] Inf
-59/0
[1] -Inf
To detect Inf values, use is.infinite or is.finite
# see the contents of qux
qux
[1] 42.0 -Inf -Inf Inf -45623.3
# test for inf values
is.infinite(x=qux)
[1] FALSE TRUE TRUE TRUE FALSE
# test for finite values
is.finite(x=qux)
[1] TRUE FALSE FALSE FALSE TRUE
Notice that these functions does not test for either +Inf or -Inf. Finally relational operators work on Inf values
qux
[1] 42.0 -Inf -Inf Inf -45623.3
-Inf<Inf
[1] TRUE
Inf>-Inf
[1] TRUE
qux==Inf
[1] FALSE FALSE FALSE TRUE FALSE
qux==-Inf
[1] FALSE TRUE TRUE FALSE FALSE
Used when it is impossible to express the result of a calculation using a number, Inf, or -Inf
foo<-NaN
foo
[1] NaN
bar<-c(NaN,54.3,-2,NaN,90094.123,-Inf,55)
bar
[1] NaN 54.30 -2.00 NaN 90094.12 -Inf 55.00
Whenever you attempt to cancel Inf in any way, the result will be Nan
-Inf+Inf
[1] NaN
Inf/Inf
[1] NaN
Note that any matheatical operation involving NaN will simply result in Nan
NaN+1
[1] NaN
2+6*(4-4)/0
[1] NaN
3.5^(-Inf/Inf)
[1] NaN
Functions to detect Nan : Is.NaN
bar
[1] NaN 54.30 -2.00 NaN 90094.12 -Inf 55.00
is.nan(x=bar)
[1] TRUE FALSE FALSE TRUE FALSE FALSE FALSE
!is.nan(x=bar)
[1] FALSE TRUE TRUE FALSE TRUE TRUE TRUE
is.nan(x=bar)| is.infinite(x=bar)
[1] TRUE FALSE FALSE TRUE FALSE TRUE FALSE
bar[-which(is.nan(x=bar)|is.infinite(x=bar))]
[1] 54.30 -2.00 90094.12 55.00
NOte: The last line uses WHICH command to convert these logical values into numberc index positions so that you can remove them with the negative indexes in the square brackets.
NA is used for missing values in data sets. Whereas NaN is only used for numeric values, NA can also be used for both numeric and non-numberic settings.
foo<-c("character","a",NA,"with","string",NA)
foo
[1] "character" "a" NA "with" "string" NA
bar<-factor(c("blue",NA,NA,"blue","green","blue",NA,"red","red",NA,"green"))
bar
[1] blue <NA> <NA> blue green blue <NA> red red <NA> green
Levels: blue green red
baz <-matrix(c(1:3,NA,5,6,NA,8,NA),nrow=3,ncol=3)
baz
[,1] [,2] [,3]
[1,] 1 NA NA
[2,] 2 5 8
[3,] 3 6 NA
Use Is.na to detect NA elements
baz
[,1] [,2] [,3]
[1,] 1 NA NA
[2,] 2 5 8
[3,] 3 6 NA
is.na(x=baz)
[,1] [,2] [,3]
[1,] FALSE TRUE TRUE
[2,] FALSE FALSE FALSE
[3,] FALSE FALSE TRUE
# is.na also flags the NaN values
qux<-c(NA,5.89,Inf,NA,9.43,-2.35,NaN,2.10,-8.53,-7.58,NA,-4.58,2.01,NaN)
qux
[1] NA 5.89 Inf NA 9.43 -2.35 NaN 2.10 -8.53 -7.58 NA -4.58 2.01 NaN
is.na(x=qux)
[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
If you want to identify Na and NaN separately
qux
[1] NA 5.89 Inf NA 9.43 -2.35 NaN 2.10 -8.53 -7.58 NA -4.58 2.01 NaN
which(x=is.nan(x=qux))
[1] 7 14
If you want to identify NaN values specifically:
qux
[1] NA 5.89 Inf NA 9.43 -2.35 NaN 2.10 -8.53 -7.58 NA -4.58 2.01 NaN
which(x=(is.na(x=qux)&!is.nan(x=qux)))
[1] 1 4 11
After locating the offending elements, you an use negative indexes in square brackets to remove them. Another way in R is to use the na.omit. It will take a structure and delete all NAs from it. na.omit will also apply to NaNs if the elements are numeric
qux
[1] NA 5.89 Inf NA 9.43 -2.35 NaN 2.10 -8.53 -7.58 NA -4.58 2.01 NaN
quux<-na.omit(object=qux)
quux
[1] 5.89 Inf 9.43 -2.35 2.10 -8.53 -7.58 -4.58 2.01
attr(,"na.action")
[1] 1 4 7 11 14
attr(,"class")
[1] "omit"
Used to EXPLICITLY define an empty entity. Notice there is NO index position for NULL values
foo <- NULL
foo
NULL
bar <- NA
bar
[1] NA
c(2,4,NA,8)
[1] 2 4 NA 8
c(2,4,NULL,8)
[1] 2 4 8
The line using NA has 4 elements, while the line using NULL has only 3 elements Null cannot take up a position in the vector.
The is.null function is used to check when an element is explicitly NULL.
opt.arg <-c("string1","string2","string3")
opt.arg
[1] "string1" "string2" "string3"
is.na(x=opt.arg)
[1] FALSE FALSE FALSE
is.null(x=opt.arg)
[1] FALSE
If the argument is empty using NULL over NA for the check is better.
opt.arg<-c(NA,NA,NA)
is.na(x=opt.arg)
[1] TRUE TRUE TRUE
opt.arg<-c(NULL,NULL,NULL)
is.null(x=opt.arg)
[1] TRUE
opt.arg<-c(NULL,NULL,1)
is.null(x=opt.arg)
[1] FALSE
opt.arg<-c(NULL,NULL,NA)
is.null(x=opt.arg)
[1] FALSE
Null dominates any arithmetic, even if it includes other special values
NULL+53
numeric(0)
53<NULL
logical(0)
NaN-NULL+NA/Inf
numeric(0)
Attributes can be implicit or explicit.
foo<-matrix(data=1:9,nrow=3,ncol=3)
foo
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
attributes(foo)
$dim
[1] 3 3
You can also use this:
attr(x=foo,which="dim")
[1] 3 3
When additional attributes are created, you can see all with the attributes command In this example, we added dimnames to the matrix.
foo<-matrix(data=1:9,nrow=3,ncol=3,dimnames=list(c("a","b","c"),c("d","e","f")))
foo
d e f
a 1 4 7
b 2 5 8
c 3 6 9
attributes(foo)
$dim
[1] 3 3
$dimnames
$dimnames[[1]]
[1] "a" "b" "c"
$dimnames[[2]]
[1] "d" "e" "f"
We can extract the values of dimnames as well
dimnames(foo)
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "d" "e" "f"
we can also assign an object after it has been created.
foo
d e f
a 1 4 7
b 2 5 8
c 3 6 9
bar<-list(c("a","b","c"),c("d","e","f"))
bar
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "d" "e" "f"
Every object we create is identified either implicitly or explicitly whith at least one class.
num.vec1<-1:4
num.vec1
[1] 1 2 3 4
num.vec2<-seq(from=1, to=4,length=6)
num.vec2
[1] 1.0 1.6 2.2 2.8 3.4 4.0
char.vec<-c("a","few","strings","here")
char.vec
[1] "a" "few" "strings" "here"
logic.vec<-c(T,F,F,F,T,F,T,T)
fac.vec<-factor(c("Blue","Blue","Green","Red","Green","Yellow"))
fac.vec
[1] Blue Blue Green Red Green Yellow
Levels: Blue Green Red Yellow
Standalone vectors
class(num.vec1)
[1] "integer"
class(num.vec2)
[1] "numeric"
class(char.vec)
[1] "character"
class(logic.vec)
[1] "logical"
class(fac.vec)
[1] "factor"
Other Data Structures
num.vec1
[1] 1 2 3 4
num.mat1<-matrix(data=num.vec1,nrow=2,ncol=2)
num.mat1
[,1] [,2]
[1,] 1 3
[2,] 2 4
class(num.mat1)
[1] "matrix"
num.vec2
[1] 1.0 1.6 2.2 2.8 3.4 4.0
num.mat2<-matrix(data=num.vec2,nrow=2,ncol=3)
num.mat2
[,1] [,2] [,3]
[1,] 1.0 2.2 3.4
[2,] 1.6 2.8 4.0
class(num.mat2)
[1] "matrix"
char.vec
[1] "a" "few" "strings" "here"
char.mat<-matrix(data=char.vec,nrow=2,ncol=2)
char.mat
[,1] [,2]
[1,] "a" "strings"
[2,] "few" "here"
class(char.mat)
[1] "matrix"
logic.vec
[1] TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE
logic.mat<-matrix(data=logic.vec,nrow=4,ncol=2)
logic.mat
[,1] [,2]
[1,] TRUE TRUE
[2,] FALSE FALSE
[3,] FALSE TRUE
[4,] FALSE TRUE
class(logic.mat)
[1] "matrix"
Multiple Classes Certain objects will have multiple classes. A variant on a standard form of an object such an ordered factor vector, will inherit the usual factor class and also contain the additional ordered class. Both are returned if you use the class function
ordfac.vec<-factor(x=c("Small","Large","Large","Regular","Small"),levels=c("Small","Regular","Large"),ordered=TRUE)
ordfac.vec
[1] Small Large Large Regular Small
Levels: Small < Regular < Large
class(ordfac.vec)
[1] "ordered" "factor"
To check whether the object is a specific class or data type, you can use the “is-dot function” on the object and it will return TRUE or False logical value.
num.vec1 <-1:4
num.vec1
[1] 1 2 3 4
is.integer(num.vec1)
[1] TRUE
is.numeric(num.vec1)
[1] TRUE
is.matrix(num.vec1)
[1] FALSE
is.data.frame(num.vec1)
[1] FALSE
is.vector(num.vec1)
[1] TRUE
is.logical(num.vec1)
[1] FALSE
Coercion is the conversion of one data type to another. Implicit coercion occurs naturally when elements need to be converted to another type in order for an operation to complete. True or false are implicitly coerced into 1 for true and 0 for false.
1:4+c(T,F,F,T)
[1] 2 2 3 5
Another example of coercion when you paste and cat to glue together character strings. Non-character strings are automatically coerced into strings before the concatenation takes places.
foo <-34
bar<-T
paste("Definitely foo:", foo, "; definitely bar: ",bar,".",sep=" ")
[1] "Definitely foo: 34 ; definitely bar: TRUE ."