R – Basic Syntax:
R Programming is a very popular programming language which is broadly
used in data analysis. The way in which we define its code is quite simple.
The "Hello World!" is the basic program for all the languages, and now we
will understand the syntax of R programming with "Hello world" program.
We can write our code either in command prompt, or we can use an R script
file.
R Command Prompt
It is required that we have already installed the R environment set up in our
system to work on the R command prompt.
After the installation of R environment setup, we can easily start R command
prompt by typing R in our Windows command prompt.
When we press enter after typing R, it will launch interpreter, and we will get
a prompt on which we can code our program.
The code of "Hello World!" in R programming can be written as:
In the above code, the first statement defines a string variable string, where we assign
a string "Hello World!".
The next statement print() is used to print the value which is stored in the variable
string.
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
A variable is a memory location reserved for storing data, and the name
assigned to it is used to access and manipulate the stored data.
The variable name is an identifier for the allocated memory block, which
can hold values of various data types during the program’s execution.
In R, variables are dynamically typed, meaning they do not require explicit
data type declarations.
Instead, a variable in R automatically adopts the data type of the object
assigned to it.
This flexibility allows variables to change their data type as needed,
depending on the values they are assigned.
R supports three ways of variable assignment:
Using equal operator: operators use an arrow or an equal sign to assign
values to variables.
Using the leftward operator: data is copied from right to left.
Using the rightward operator: data is copied from left to right.
Syntax
Types of Variable Creation in R:
Using equal to operators
variable_name = value
using leftward operator
variable_name <- value
using rightward operator
value -> 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
The variables can be assigned values using leftward, rightward and equal to operator.
The values of the variables can be printed using print() or cat() function.
The cat() function combines multiple items into a continuous print output.
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,
Finding Variables
To know all the variables currently available in the workspace we use
the ls() function.
Also the ls() function can use patterns to match the variable names.
DeletingVariables
In R, you can delete variables from your environment using the rm() or remove() functions.
The methods differ slightly depending on whether you are removing individual variables,
multiple specific variables, or clearing the entire workspace.
Deleting from the R Environment
The primary function for removing objects from the R environment is rm() (or remove()).
Delete a single variable:
Ex:
# Assign a value to a variable
my_var <- 10
# Delete the variable
rm(my_var)
Delete multiple specific variables:
You can list multiple variables separated by commas.
var_a <- 1
var_b <- 2
var_c <- 3
# Delete var_a and var_c
rm(var_a, var_c)
Delete all variables in the current session:
This command removes all objects (including functions) from your current global
environment.
Ex:
a=3
cat("A=",a)
rm(a)
print(ls())
output:
A= 3
character(0)