Types of R object – 2. logical
All R objects have a class attribute, which can be important as R “decides” how to deal with objects based upon their class.
Object type: logical
Simple 1-dimensional objects are called vectors but vector is not a class in itself. A vector can be numeric or character in nature (these are class attributes). You can also have a logical class, which is either TRUE or FALSE.
> lv
[1] FALSE TRUE FALSE FALSE FALSE
> class(lv)
[1] "logical"
You can create a logical vector by using TRUE or FALSE in a command like c() for example:
> lv2 <- c(TRUE, FALSE, FALSE, TRUE)
> lv2
[1] TRUE FALSE FALSE TRUE
R recognises TRUE and FALSE as logical. You must use upper case but you can abbreviate using T or F:
> lv3 <- c(T, T, F, F)
> lv3
[1] TRUE TRUE FALSE FALSE
If you are typing commands for yourself then using abbreviations is fine but when your commands are destined to be seen by others, then it is a good idea to use the “full” version.
Comments are closed.