Logical vectors and operators in R project

The elements in the logical vectors in R project can have the values of T for TRUE, F for FALSE and NA, i.e. short form for Not Available. T and F are not the reserved words, they are just variables set to TRUE and FALSE by default. They can be overwritten by the user.

Logical vectors are produced by conditions such as

temp <- x > 13

where temp act as a vector of the same length as x and will show the values as FALSE, if the condition is not met and TRUE, if the condition will met.

You can understand this by assigning a value to x and then asking the console about FALSE or TRUE condition as follows,

x=2

temp <- x > 3

temp

You will get the FALSE condition for this. Now write the following in the R console and find what the software will tell.

x <- c(1,2,3,4,5)

temp <- x > 3

temp

It will show you FALSE FALSE FALSE  TRUE  TRUE.

Logical operators are <, <=, >, >=, == for exact equality and != for inequality. Now write the following in R and find which logical vector you will get.

x=2

y=3

x<y

As we know that 2 is less than 3, so the logical vector will be TRUE. Now write

x!=y

You will again get TRUE as x is not equal to y.

In addition if c1 and c2 are logical expressions, then c1 & c2 is their intersection (“and”), c1 | c2 is their union (“or”), and !c1 is the negation of c1. Logical vectors may be used in ordinary arithmetic, in which case they are coerced into numeric vectors, FALSE becoming 0 and TRUE becoming 1. However there are situations where logical vectors and their coerced numeric counterparts are not equivalent.

Source: R project

saypeople

SayPeople.com gives you the news and information about Science, Research, Technology, Business and Islam.