0% found this document useful (0 votes)
34 views3 pages

NPHIL Lec 2 R

Uploaded by

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

NPHIL Lec 2 R

Uploaded by

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

Lesson 2

Data Types in R

Basic data types in R can be divided into the following types:

 numeric - (10.5, 55, 787)


 integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
 complex - (9 + 3i, where "i" is the imaginary part)
 character ( string) - ("k", "R is exciting", "FALSE", "11.5") alway in double quotes
 logical (a.k.a. boolean) - (TRUE or FALSE)

We can use the class() function to check the data type of a variable:

Basic syntax in R

 Basic syntax represents the fundamental rule for a programming language.


 R programs are written either in the terminal or directly in scripts files(editor
windows or console)
 R files are save with .R extension
 A program in R is made up of three things: Variables, Comments, and Keywords.

Keywords in R

Keyword is a reserved word defined for a specific function in R

These words are: function, if, else, for, while, repeat, break, next, TRUE, FALSE, inf,
NAN, NA_real, NA_Integer, NA_Complex, NA_character, NULL

Variables
 Variable is the storage of our program, or a variable is used to store data
Assiigning value to a variable
 Method 1
We use the leftward operator, <- ( the less than sign follow by a hypen)
Code: var_name <- value
 Method 2
We use the assignment operator , = ( the equal to sign)
Code: var_name = value
 Method 3
We use the rightward operator, >- The greater than sign follow by a hypen)
Code: var_name >- value
 To see the list of variables we use in our program , we use the ls() function
Code: ls()
 To remove a variable from the list of variables, we use the remove function.

Code: rm(variable name)


Rules to follow when naming a variable
 It should not match with keywords
 It should be alphanumeric( alphabet and numbers)
 It can a dot(.) or underscore(_) no other symbol allowed
 Starts with alphabet but not digit
 Starts with a dot but not followed by a digit
 Case sensitive( gap, Gap, GAP) are all different variables

Valid name Invalid name


Name .5name(starts with a dot followed by a digit)
Sb_2 3_sb (starts with a digit)
P_2 P M (has a space)
M6 if (keyword)

Comment
Comments are use to keep track of the code you have written. It is for the user only,
so the interpreter ignore it
The # (hash) symbol is use to comment a code, it is usually written above the code.

Practice using rstudio


Determine the datatype for the following:
a) 100 b) 20.99 c) hello world d) 3>1
Create five different variables and store the following object in them
a) 10 b) 15 c) books d) 2.4 e) 6 + 3i

You might also like