DEV Community

maxwizard01
maxwizard01

Posted on • Edited on

Introduction to R-programming

πŸ”₯ Introduction to R

πŸ”₯ Introduction to R (1)

πŸ”₯ Data Structure (2)

πŸ”₯ Statistical value (mean, median, mode etc) (3)

πŸ”₯ Tabular Presentation of Data (4)

πŸ”₯ Ploting graph with R

πŸ”₯ constructing frequency distribution with R (6)

Getting Started

R is both a programming language and software environment for statistical computing, which is free and open-source. To get started, you will need To Install two pieces of software:
β€’ R, the actual programming language.
β€’ RStudio, an excellent IDE for working with R.
– Note, you must have R installed to use RStudio. RStudio is simply an interface used to interact with R.

FOR MAC

To Install R

  1. Open an internet browser and go to www.r-project.org
  2. Click the "download R" link in the middle of the page under "Getting Started."
  3. Select a CRAN location (a mirror site) and click the corresponding link.
  4. Click on the "Download R for (Mac) OS X" link at the top of the page.
  5. Click on the file containing the latest version of R under "Files."
  6. Save the .pkg file, double-click it to open, and follow the installation instructions. Now that R is installed, you need to download and install RStudio.
To Install RStudio
  1. Go to www.rstudio.com and click on the "Download RStudio" button.
  2. Click on "Download RStudio Desktop."
  3. Click on the version recommended for your system, or the latest Mac version, save the .dmg file on your computer, double-click it to open, and then drag and drop it to your applications folder.

To Install the SDSFoundations Package

  1. Download SDSFoundations to your desktop (make sure it has the ".tgz" extension).
  2. Open RStudio.
  3. Click on the Packages tab in the bottom right window.
  4. Click "Install."
  5. Select install from "Package Archive File."
  6. Select the SDSFoundations package file from your desktop.
  7. Click install. You are done! You can now delete the SDSpackage file from your desktop.

FOR WINDOW USERS

To Install R:
  1. Open an internet browser and go to www.r-project.org
  2. Click the "download R" link in the middle of the page under "Getting Started."
  3. Select a CRAN location (a mirror site) and click the corresponding link.
  4. Click on the "Download R for Windows" link at the top of the page.
  5. Click on the "install R for the first time" link at the top of the page.
  6. Click "Download R for Windows" and save the executable file somewhere on your computer. Run the .exe file and follow the installation instructions. Now that R is installed, you need to download and install RStudio.
To Install RStudio
  1. Go to www.rstudio.com and click on the "Download RStudio" button.
  2. Click on "Download RStudio Desktop."
  3. Click on the version recommended for your system, or the latest Windows version, and save the executable file. Run the .exe file and follow the installation instructions. ### To Install the SDSFoundations Package
  4. Download SDSFoundation to your desktop (make sure it has the ".zip" extension).
  5. Open RStudio.
  6. Click on the Packages tab in the bottom right window.
  7. Click "Install."
  8. Select install from "Package Archive File." Select the SDSFoundations package file from your desktop.
  9. Click install. You are done! You can now delete the SDSpackage file from your desktop.

The popularity of R is on the rise, and everyday it becomes a better tool for statistical analysis. It even generated this book! (A skill you will learn in this course.) There are many good resources for learning R.
The following few chapters will serve as a whirlwind introduction to R. They are by no means meant to be a complete reference for the R language, but simply an
introduction to the basics that we will need along the way. Several of the more important topics will be re-stressed as they are actually needed for analyses

Data Types

R has a number of basic data types.
πŸ’₯ 1. Character/String: they are non-number that is they are always alphabetic word or alpha-numeric.
Examples: "a", "Statistics", "1 plus 2."

πŸ’₯ 2. Numeric
– Also known as Double. The default type when dealing with numbers.
Examples: 1, 1.0, 42.5

πŸ’₯ 3. Integer
– Examples: 1L, 2L, 42L

πŸ’₯ 4. Complex: they are the imaginary number where their i represent the square root of -1
– Example: 4 + 2i , 5 - 5i

πŸ’₯ 5. Logical
– Two possible values: TRUE and FALSE
– You can also use T and F, but this is not recommended.
– NA is also considered logical.
Let quickly see how to assign variable and gives variable name.

πŸ”₯ Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).

Rules for R variables:

πŸ”₯ A variable name must start with a letter
πŸ”₯ A variable name cannot start with a number
πŸ”₯ A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
πŸ”₯ Variable names are case-sensitive (age, Age and AGE are three different variables)

Examples

type the following codes:

myname = "Oladejo"
my_name = "Abdullahi"
myNAME = "Titilayo"
myName = "Maxwizard"
MYNAME = "code"
myName = "T-Boy"
print(myname)
print(my_name)
print(myNAME)
print(myName)
print(MYNAME)
print(myName)
Enter fullscreen mode Exit fullscreen mode

Result

> print(myname)
[1] "Oladejo"
> print(my_name)
[1] "Abdullahi"
> print(myNAME)
[1] "Titilayo"
> print(myName)
[1] "T-Boy"
> print(MYNAME)
[1] "code"
> print(myName)
Enter fullscreen mode Exit fullscreen mode

Note: all the data above has the same variable names(myname) but the capital and small letter differentiate them so if you declare a variable names be careful with the case.
Now let's try another Example

Example2

type the following codes

name="Oladejo Abdullahi"
Age=12
department="science and Technology"
matricNumber=221382
faculty="Education"
nickname: "Maxwizard01"
# now I have assign values to different variables. let see how I will output them
print(name)
print(age) #this produce error because Age is different from age
print(Age)
Print(department)
#this produce error because Print is different from print
print(department)
print(faculty) 
Enter fullscreen mode Exit fullscreen mode

Result

> name="Oladejo Abdullahi"
> Age=12
> department="science and Technology"
> matricNumber=221382
> faculty="Education"
> nickname: "Maxwizard01"
Error: object 'nickname' not found
> # now I have assign values to different variables. let see how I will output them
> print(name)
[1] "Oladejo Abdullahi"
> print(age) #this produce error because Age is different from age
Error in print(age) : object 'age' not found
> print(Age)
[1] 12
> Print(department) #this produce error because Print is different from print
Error in Print(department) : could not find function "Print"
> print(department)
[1] "science and Technology"
> print(faculty) 
[1] "Education"
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ NOTE: you might be wondering why I wrote "#" and type some words after, well it is called comment.
You can write comment to explain your code in your editor or console.
πŸ’₯ what is comment?
comments are the words or sentence written usually to explain the codes.
πŸ’₯ Fact about Comment.
πŸ”₯ they are not executed.i.e they are usually neglected by the console.
πŸ”₯ Comments can be used to explain R code.
πŸ”₯ Comments can be used to make the code more readable.
πŸ”₯ Comments can be used to prevent execution when testing code.

If you study the Example 2 codes very well then you will discovered that some data are written in quote while some are not.
Yeah those in string are (string or characters ) while the others are just numbers.

Now let's see how to quickly do some simple math operation
Basic Calculations
To get started, we’ll use R like a simple calculator.
Addition, Subtraction, Multiplication and Division

Operators

πŸ”₯* Addition(+)
πŸ”₯* Subtraction(-)
πŸ”₯* Multiplication()
πŸ”₯
Division(/)
πŸ”₯* Exponentiation(^ OR **)

Let's use them for simple Calculations

Example3.1

Copy the following codes

print(4*222)
print(44-33)
Print(34+344)
print(33**3)
print(33^3)
print(405/34)
Enter fullscreen mode Exit fullscreen mode

Result

> print(4*222)
[1] 888
> print(44-33)
[1] 11
> Print(34+344)
Error in Print(34 + 344) : could not find function "Print"
> print(33**3)
[1] 35937
> print(33^3)
[1] 35937
> print(405/34)
[1] 11.91176
Enter fullscreen mode Exit fullscreen mode

Now let's solve some real problems by assigning values to some variables.

Example3.2

let's say we want to find a simple interest on a money of principal #50000 at rate 10% for 5 years.
we all know the formula for interest= principal*Rate *time/100

Now type the following codes

principal=50000
rate=10
time=5
interest=principal*rate*time/100
print(interest)
Enter fullscreen mode Exit fullscreen mode

Result

> principal=50000
> rate=10
> time=5
> interest=principal*rate*time/100
> print(interest)
[1] 25000
>
Enter fullscreen mode Exit fullscreen mode

Example3.3

Let say we need to calculate the mean of the age of 4 boys in class. if
abdullahi,tomiwa,john and Rooney are 13,12,14,10 respectively

Now we all know that maean= sum of the data/number of data.
now let's program it
copy the following codes

 abdulAge=13
 tomiwaAge=12
 johnAge=14
 rooneyAge=10
 numberOfBoys=4
 mean=(abdulAge+tomiwaAge+johnAge+rooneyAge)/numberOfBoys
 print(mean).
Enter fullscreen mode Exit fullscreen mode

RESULT

> abdulAge=13
>  tomiwaAge=12
>  johnAge=14
>  rooneyAge=10
>  numberOfBoys=4
>  mean=(abdulAge+tomiwaAge+johnAge+rooneyAge)/numberOfBoys
>  print(mean)
[1] 12.25
> 
Enter fullscreen mode Exit fullscreen mode

Now try the following questions

  1. write a program to calculate the area of a triangle of base 6 cm and height 5 cm
    note: Area=base*height/2

  2. write a program to calculate the perimeter and area of a square of length 6 cm
    Note: Area=L^2 and perimeter= 4L

You can ask me any question please try to solve the question we shall meet in the next class
You can click here to read the next articles.

πŸ”₯ Introduction to R (1)

πŸ”₯ Data Structure (2)

πŸ”₯ Statistical value (mean, median, mode etc) (3)

πŸ”₯ Tabular Presentation of Data (4)

πŸ”₯ Ploting graph with R

πŸ”₯ constructing frequency distribution with R (6)

Top comments (0)