R Lanaguage
R Lanaguage
data analysis, and graphical representation. It was developed by statisticians Ross Ihaka and
Robert Gentleman in the mid-1990s and is open-source, which means it is freely available to
anyone. R is widely used by statisticians, data scientists, and researchers for tasks such as:
Statistical analysis: R provides a wide array of statistical techniques, including linear and
Data visualization: R has strong capabilities for producing high-quality plots and charts.
Libraries like ggplot2 allow users to create detailed and customizable visualizations.
Data manipulation: Packages like dplyr and tidyr make it easy to clean, filter, and
Machine learning: R is also used for machine learning tasks, offering tools for building
R's extensibility comes from its active user community, which has contributed thousands of
Key Features:
Code Editor: A text editor with syntax highlighting, auto-completion, and debugging
tools to write and run R code efficiently.
Console: A space to run R commands interactively, allowing users to test code snippets or
run scripts directly.
Environment/History Pane: Displays variables, data frames, and functions loaded into
the R environment, along with a record of previously executed commands.
Plots and Viewer Pane: Allows users to view plots, graphs, and visualizations directly
within RStudio, as well as browse files and manage packages.
Package Management: Easily install, update, and load R packages from CRAN or other
sources.
RStudio makes the R language more accessible, especially for beginners, by integrating various
tools into one environment. It’s widely used in data science, academic research, and statistical
analysis.
INSTALLATION OF R STUDIO SOFTWARE ON WINDOW
Step-1-First, you need to set up an R environment in your local machine. You can
download the same from “r-project.org” and click Install Rstudio.
1-Data Types:-
Numeric:- Numeric data type stores numbers, which can be integers or real (floating-
point) numbers.
X = 3.14
Y=5
Output:
Logical(Boolean):- Logical data type stores TRUE or FALSE values, often used for
condition checking.
> X=TRUE
> Y=FALSE
Output:
> x<-as.integer(42)
>x
Output:
2- variables:- variables are used to store data that can be referenced and manipulated
throughout the code. You assign values to variables and use them to store various data types like
numbers, strings, vectors, or data frames.
Numeric Varibales:-
> num_var<-10.5
> print(num_var)
Output:
Logical (Boolean) Variable:-
Output:
>my_list<-list(name = "alice",age=25,scores
=c(85,90,88),is_student=TRUE)
> print(my_list)
Output:
Matrices:- A matrix is a two-dimensional array where all elements are
of the same type (numeric, character, or logical).
> mat<-matrix(1:9,nrow=3,ncol=3)
> print(mat)
Output:-
Arrays:- Arrays are similar to matrices but can have more than two
dimensions. They can hold data in multiple dimensions, all of the same
type.
> print(arr)
Output:-
Factors:- Factors are used to represent categorical data and store both
the values and the corresponding category levels.
> gender<-factor(c("male","female","female","male"))
> print(gender)
Output:
Data Frames:- Data frames are tables or 2D arrays in which each
column can contain different types of data (numeric, character, etc.).
They are used to store datasets.
+ score = c(90,85,88)
+)
Output:
4- R operators :-
Arthematic operators:- Arithmetic operators are used for mathematical calculations.
> a<-10
> b<-3
> sum_resukt<-a+b
> div_result<-a/b
> mod_result<-a%%b
> exp_resukt<-a^b
Output:
Relational operators:- Relational operators are used to compare two values or variables.
> x<-15
> y<-10
Output:
Logical operators:- Logical operators are used to combine or modify logical statements.
> x=10
> y=20
> z=5
> sum_result=x+y
> product_result=x*y
Output:
PROGRAM DISCRIPTION
A function is a set of statements organized together to perform a specific task. R has a large
number of in-built functions and the user can create their own functions.
In R, a function is an object so the R interpreter is able to pass control to the function, along with
arguments that may be necessary for the function to accomplish the actions.
The function in turn performs its task and returns control to the interpreter as well as any result
which may be stored in other objects.
Benefits of Using Functions:
Code Reusability: Once written, functions can be reused in different parts of a program without
rewriting code.
Modularity: Functions break down large problems into smaller, manageable blocks.
Maintenance: Easier to maintain and update code as modifications in one function do
not affect others.
Built-in Functions in R
R provides a variety of built-in functions that are ready to use for performing different operations,
from mathematical calculations to data manipulation. Some common categories of built-in
functions include:
Mathematical Functions:
Statistical Functions:-