0% found this document useful (0 votes)
3 views44 pages

R Basics: Installation and Overview

The document provides a comprehensive guide on installing R and RStudio, as well as an introduction to R programming, its capabilities in statistical computing, data analysis, and visualization. It explains the user interface of RStudio, how to create and manipulate objects, and the use of functions, including built-in and user-defined functions. Additionally, it includes exercises for practicing basic arithmetic, object creation, and function usage in R.

Uploaded by

bimanbibu
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)
3 views44 pages

R Basics: Installation and Overview

The document provides a comprehensive guide on installing R and RStudio, as well as an introduction to R programming, its capabilities in statistical computing, data analysis, and visualization. It explains the user interface of RStudio, how to create and manipulate objects, and the use of functions, including built-in and user-defined functions. Additionally, it includes exercises for practicing basic arithmetic, object creation, and function usage in R.

Uploaded by

bimanbibu
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

Install R

Install RStudio
Install the following essential Packages

* gg – Grammar of Graphics
WHAT IS R?
✓ R is one of the best programming languages specifically designed for
statistics and graphics. Programming in R is a fast and effective way to
perform :
❖ statistical computing
❖ data analysis – there a number of tools available for data
analysis
Ross Ihaka & Robert Gentleman
❖ visualization – Graphical representation of data

R Programming
Why is R becoming such a popular and useful tool in data analysis and statistical analysis ?
❖ R, being an open-source programming language, you've got people all over the world writing packages and packages and
things that you can install and use that.
❖ R has incredible data visualization and graphics capabilities, which beats any other package hands down.
➢ For example, we have got a little data frame called friends. We want to find their age and height. The way the R
works is, We can plot a histogram of the age or plot age against height.
➢ We might want to know if there is a statistically significant correlation between age and height.
R Studio, an USER INTERFACE, is used to implement R programs, has four partitions/panes

Source Partition:
✓ Code is written & executed Source Partition:
✓ Inputs, e.g. objects, functions, data
structures used for the code is
displayed here
✓ The data sheet can be imported

Console : Files/Plots Partition:


✓ Type your command, after the command ✓ If the code is trying to plot something,
prompt >, such as : the plot is displayed here.
✓ > # Use R as a calculator
✓ > 3+2
✓ [1] 5
Customize the partition/pane layout

Click the checkboxes


to Customize
Run the Repeatedly
 Put the program Run the  Input
cursor here here program data is
here displayed
here

 See the output here


How is a plot created ?
How are the windows cleared ?

Texts in this window can be cleared


How to run the entire code ?

Method 1 :
Select the entire code → RUN
How to run the entire code ?

Output
How to run the entire code ?

MethodSelect the entire code


→ RUN
How to run the entire code ?

Output
How to save the entire code ?
Shows all the files & the
folders in your work space
All visualizations appear here
Define a String Variable, name Define and print a numeric variable, my_value
Verify, what does the variable name contain

There is another way of showing the name, i.e print (“name”) Assign a single value to a multiple variables

# Notice that, the square bracket [1] represents the output line
Assign a range of values to a single variable

# The colon operator (:) returns every integer between two


integers

# Notice that, since the output could not fit in one line, it is displayed in two lines starting with square brackets
Concatenation Operation :

# means, it’s not an executable code

The c() function combines multiple values into a single vector.


The name "c" stands for concatenate or combine.
Using R as a Calculator
R follows the order of operations, e.g.
1. Division
2. Multiplication
3. Addition
4. Subtraction
EXERCISES
1. Basic Arithmetic:

a) Perform a basic arithmetic operation: 5 + 7. Print the result.


b) Calculate the expression: 4 + 6 * 8 - 3. Print the result.
What’s an object ? Just a name that you can use to call up stored data.
What are the Objects ?
• A single digit (integer / double)
• A character ( a string)
• A Boolean (True/False)
• A data frame
• A list of data frames etc.
What are the Objects Names ? Call the data stored in an object
> # object name <- value
➢ bob <- 7 [ bob = object name, 7 = value]
Now
➢ bob*2 = 14
An object can also have multiple values

What if, the object ‘a’ is multiplied by 2

Multiplication operation is carried out element-wise.


What are the naming convention of objects ?
Could start with a small letter i.e. a → z

Could contain, but should not start with :


Digits, i.e. 0 →9
Dot (.), such as : [Link]
Undescore (_), such as longer_name

A popular style is : longerName

R will overwrite any previous information stored in an object without


asking you for permission.
seven <- 10
seven <- 7
2. Create and Use Objects:

a) Create an object called my_number and assign it the value 15.


b) Multiply my_number by 3 and print the result.
3. Create and Use a Vector Object:
a) Create an object called num_vector and assign it a vector of numbers from 10 to 20.
b) Print the object num_vector to confirm if it contains the correct values.
c) Multiply num_vector by 5 and print the result. Observe how R applies the operation element-wise to
the entire vector.
What is a function in R ?
• A function is a set of instructions that performs a specific task.
• Functions help in code reusability, modularity, and better organization.

Types of Functions in R
• Built-in Functions: Predefined functions available in R, such as: sum(), mean(), median(), sqrt(), log(), etc.
• User-defined Functions: Custom functions created by users to perform specific tasks.
Built-in Functions
R comes with many functions that can be used to do sophisticated tasks, such as :
SAMPLING A ROLL OF DICE
✓ The sample function takes two arguments : a vector named x and a number named size
✓ By default, sample builds a sample without replacement
✓ sample will return size elements from the vector :
✓ Function (x, size, replace = FALSE, prob = NULL)
So far we know that : Function ‘mean’
Object ‘a’
Functions are used to create objects,
i.e.
object <- function ()

How does a function operate ? The inner most function mean(a)


is executed first & then the
• A function operates, when we pass outermost function round()
on data, also called arguments,
within its parenthesis.
• The arguments could be : raw data,
an object or the result from another
function
1. Calculate the Mean:
a) Create a vector called data_vector containing the
numbers 3, 7, 12, 19, 25.
b) Use the mean() function to calculate the mean of
data_vector.
c) Save the result into an object called mean_value
and print it.

2. Round the Mean:


a) Use the round() function to round mean_value to
the nearest whole number.
b) Save the result into an object called
rounded_mean and print it.

3. Nested Function:
Combine mean() and round() in a single line of code
to calculate and round the mean of data_vector
directly (without creating intermediate objects).
Save this result in an object called
direct_rounded_mean and print it.
A function can take multiple arguments

Here the argument can still work, even


without its name, e.g. digit

However, in this case, the argument


wont work, without its name, e.g. digit
1. Basic Rounding:
a. Create a variable called num and assign it the
value 3.14159.
b. Use the round() function on num without
specifying any additional arguments. Save the
result to an object called rounded_num and print it.

2. Specifying Decimal Places:


Use the round() function again on num, but this time
round it to two decimal places by specifying the digits
argument. Save the result in an object called
rounded_two_digits and print it.

3. Using Arguments without Names


Use the round() function again on num, to three
decimal places by specifying the digits argument. Save
the result in an object called rounded_three_digits and
Explanation of the digits argument:
print it.
✓ The round() function takes two main arguments:
✓ x: The numeric value to be rounded.
4. Specifying Arguments in any Order
❖ digits: The number of decimal places to round to.
Use the round() function again on num, to four decimal
❖ The digits argument has a default value of 0, which
places by specifying the digits first, followed by num.
means that if you don't specify it, round() will round x to
Save the result in an object called rounded_four_digits
the nearest whole number.
and print it.
User-Defined Functions
Ex 1: Basic Function
Create a function (add_numbers) to add numbers Another way of creating a function (add_numbers) to add numbers

* Continue the code with subsequent lines after pressing (Shift+Enter)


Ex – 2: Function without return statement
Function with default arguments

Create a function “greet” to accept character arguments

* Continue the code with subsequent lines after pressing (Shift+Enter)


Function to deal with variable (i.e. any number) of arguments (…)
Create a function “sum_numbers” to add any number of numerical values

* Continue the code with subsequent lines after pressing (Shift+Enter)

You might also like