Tuesday 11 September 2018

Basic calculations: Missing data and logical operators in R Language

Missing data

R represents missing observations through the data value NA
We can detect missing values using is.na

> x  <-  NA             # assign NA to variable x
> is.na (x)               # is it missing ?
   [1]    TRUE

Now try a vector to know if any value is missing?

> x <-  c(11, NA, 13)
> is.na (x)
  [1] FALSE TRUE FALSE














Example : How to work with missing data

> x  <-  c(11, NA, 13)  # vector
> mean (x)     11 + NA + 13/2
  [1]   NA
> mean (x, na.rm = TRUE )  # NAs can be removed 
 [1]  12
                    11 + 13/2 = 12
The null object, called NULL, is returned by some functions and expressions.

Note that NA and NULL are not the same.

NA is a placeholder for something that exists but is missing.

NULL stands for something that never existed at all.





Logical Operators and Comparisons

The following table shows the operations and functions for logical comparisons (True or False)

TRUE and FALSE are reserved words denoting logical constants.


Logical Operators and Comparisons



  • The shorter form performs element-wise comparisons in almost the same way as arithmetic operators.
  • The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined.
  • The longer form is appropriate for programming control-flow and typically preferred in if clauses (conditional).
TRUE and FALSE are reserved words denoting logical constants


Example

 > x  <- 5
Is x less than 10 or x is greater than 5 ?
 > (x < 10) | | (x > 5)   # | | means OR
 [1]  TRUE

Is x greater than 10 or x is greater than 5 ?
> (x > 10) | |  (x > 5)
[1] FALSE


0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (191) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses