# R Operators: Arithmetic, Rational, Logical | Beginning R Programming Part 4

June 28, 2019 9:18 pm

R operators are not only useful for doing calculations on data but can also be used to compare values or set up conditions for values. Learn the 3 main types of operators: arithmetic, rational, and logical.

If you want to do a quick calculation on

some numeric values such as calculating

the difference between values

or compare values to see if they match or meet a

certain condition

then you’ll need to know the different operators

you can work with

we’ll focus on three main types of operators:

arithmetic, rational, and logical

let’s first look at arithmetic

you have your typical

addition, subtraction, multiplication, division

remainder, and exponent

what’s also important to know with these

arithmetic operators

is their order of operations

so when calculating some values is first

calculates anything inside the

parentheses followed by anything that

has an exponent, then a multiplied number

then division, addition, and subtraction

This is important because when you’re

calculating the mean of some numbers so

for example

you’re going to some these numbers here

then divide it by the numbers of numbers

you’ll see that this results

in a different number to if we

had to use parentheses so

if we had of

summed of these numbers first

then divided

As the default order is division comes

before addition we want to tell the

program to first calculate additions

and then move on to division

this gives us the correctly calculated mean

So rational and logical operators allow us

to compare data values to see if they

match, don’t match, are above, below, or

equal to numeric thresholds

or extract data that

meet a number of these conditions

your rational operators include

checking if a numeric value is

greater than or less than a threshold

is greater than or equal to

or is less than or equal to

A numeric value or a character string

that is equal to or

matches another value

or is not equal to a value

your logical operators include

“and”, “or”, and “not”

you use “and” when you want

to extract data that meets both one

condition and the other condition

or more

for example it has to be both

greater than this number and equal to

this category

and “or” means that data

will be extracted if it only meets one

of these conditions or options that apply

for example it can either be greater

than this value or belong to this category

If it is greater than this value

then it will extract the data and

will have no need to check any other

condition as it’s already satisfied at least one

if it doesn’t meet the first

condition it will search the data based

on the next condition and the next

condition after that and so on and so forth

until it meets at least one of the given

conditions

The not logical operator

basically extracts out everything that

is not one or more of the conditions

So, for example, I want to get everything

that does not belong to this category

I’m interested in everything except for

those things that are in this category

So I’ll give you an example we have some

data here, which I ran into R, and we’ll

cover reading data into R in another

video dedicated to this but we’re just

using this to demonstrate operators

So this data set looks at the average

income across main U.S. cities

across different job roles

So then I as a product manager

I want to know if San Francisco

pays higher on average for my

job role than where I’m currently living

in New York City so I’ll show you how to

use some operators to extract these data

So we’ll first extract New York City

average income for product managers and

store this in a variable called

“nyc.product.managers”

and we’re going to use our income

data set here and inside this

we want to look for our city variable

and have this equal to or match “New York City”

We’re going to use an end condition as well

because we also want that

to match product managers

so people who

live in New York City and are product

managers

and we’re interested in the job

title variable we would like this to

equal to product managers

or product manager

Okay I’ll print this here

Awesome so what we’re interested in here

is the value under the average income here

this variable

now we need to also get the

same for San Francisco so we can compare them

so we’ll just call this

“sf.product.managers”

and using our income data set

So for our income data set

we’re interested in our “city” variable

and we would like it to equal to

“San Francisco”

and we would also like it to equal to

“job title”

“product manager”

Okay cool,

so we have the average income for product

managers in New York City and San Francisco

so first I want to know if

it’s true that product managers living

in San Francisco have an higher

income on average than people in

New York City

so what I’m going to do is

is San Francisco product managers average

income greater than NYC product

managers average income?

Okay the results say this is true

So basically

San Francisco product managers are paid

higher on average so then I might

consider relocating to this to city

but I’ll go bits

I’ll go step further than that I want to

know how much more San Francisco folks

are paid on average in terms of a dollar figure

so I’m looking at the difference

between San Francisco product managers

average income

I’m gonna minus New York City’s

product managers average income

okay so the difference is 7,000

that might or might not be a big enough a

difference for me to make the relocation

worth it but it’s not bad either

and now you know how to use operators

to extract useful data

next we’ll cover how to read data into R

**Prerequisites****:**

Data Set

How to Install R

**Part 5: **

Read and Write Data

**Part 3:
**Creating Variables in R

**Full Series:**

Beginning R Programming

**More Data Science Material:**

[Video] Power BI for R Visualizations

[Blog] Unleash the potential of Recommender Systems

[Blog] Math for Aspiring Data Scientists

[Blog] Top Math Resources for Data Scientists

(154)