R Assignment Questions
1) How to add two vectors of integers type and length 3 ?
A. Eg.: A<-c(9,4,3)
B<-c(2,4,3)
Sum<-A+B
Sum
2) How to multiply two vectors of integers type and length 3?
B. Eg.: A<-c(9,4,3)
B<-c(2,4,3)
Product<-A*B
Product
3) Write a R program to test whether a given vector contains a
specified element?
A. Eg.: any(grep(9,A))
4) Create a list containing a vector, a matrix and a list and add
element at the end of the list?
A. Eg.:
List=list(c("Name","place","Animal","Thing"),matrix(c(34,6,33,67,9
6,22),nrow=2),list(23,44,56.23))
List[4]=list(c(34,45,57))
List
5)Create Three list and combine all the list into
one list?
A. List_1=list(23,45,22,67,87)
List_2=list("Name","place","Animal","Thing")
List_3=list("SAS","R")
List=list(List_1,List_2,List_3)
List
6) Find out the levels of factors of a given
below vector?
Y=C(1, 2, 3, 3, 4, NA, 3, 2, 4, 5, NA, 5)
A. Levels: 1 2 3 4 5 NA
7) Write an R program to create an ordered factor from data
consisting of the names of months.
A. X=c('Jan','July','Apr','Dec')
Y=factor(X,levels=c('Jan','Feb','Mar','Apr','May','June','July','Sept','
Oct','Nov','Dec'),ordered=TRUE)
8) Create a data frame by taking following
variables & find out the structure of data.
name = "RX","TX","VX" "FX"
V1 = 10,30,40,90
V2 = 0.5,0.8.1.5.1.9
Logi=TRUE,FALSE,TRUE,TRUE
A. Dataframe=data.frame(name=c("RX","TX","VX"
"FX")
V1=c(10,30,40,90)
V2=c(0.5,0.8.1.5.1.9)
Logi=c(TRUE,FALSE,TRUE,TRUE))
Print(str(Dataframe))
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)
A. print(min(nums))
print(max(nums))
11) How to create an array with three columns, three rows, and
two "tables", taking two vectors as input to the array.
A. A=c(23,55,22,67,89,5,6)
B=c(66,9,23,56,20,34,22)
Z=array(c(A,B),dim=c(3,3,2))
12)How to create an empty data frame.
A. Dataframe=data.frame()
13)How to get the structure of following data frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A.print(str(exam_data))
14) How to get the statistical summary of the data of a
following data frame?
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. print(summary(exam_data))
16)How to extract specific column from a given data frame
without using indexing method?
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. Eg.: print(exam_data$score)
17) Write a R program to extract first two rows from a given data
frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. print(exam_data[,c(1,2)])
18)How to extract 3rd and 5th rows with 1st and 3rd columns from a
given data frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. print(exam_data[c(3,5),c(1,3)])
19) Write a R program to add a new column in a given data
frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. Age=c(20,19,22,23,19,18,21,21,22,23)
cbind(exam_data,Age)
20) How to add new row(s) to an existing data frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. X=list(‘Agna’,20,3,’yes’)
rbind(exam_data,X)
21)How to drop 3rd column(s) by name from a given data frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
)
A. exam_data$attempts=NULL
22) How to drop 2nd & 4th row(s) from a given data frame.
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine',
'James', 'Emily', 'Michael', 'Matthew', 'Laura',
'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5,
8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no',
'yes', 'yes', 'no', 'no', 'yes')
A. print(exam_data[c(-2,-4),])