UNIT-II
R – Basic Syntax:
R Programming is a very popular programming language which is broadly used in data
analysis.
We can write our code either in command prompt, or we can use an R script file.
R Command Prompt:
installed the R environment set up & After the installation of R environment setup
start R command prompt by typing R in our Windows command prompt.
Data Types in R Programming Language
Each variable in R has an associated data type.
Each R-Data Type requires different amounts of memory and has some specific operations
which can be performed over it.
Numeric Data type in R
Decimal values are called numeric in R. It is the default R data type for numbers in R.
If you assign a decimal value to a variable x as follows, x will be of numeric type.
Real numbers with a decimal point are represented using this data type in R. It uses a format for
double-precision floating-point numbers to represent numerical values.
Integer Data Type
The integer data type specifies real values without decimal points. We use the suffix L to specify
integer data.
For example,
Character Data type in R
R supports character data types where you have all the alphabets and special characters.
It stores character values or strings.
Strings in R can contain alphabets, numbers, and symbols.
You can use single quotes '' or double quotes "" to represent strings.
In general, we use:
'' for character variables
"" for string variables
For example, 'A' is a single character and "Apple" is a string.
Complex Data Type
The complex data type is used to specify purely imaginary values in R. We use the suffix i to
specify the imaginary part.
For example,
Logical Data Type
The logical data type in R is also known as boolean data type. It can only have two
values: TRUE and FALSE .
For example,
Creating Variables in R
Variables are containers for storing data values.
R does not have a command for declaring a variable.
A variable is created the moment you first assign a value to it.
To assign a value to a variable, use the <- sign. To output (or print) the variable
value, just type the variable name:
Example:
Rules to Declare R Variables
As per our requirements, we can use any name for our variables. However, there are certain rules
that need to be followed while creating a variable:
A variable name in R can be created using letters, digits, periods, and underscores.
You can start a variable name with a letter or a period, but not with digits.
If a variable name starts with a dot, you can't follow it with digits.
R is case sensitive. This means that age and Age are treated as different variables.
We have some reserved words that cannot be used as variable names.
Variable Assignment
There a number of ways to assign a value to a variable, it does not depend on the type
of value being assigned.
There is no need to declare your variable first:
Data types of R Variables
Depending on the type of data that you want to store, variables can be divided into the following
types.
1. Boolean Variables
It stores single bit data which is either TRUE or FALSE . Here, TRUE means yes and FALSE means
no.
For example,