Lab 1: Introduction to R Mathematical and variable Assignment

In this lab, I will discuss how to start with R script, general mathematical operations and variable.

The task lists for the lab 1:

  • General Mathematical Operations
  • Variable assignment
  • Basic data types in R

Step for the task: The General Mathematical Operations

  • +, plus (addition)
  • −, minus (subtraction)
  • ×, times (multiplication)
  • ÷, obelus (division)
  • ^, exponentiation [written as an, involving two numbers, the base a and the exponent n, called n-th power of a]
  • %%, remainder

1

2

Steps for the task: Variable assignment

A variable is a basic concept in statistical programming. It allows storing a value or an object function in R. A programmer can use later the variables’name to easily access the value or object that is stored in that variable.

Step1: assign a value to a variable with the command

x= 5

R command: x

3.jpg

Noticed that: R doesn’t print the value of a variable to the console. x future, otherwise, you wouldn’t have stored the value in a variable in the first place.

Step2:  Suppose, you are a data analyst, you have an egg basket with 10 eggs. Now you need to store the number of eggs in a variable with the name. Number_of_eggs

R command: Number_of_eggs

4.jpg

Step3: Suppose, your egg basket is the combination of Chicken eggs, Bird eggs. As a data analyst, you need to assign variable by name and store the value to the variable, the calculate how many eggs you have in total in the basket.

R command:

Chicken_eggs

Bird_eggs

Total_eggs

5.jpg

Noted that: The advantage of doing calculations with a variable is re-usability. 

For example, If you change the value for the Chicken_eggs variable = 15 instead of the 7, Total_eggs will automatically update as well. 6

Remember that: Mathematical operations work numeric variables in R. If you try to add two variable and assigned a variable with a text value. It will show error. The addition of a numeric and a character variable is not possible.

R command: Chicken_eggs

                        Bird_eggs  <- ‘eight’

Total_eggs= Chicken_eggs + Bird_eggs = error: non-numeric argument to binary operator

7.jpg

Steps for the task: Basic data types in R

R works with the following data types:

  • Decimal values, for example, 4.5, are called numeric
  • Natural numbers ( 4), are called integers or numeric
  • Boolean values (True, False) are called Logical
  • Text or string values are called characters

Exercise 8.jpg

Exercise To avoid mismatch in data types, you can check the data type of the variable beforehand of operation with the class () function.

R command: class (my_numeric); it will show the data type of my_numeric

9.jpg

Thank you 🙂 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s