Rprograms CSE
Rprograms CSE
1.Sample Program
Output:
2.Write a R program to take input from the user (name and age ) and display the values.Also print
the version of R installation.
print(R.version.string)
Output1:
name = "Python";
n1 = 10;
n2 = 0.5
nums = c(10,20,30,40,50,60)
print(ls())
print(ls.str())
Output:
n1 : num 10
n2 : num 0.5
4.Write a R program to create a sequence of numbers from 20 t0 50 and find the mean of numbers
from 20 to 60 and the sum of numbers from 51 to 91.
print(seq(20,50))
print(mean(20:60))
print(sum(51:91))
Output:
[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
[26] 45 46 47 48 49 50
[1] 40
[1] 2911
barplot(marks,
xlab = "Marks",
ylab = "Subject",
col = "darkgreen",
horiz = FALSE)
Output:
5) Write a R program to get the unique elements of a given string and unique numbers of vector
print("Original vector(string)")
print(str1)
print(unique(tolower(str1)))
print(unique(nums))
OUTPUT:
6) 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(m)
Output:
abc
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
7) Write a R program to create a 5 x 4 matrix , 3 x 3 matrix with labels and fill the matrix by rows and
2 × 2 matrix with labels and fill the matrix by columns.
print("5 * 4 matrix:")
print(m1)
cells =c(1,3,5,7,8,9,11,12,14)
print(m2)
print(m3)
Output:
Row1 1 7 11
Row2 3 8 12
Row3 5 9 14
8) Write a R program to combine three arrays so that the first row of the first array is followed by the
first row of the second array and then first row of the third array.
print("num1")
print(num1)
print("num2")
print(num2)
print("num3")
print(num3)
a = matrix(t(cbind(num1,num2,num3)),ncol=3,byrow=T)
print("Combine three arrays, taking one row from each one by one:")
print(a)
Output:
[1] "Combine three arrays, taking one row from each one by one:"
9) Write a R program to create a two-dimensional 5x3 array of sequence of even integers greater
than 50.
print(a)
Output:
[1,] 50 60 70
[2,] 52 62 72
[3,] 54 64 74
[4,] 56 66 76
[5,] 58 68 78
10) Write a R program to create an array using four given columns, three given rows, and two given
tables and display the content of the array.
array1 = array(1:30,dim=c(3,4,2))
print(array1)
Output:
,,1
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
,,2
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
df = data.frame(Ints=integer(),
Doubles=double(),
Characters=character(),
Logicals=logical(),
Factor=factor(),
stringsAsFactors=FALSE)
print(str(df))
print(df)
Output:
12) Write a R program to create a data frame from four given vectors.
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas')
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1)
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
print(name)
print(score)
print(attempts)
print(qualify)
print(df)
Output:
[1] 12.5 9.0 16.5 12.0 9.0 20.0 14.5 13.5 8.0 19.0
[1] 1 3 2 3 2 3 1 1 2 1
[1] "yes" "no" "yes" "no" "no" "yes" "yes" "no" "no" "yes"
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
13) Write a R program to create a data frame using two given vectors and display the duplicated
elements and unique rows of the said data frame.
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
ab = data.frame(a,b)
print(ab)
print(duplicated(ab))
print(unique(ab))
Output:
a b
1 10 10
2 20 30
4 10 20
5 40 0
6 50 50
8 30 30
14) Write a R program to save the information of a data frame in a file and display the information of
the file.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
print("Original dataframe:")
print(exam_data)
save(exam_data,file="data.rda")
load("data.rda")
file.info("data.rda")
Output:
2 Dima 9.0 3 no
4 James 12.0 3 no
5 Emily 9.0 2 no
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
> save(exam_data,file="data.rda")
> load("data.rda")
> file.info("data.rda")
atime exe
l = list()
print("List of vectors:")
print(l)
result = do.call(rbind, l)
print("New Matrix:")
print(result)
Output:
[[1]]
[1] 1 1 2 3 4
[[2]]
[1] 2 1 2 3 4
[[3]]
[1] 3 1 2 3 4
[[4]]
[1] 4 1 2 3 4
[[5]]
[1] 5 1 2 3 4
[1,] 1 1 2 3 4
[2,] 2 1 2 3 4
[3,] 3 1 2 3 4
[4,] 4 1 2 3 4
[5,] 5 1 2 3 4
16) Write a R program to concatenate two given matrices of same column but different rows.
x = matrix(1:12, ncol=3)
y = matrix(13:24, ncol=3)
print("Matrix-1")
print(x)
print("Matrix-2")
print(y)
result = dim(rbind(x,y))
print(result)
print(dim(rbind(x,y)))
Output:
[1] "Matrix-1"
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[1] "Matrix-2"
[1,] 13 17 21
[2,] 14 18 22
[3,] 15 19 23
[4,] 16 20 24
[1] 8 3
[1] 8 3
17) Write a R program to find row and column index of maximum and minimum value in a given
matrix.
Output:
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
row col
[1,] 4 4
row col
[1,] 1 1
vector = c()
values = c(0,1,2,3,4,5,6,7,8,9)
for (i in 1:length(values))
print(vector)
Output:
[1] 0 1 2 3 4 5 6 7 8 9
19) Write a R program to multiply two vectors of integers type and length 3.
print("Original Vectors:")
print(x)
print(y)
z=x*y
print(z)
Output:
[1] 10 20 30
[1] 20 10 40
20) Write a R program to find Sum, Mean and Product of a Vector, ignore element like NA or NaN.
print("Sum:")
print(sum(x, na.rm=TRUE))
print("Mean:")
print(mean(x, na.rm=TRUE))
print("Product:")
print(prod(x, na.rm=TRUE))
Output:
[1] "Sum:"
[1] 60
[1] "Mean:"
[1] 20
[1] "Product:"
[1] 6000
21) Write a R program to list containing a vector, a matrix and a list and give names to the elements
in the list.
Output:
$Color
$`Odd numbers`
[1,] 1 5 9
[2,] 3 7 11
$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
$Color
$`Odd numbers`
[1,] 1 5 9
[2,] 3 7 11
22) Write a R program to create a list containing a vector, a matrix and a list and give names to the
elements in the list. Access the first and second element of the list.
list_data <-list(c("CSE","CSM","IT"),matrix(c(1,0,1,0,1,0,0,0,1),nrow=3),list("","PHP","Java"))
print("List:")
print(list_data)
print(list_data)
print('1st element:')
print(list_data[1])
print('2nd element:')
print(list_data[2])
Output:
[1] "List:"
[[1]]
[[2]]
[1,] 1 0 0
[2,] 0 1 0
[3,] 1 0 1
[[3]]
[[3]][[1]]
[1] ""
[[3]][[2]]
[1] "PHP"
[[3]][[3]]
[1] "Java"
$Color
$`Odd numbers`
[1,] 1 0 0
[2,] 0 1 0
[3,] 1 0 1
$`Language(s)`
$`Language(s)`[[1]]
[1] ""
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
$Color
$`Odd numbers`
[1,] 1 0 0
[2,] 0 1 0
[3,] 1 0 1
23)Write a R program to create a list containing a vector,a matrix and a list and remove the second
element.
list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),
print("List:")
print(list_data)
list_data[2] = NULL
print("New list:")
print(list_data)
Output:
[1] "List:"
[[1]]
[[2]]
[1,] 1 5 9
[2,] 3 7 11
[[3]]
[[3]][[1]]
[1] "Python"
[[3]][[2]]
[1] "PHP"
[[3]][[3]]
[1] "Java"
[[1]]
[[2]][[1]]
[1] "Python"
[[2]][[2]]
[1] "PHP"
[[2]][[3]]
[1] "Java"
print(x)
e = lapply(x, '[[', 2)
print(e)
Output:
[[1]]
[[1]][[1]]
[1] 0
[[1]][[2]]
[1] 2
[[2]]
[[2]][[1]]
[1] 3
[[2]][[2]]
[1] 4
[[3]]
[[3]][[1]]
[1] 5
[[3]][[2]]
[1] 6
[[1]]
[1] 2
[[2]]
[1] 4
[[3]]
[1] 6
n1 = list(1,2,3)
print("Original lists:")
print(n1)
print(c1)
print(mlist)
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Red"
[[5]]
[1] "Green"
[[6]]
[1] "Black"
26) Write a R program to create a list named s containing sequence of 15 capital letters, starting
from ‘E’.
print(l)
Output:
[1] "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
27) Write a R program to assign new names "a", "b" and "c" to the elements of a given list.
print("Original list:")
print(list1)
print("Assign new names 'one', 'two' and 'three' to the elements of the said list")
print(list1)
Output:
$g1
[1] 1 2 3 4 5 6 7 8 9 10
$g2
$g3
[1] "HTML"
[1] "Assign new names 'one', 'two' and 'three' to the elements of the said list"
$one
[1] 1 2 3 4 5 6 7 8 9 10
$two
$three
[1] "HTML"
print("Original vector:")
print(v)
print(levels(factor(v)))
Output:
[1]"Original vector:"
[1] 1 2 3 3 4 NA 3 2 4 5 NA 5
29.Write a R program to create an ordered factor from data consisting of the names of months.
mons_v = c("March","April","January","November","January",
"September","October","September","November","August","February",
"January","November","November","February","May","August","February",
"July","December","August","August","September","November","September",
"February","April")
print("Original vector:")
print(mons_v)
f = factor(mons_v)
print(f)
print(table(f))
Output:
11 Levels: April August December February January July March May ... September
2 4 1 4 3 1 1 1
5 1 4
print("Original factors:")
print(f1)
print(f2)
f = factor(c(levels(f1)[f1], levels(f2)[f2]))
print(f)
Output:
[1] I U Z C P K
Levels: C I K P U Z
[1] G J C M N X
Levels: C G J M N X
[1] I U Z C P K G J C M N X
Levels: C G I J K M N P U X Z