SlideShare a Scribd company logo
1 of 7
Download to read offline
R Basic Programs
1. Write a R program to take input from the user (name and age) and
display the values. Also print the version of R installation.
name = readline(prompt="Input your name: ")
age = readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))
print(R.version.string)
2. Write a R program to create a sequence of numbers from 20 to 50 and
find the mean of numbers from 20 to 60 and sum of numbers from 51 to 91
print("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))
3. Write a R program to create a vector which contains 10 random integer
values between -50 and +50.
v = sample(-50:50, 10, replace=TRUE)
print("Content of the vector:")
print("10 random integer values between -50 and +50:")
print(v)
4. Write a R program to get the first 10 Fibonacci numbers.
Fibonacci <- numeric(10)
Fibonacci[1] <- Fibonacci[2] <- 1
for (i in 3:10)
Fibonacci[i] <- Fibonacci[i - 2] + Fibonacci[i - 1]
print("First 10 Fibonacci numbers:") print(Fibonacci)
5. Write a R program to get all prime numbers up to a given number
prime_numbers <- function(n)
{
if (n >= 2)
{
x = seq(2, n)
prime_nums = c()
for (i in seq(2, n))
{
if (any(x == i))
{
prime_nums = c(prime_nums, i) x = c(x[(x %% i) != 0], i)
}
} return(prime_nums)
}
else
{
stop("Input number should be at least 2.") } }
prime_numbers(12)
6. Write a R program to print the numbers from 1 to 100 and print "Fizz" for
multiples of 3, print "Buzz" for multiples of 5, and print "FizzBuzz" for
multiples of both.
for (n in 1:100) {
if (n %% 3 == 0 & n %% 5 == 0) {print("FizzBuzz")}
else if (n %% 3 == 0) {print("Fizz")}
else if (n %% 5 == 0) {print("Buzz")}
else print(n)
}
7. Write a R program to extract first 10 english letter in lower case and last
10 letters in upper case and extract letters between 22nd
to 24th
letters in
upper case.
print("First 10 letters in lower case:")
t = head(letters, 10)
print(t)
print("Last 10 letters in upper case:")
t = tail(LETTERS, 10)
print(t)
print("Letters between 22nd to 24th letters in upper case:")
e = tail(LETTERS[22:24])
print(e)
8. Write a R program to find the factors of a given number.
print_factors = function(n) {
print(paste("The factors of",n,"are:"))
for(i in 1:n) {
if((n %% i) == 0) {
print(i)
}
}
}
print_factors(4)
print_factors(7)
print_factors(12)
9. Write a R program to find the factors of a given number.
print_factors = function(n) {
print(paste("The factors of",n,"are:"))
for(i in 1:n) {
if((n %% i) == 0) {
print(i)
}
}
}
print_factors(4)
print_factors(7)
print_factors(12)
10.Write a R program to find the maximum and the minimum value of a
given vector.
nums = c(10, 20, 30, 40, 50, 60)
print('Original vector:')
print(nums)
print(paste("Maximum value of the said vector:",max(nums)))
print(paste("Minimum value of the said vector:",min(nums)))
11.Write a R program to get the unique elements of a given string and
unique numbers of vector.
str1 = "The quick brown fox jumps over the lazy dog."
print("Original vector(string)")
print(str1)
print("Unique elements of the said vector:")
print(unique(tolower(str1)))
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
print("Original vector(number)")
print(nums)
print("Unique elements of the said vector:")
print(unique(nums))
12.Write a R program to create three vectors a,b,c with 3 integers.
Combine the three vectors to become a 3×3 matrix where each column
represents a vector. Print the content of the matrix.
a<-c(1,2,3)
b<-c(4,5,6)
c<-c(7,8,9)
m<-cbind(a,b,c)
print("Content of the said matrix:")
print(m)
13.Write a R program to create a list of random numbers in normal
distribution and count occurrences of each value.
n = floor(rnorm(1000, 50, 100))
print('List of random numbers in normal distribution:')
print(n)
t = table(n)
print("Count occurrences of each value:")
print(t)
14.Write a R program to read the .csv file and display the content.
movie_data = read.csv(file="movies.csv", header=TRUE, sep=",")
print("Content of the .csv file:")
print(movie_data)
15.Write a R program to create three vectors numeric data, character data
and logical data. Display the content of the vectors and their type.
a = c(1, 2, 5, 3, 4, 0, -1, -3)
b = c("Red", "Green", "White")
c = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
print(a)
print(typeof(a))
print(b)
print(typeof(b))
print(c)
print(typeof(c))
16.Write a R program to create a 5 × 4 matrix , 3 × 3 matrix with labels and
fill the matrix by rows and 2 × 2 matrix with labels and fill the matrix by
columns.
m1 = matrix(1:20, nrow=5, ncol=4)
print("5 × 4 matrix:")
print(m1)
cells = c(1,3,5,7,8,9,11,12,14)
rnames = c("Row1", "Row2", "Row3")
cnames = c("Col1", "Col2", "Col3")
m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE,
dimnames=list(rnames, cnames))
print("3 × 3 matrix with labels, filled by rows: ")
print(m2)
print("3 × 3 matrix with labels, filled by columns: ")
m3 = matrix(cells, nrow=3, ncol=3, byrow=FALSE,
dimnames=list(rnames, cnames))
print(m3)
17.Write a R program to create an array, passing in a vector of values and
a vector of dimensions. Also provide names for each dimension.
a = array(
6:30,
dim = c(4, 3, 2),
dimnames = list(
c("Col1", "Col2", "Col3", "Col4"),
c("Row1", "Row2", "Row3"),
c("Part1", "Part2")
)
)
print(a)
18.Write a R program to create an array with three columns, three rows,
and two "tables", taking two vectors as input to the array. Print the array.
v1 = c(1, 3, 5, 7)
v2 = c(2, 4, 6, 8, 10)
arra1 = array(c(v1, v2),dim = c(3,3,2))
print(arra1)
19.Write a R program to create a list of elements using vectors, matrices
and a functions. Print the content of the list.
l = list(
c(1, 2, 2, 5, 7, 12),
month.abb,
matrix(c(3, -8, 1, -3), nrow = 2),
asin
)
print("Content of the list:")
print(l)
20.Write a R program to draw an empty plot and an empty plot specify the
axes limits of the graphic.
#print("Empty plot:")
plot.new()
#print("Empty plot specify the axes limits of the graphic:")
plot(1, type="n", xlab="", ylab="", xlim=c(0, 20), ylim=c(0, 20))
21.Write a R program to create a simple bar plot of five subjects marks.
marks = c(70, 95, 80, 74)
barplot(marks,
main = "Comparing marks of 5 subjects",
xlab = "Marks",
ylab = "Subject",
names.arg = c("English", "Science", "Math.", "Hist."),
col = "darkred",
horiz = FALSE)
22.Write a R program to create bell curve of a random normal distribution.
n = floor(rnorm(10000, 500, 100))
t = table(n)
barplot(t)
23.Write a R program to compute sum, mean and product of a given vector
elements.
nums = c(10, 20, 30)
print('Original vector:')
print(nums)
print(paste("Sum of vector elements:",sum(nums)))
print(paste("Mean of vector elements:",mean(nums)))
print(paste("Product of vector elements:",prod(nums)))
24.Write a R program to create a list of heterogeneous data, which include
character, numeric and logical vectors. Print the lists.
my_list = list(Chr="Python", nums = 1:15, flag=TRUE)
print(my_list)
25.Write a R program to create a Dataframes which contain details of 5
employees and display the details.
Employees = data.frame(Name=c("Anastasia S","Dima R","Katherine
S", "JAMES A","LAURA MARTIN"),
Gender=c("M","M","F","F","M"),
Age=c(23,22,25,26,32),
Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"),
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","67
9-77-576")
)
print("Details of the employees:")
print(Employees)
26.Write a R program to create a Dataframes which contain details of 5
employees and display summary of the data.
Employees = data.frame(Name=c("Anastasia S","Dima R","Katherine
S", "JAMES A","LAURA MARTIN"),
Gender=c("M","M","F","F","M"),
Age=c(23,22,25,26,32),
Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"),
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","67
9-77-576")
)
print("Summary of the data:")
print(summary(Employees))
27.Write a R program to create the system's idea of the current date with
and without time.
print("System's idea of the current date with and without time:")
print(Sys.Date())
print(Sys.time())

More Related Content

What's hot

What's hot (20)

FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Programs in array using SWIFT
Programs in array using SWIFTPrograms in array using SWIFT
Programs in array using SWIFT
 
PART 5: RASTER DATA
PART 5: RASTER DATAPART 5: RASTER DATA
PART 5: RASTER DATA
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
PART 6: FROM GEO INTO YOUR REPORT
PART 6: FROM GEO INTO YOUR REPORTPART 6: FROM GEO INTO YOUR REPORT
PART 6: FROM GEO INTO YOUR REPORT
 
Program to reflecta triangle
Program to reflecta triangleProgram to reflecta triangle
Program to reflecta triangle
 
Clauses
ClausesClauses
Clauses
 
week-8x
week-8xweek-8x
week-8x
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
XKE Typeclass
XKE TypeclassXKE Typeclass
XKE Typeclass
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
ML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional LanguageML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional Language
 
some basic C programs with outputs
some basic C programs with outputssome basic C programs with outputs
some basic C programs with outputs
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
 
week-6x
week-6xweek-6x
week-6x
 
PART 3: THE SCRIPTING COMPOSER AND PYTHON
PART 3: THE SCRIPTING COMPOSER AND PYTHONPART 3: THE SCRIPTING COMPOSER AND PYTHON
PART 3: THE SCRIPTING COMPOSER AND PYTHON
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
 

Similar to R basic programs

Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsProf. Dr. K. Adisesha
 
CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL Rishabh-Rawat
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILEAnushka Rai
 
ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxjeyel85227
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeKevlin Henney
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYvikram mahendra
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfrajatxyz
 
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdfsolution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdfparthp5150s
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePeter Solymos
 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..Kamarudheen KV
 

Similar to R basic programs (20)

Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 
CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
 
ANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptxANSHUL RANA - PROGRAM FILE.pptx
ANSHUL RANA - PROGRAM FILE.pptx
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
 
Python : Dictionaries
Python : DictionariesPython : Dictionaries
Python : Dictionaries
 
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdfsolution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
 
paython practical
paython practical paython practical
paython practical
 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..
 
2D array
2D array2D array
2D array
 
R language tutorial.pptx
R language tutorial.pptxR language tutorial.pptx
R language tutorial.pptx
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
ECE-PYTHON.docx
ECE-PYTHON.docxECE-PYTHON.docx
ECE-PYTHON.docx
 
R language introduction
R language introductionR language introduction
R language introduction
 

Recently uploaded

Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxnuruddin69
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 

Recently uploaded (20)

Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 

R basic programs

  • 1. R Basic Programs 1. Write a R program to take input from the user (name and age) and display the values. Also print the version of R installation. name = readline(prompt="Input your name: ") age = readline(prompt="Input your age: ") print(paste("My name is",name, "and I am",age ,"years old.")) print(R.version.string) 2. Write a R program to create a sequence of numbers from 20 to 50 and find the mean of numbers from 20 to 60 and sum of numbers from 51 to 91 print("Sequence of numbers from 20 to 50:") print(seq(20,50)) print("Mean of numbers from 20 to 60:") print(mean(20:60)) print("Sum of numbers from 51 to 91:") print(sum(51:91)) 3. Write a R program to create a vector which contains 10 random integer values between -50 and +50. v = sample(-50:50, 10, replace=TRUE) print("Content of the vector:") print("10 random integer values between -50 and +50:") print(v) 4. Write a R program to get the first 10 Fibonacci numbers. Fibonacci <- numeric(10) Fibonacci[1] <- Fibonacci[2] <- 1 for (i in 3:10) Fibonacci[i] <- Fibonacci[i - 2] + Fibonacci[i - 1] print("First 10 Fibonacci numbers:") print(Fibonacci) 5. Write a R program to get all prime numbers up to a given number prime_numbers <- function(n) { if (n >= 2) { x = seq(2, n) prime_nums = c() for (i in seq(2, n)) { if (any(x == i)) { prime_nums = c(prime_nums, i) x = c(x[(x %% i) != 0], i) } } return(prime_nums) } else { stop("Input number should be at least 2.") } } prime_numbers(12)
  • 2. 6. Write a R program to print the numbers from 1 to 100 and print "Fizz" for multiples of 3, print "Buzz" for multiples of 5, and print "FizzBuzz" for multiples of both. for (n in 1:100) { if (n %% 3 == 0 & n %% 5 == 0) {print("FizzBuzz")} else if (n %% 3 == 0) {print("Fizz")} else if (n %% 5 == 0) {print("Buzz")} else print(n) } 7. Write a R program to extract first 10 english letter in lower case and last 10 letters in upper case and extract letters between 22nd to 24th letters in upper case. print("First 10 letters in lower case:") t = head(letters, 10) print(t) print("Last 10 letters in upper case:") t = tail(LETTERS, 10) print(t) print("Letters between 22nd to 24th letters in upper case:") e = tail(LETTERS[22:24]) print(e) 8. Write a R program to find the factors of a given number. print_factors = function(n) { print(paste("The factors of",n,"are:")) for(i in 1:n) { if((n %% i) == 0) { print(i) }
  • 3. } } print_factors(4) print_factors(7) print_factors(12) 9. Write a R program to find the factors of a given number. print_factors = function(n) { print(paste("The factors of",n,"are:")) for(i in 1:n) { if((n %% i) == 0) { print(i) } } } print_factors(4) print_factors(7) print_factors(12) 10.Write a R program to find the maximum and the minimum value of a given vector. nums = c(10, 20, 30, 40, 50, 60) print('Original vector:') print(nums) print(paste("Maximum value of the said vector:",max(nums))) print(paste("Minimum value of the said vector:",min(nums)))
  • 4. 11.Write a R program to get the unique elements of a given string and unique numbers of vector. str1 = "The quick brown fox jumps over the lazy dog." print("Original vector(string)") print(str1) print("Unique elements of the said vector:") print(unique(tolower(str1))) nums = c(1, 2, 2, 3, 4, 4, 5, 6) print("Original vector(number)") print(nums) print("Unique elements of the said vector:") print(unique(nums)) 12.Write a R program to create three vectors a,b,c with 3 integers. Combine the three vectors to become a 3×3 matrix where each column represents a vector. Print the content of the matrix. a<-c(1,2,3) b<-c(4,5,6) c<-c(7,8,9) m<-cbind(a,b,c) print("Content of the said matrix:") print(m) 13.Write a R program to create a list of random numbers in normal distribution and count occurrences of each value. n = floor(rnorm(1000, 50, 100)) print('List of random numbers in normal distribution:') print(n) t = table(n) print("Count occurrences of each value:") print(t) 14.Write a R program to read the .csv file and display the content. movie_data = read.csv(file="movies.csv", header=TRUE, sep=",") print("Content of the .csv file:") print(movie_data) 15.Write a R program to create three vectors numeric data, character data and logical data. Display the content of the vectors and their type. a = c(1, 2, 5, 3, 4, 0, -1, -3) b = c("Red", "Green", "White") c = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE) print(a) print(typeof(a)) print(b) print(typeof(b)) print(c) print(typeof(c))
  • 5. 16.Write a R program to create a 5 × 4 matrix , 3 × 3 matrix with labels and fill the matrix by rows and 2 × 2 matrix with labels and fill the matrix by columns. m1 = matrix(1:20, nrow=5, ncol=4) print("5 × 4 matrix:") print(m1) cells = c(1,3,5,7,8,9,11,12,14) rnames = c("Row1", "Row2", "Row3") cnames = c("Col1", "Col2", "Col3") m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE, dimnames=list(rnames, cnames)) print("3 × 3 matrix with labels, filled by rows: ") print(m2) print("3 × 3 matrix with labels, filled by columns: ") m3 = matrix(cells, nrow=3, ncol=3, byrow=FALSE, dimnames=list(rnames, cnames)) print(m3) 17.Write a R program to create an array, passing in a vector of values and a vector of dimensions. Also provide names for each dimension. a = array( 6:30, dim = c(4, 3, 2), dimnames = list( c("Col1", "Col2", "Col3", "Col4"), c("Row1", "Row2", "Row3"), c("Part1", "Part2") ) ) print(a) 18.Write a R program to create an array with three columns, three rows, and two "tables", taking two vectors as input to the array. Print the array. v1 = c(1, 3, 5, 7) v2 = c(2, 4, 6, 8, 10) arra1 = array(c(v1, v2),dim = c(3,3,2)) print(arra1) 19.Write a R program to create a list of elements using vectors, matrices and a functions. Print the content of the list. l = list( c(1, 2, 2, 5, 7, 12), month.abb, matrix(c(3, -8, 1, -3), nrow = 2), asin ) print("Content of the list:") print(l)
  • 6. 20.Write a R program to draw an empty plot and an empty plot specify the axes limits of the graphic. #print("Empty plot:") plot.new() #print("Empty plot specify the axes limits of the graphic:") plot(1, type="n", xlab="", ylab="", xlim=c(0, 20), ylim=c(0, 20)) 21.Write a R program to create a simple bar plot of five subjects marks. marks = c(70, 95, 80, 74) barplot(marks, main = "Comparing marks of 5 subjects", xlab = "Marks", ylab = "Subject", names.arg = c("English", "Science", "Math.", "Hist."), col = "darkred", horiz = FALSE) 22.Write a R program to create bell curve of a random normal distribution. n = floor(rnorm(10000, 500, 100)) t = table(n) barplot(t) 23.Write a R program to compute sum, mean and product of a given vector elements. nums = c(10, 20, 30) print('Original vector:') print(nums) print(paste("Sum of vector elements:",sum(nums))) print(paste("Mean of vector elements:",mean(nums))) print(paste("Product of vector elements:",prod(nums))) 24.Write a R program to create a list of heterogeneous data, which include character, numeric and logical vectors. Print the lists. my_list = list(Chr="Python", nums = 1:15, flag=TRUE) print(my_list) 25.Write a R program to create a Dataframes which contain details of 5 employees and display the details. Employees = data.frame(Name=c("Anastasia S","Dima R","Katherine S", "JAMES A","LAURA MARTIN"), Gender=c("M","M","F","F","M"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"), SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","67 9-77-576") ) print("Details of the employees:")
  • 7. print(Employees) 26.Write a R program to create a Dataframes which contain details of 5 employees and display summary of the data. Employees = data.frame(Name=c("Anastasia S","Dima R","Katherine S", "JAMES A","LAURA MARTIN"), Gender=c("M","M","F","F","M"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"), SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","67 9-77-576") ) print("Summary of the data:") print(summary(Employees)) 27.Write a R program to create the system's idea of the current date with and without time. print("System's idea of the current date with and without time:") print(Sys.Date()) print(Sys.time())