Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views9 pages

Module 2.6

The document provides an overview of mathematical operators in R, essential for data manipulation and analysis in Data Science. It categorizes operators into arithmetic, comparison, logical, assignment, and miscellaneous types, detailing their functions and usage. Additionally, it includes hands-on activities to practice using these operators with a dataset related to fitness app users.

Uploaded by

Squall Lionheart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Module 2.6

The document provides an overview of mathematical operators in R, essential for data manipulation and analysis in Data Science. It categorizes operators into arithmetic, comparison, logical, assignment, and miscellaneous types, detailing their functions and usage. Additionally, it includes hands-on activities to practice using these operators with a dataset related to fitness app users.

Uploaded by

Squall Lionheart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

2.

6 THE OPERATORS

Ptherogramming will never be complete without going through


mathematical operators especially on a Data Science
optimized tool like R.

Now that you've explored data types, variables, and


various R objects, it's time to delve into the operators – the
tools that empower you to manipulate, analyze, and transform
your data. Operators act like bridges between these
elements, allowing you to perform calculations, comparisons,
and more.

Categorizing R Operators:

R boasts a diverse set of operators, each with a speci c


function. Here's a breakdown of the key categories:

‣ Arithmetic Operators: These perform basic mathematical


operations on numeric data.
Examples:
+ (addition), - (subtraction),
* (multiplication), / (division),
^ (exponentiation),
%% (modulo - remainder after division)

‣ Comparison Operators: These compare values and


return logical (TRUE/FALSE) results.
Examples:
== (equal to), != (not equal to), < (less than),
>(greater than), <= (less than or equal to),
>= (greater than or equal to)

Page 58 of 77
fi
• Logical Operators: These combine logical values (TRUE/
FALSE) to create complex expressions.

• Assignment Operators: These assign values to variables.


Examples:
= (assignment),
<- (assignment),
<<- (left assignment - assigns and evaluate)

• Miscellaneous Operators: These cover various


functionalities like subsetting (extracting speci c data)
and recycling (using a short vector multiple times).
Examples:
: (colon for subsetting),
[] (square brackets for subsetting),
c() (combine vectors)

Exploring Key Operators:

Let's delve deeper into some commonly used operators:

• Arithmetic Operators: Imagine calculating the average


age in a dataset. You'd use the + operator to sum the
ages and then / to divide by the number of people.

• Comparison Operators: Determining if a customer’s


purchase amount exceeds a certain threshold involves
comparison operators. > would check if the purchase
amount is greater than the threshold.

• Logical Operators: Combining conditions often involves


logical operators. For example, ltering customers who
are both active (TRUE) and have purchased a speci c
product category (TRUE) might use the & operator.

Page 59 of 77
fi
fi
fi
• Assignment Operators: Assigning a calculated value to a
variable is crucial. The = operator allows you to store the
result of an expression in a variable for further use.

Remember:
• Precedence: Operators have a speci c order of
operations (precedence). Just like in math, multiplication
and division happen before addition and subtraction. Use
parentheses to control the order if needed.

• Data Type Compatibility: Operators often work best with


compatible data types. Mixing numeric and character data
might lead to unexpected results.

Beyond the Basics:


While these are the fundamental operators, R o ers more!
Here are some additional points to consider:

• Vectorized Operations: R performs operations element-


wise on vectors. This means a single operator can be
applied to all elements simultaneously, making vectorized
operations e cient for data manipulation.

• Recycling: When using short vectors in operations with


longer ones, R recycles the shorter vector to match the
length of the longer one. This can be handy but requires
awareness to avoid errors.

Mastering Operators:
By understanding and applying operators e ectively, you’ll
unlock the power to manipulate your data in R. As you
progress, you'll encounter more specialized operators within
speci c packages, further expanding your data analysis
capabilities. So, experiment, practice, and unleash the power
of operators in your R journey!

Page 60 of 77
fi
ffi
fi
ff
ff
HANDS-ON ACTIVITY # 3
Gdesigned
et ready to ex your R muscles with this hands-on activity
to solidify your understanding of essential
operators!

Scenario:
You're a data analyst for a tness app and have a dataset
containing information about users' workouts. Let's explore
and manipulate this data using various operators.

Data: (Imagine you have this data loaded into a data frame
named workout_data)

Duration Calories
User ID Activity
mins) Burned

1 Running 30 250

2 Swimming 45 320

3 Yoga 60 180

4 Cycling 20 150

5 Strength Training 40 300

Calculating Average Workout Duration:

Objective: Calculate the average workout duration for all


users.

Operators Used: Arithmetic (+, /)

Page 61 of 77
fl
fi
Steps:
1. Use sum(workout_data$Duration) to nd the total workout
duration for all users.

2. Use nrow(workout_data) to nd the number of users


(number of rows in the data frame).

3. Combine these steps using the arithmetic operators to


calculate the average duration:

average_duration <- sum(workout_data$Du ration) / nrow(workout_data)

4. Print the average_duration to see the average workout


time in minutes.

Write the R scripts (commands) you used on each line and


have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 62 of 77
fi
fi
Identifying High-Intensity Workouts:

Objective: Find workouts that burned more than 200 calories.

Operators Used: Comparison (>) and Logical (&)

Steps:
1. Use the comparison operator > to identify workouts with
calories burned greater than 200:

high_intensity_workouts <- workout_data$CaloriesBurned > 200

2. This creates a logical vector indicating TRUE for workouts


exceeding 200 calories and FALSE otherwise.

3. Use the data frame with this logical vector to lter the
original data and see only high-intensity workouts:

subset(workout_data, high_intensity_workouts)

Write the R scripts (commands) you used on each line and


have it validated by your instructor.

>

Please use a separate sheet should your need more lines.

Page 63 of 77
fi
Analyzing Activity Preferences:

Objective: See if there's a relationship between activity type


and calorie burn.

Operators Used: Comparison (==) and Logical (&)

Steps:
1. Create a new variable indicating "High Burn" (TRUE) if
calories burned exceed 250 and "Low Burn" (FALSE)
otherwise:

workout_data$Burn_Category <- workout_data$CaloriesBurned > 250

2. Use the comparison operator == to identify workouts with


speci c activities

(e.g., running_workouts <- workout_data$Activity == “Running”).

3. Combine these conditions using the logical operator & to


see if running workouts tend to be high burn:

high_burn_running <- workout_data$Burn_Category == TRUE &


running_workouts

4. Analyze similar comparisons for other activity types to


explore potential trends.

Page 64 of 77
fi
Write the R scripts (commands) you used on each line and
have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 65 of 77
Bonus Activity:

1. Modify the activity to calculate the total calories burned


for all users combined.

Write the R scripts (commands) you used on each line and


have it validated by your instructor.

>

Please use a separate sheet should your need more lines.

2. Explore using assignment operators (=, <-) to create new


variables derived from existing data (e.g., calculate total
calories burned per user).

Write the R scripts (commands) you used on each line and


have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 66 of 77

You might also like