0% found this document useful (0 votes)
352 views3 pages

Assignment 1 With Answers

The document provides sample R code and answers for creating and manipulating different data structures in R including vectors, matrices, lists, and arrays. Some key tasks covered include: 1) Creating vectors, multiplying them, and extracting subsets of the results. 2) Generating a vector of names, getting its length and accessing specific elements. 3) Creating a matrix from a vector, adding row and column names, and extracting elements above a threshold. 4) Creating a list containing different data types and naming its elements. 5) Removing an element from the created list. 6) Creating a three-dimensional array from columns and displaying its content.

Uploaded by

prabhulean14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
352 views3 pages

Assignment 1 With Answers

The document provides sample R code and answers for creating and manipulating different data structures in R including vectors, matrices, lists, and arrays. Some key tasks covered include: 1) Creating vectors, multiplying them, and extracting subsets of the results. 2) Generating a vector of names, getting its length and accessing specific elements. 3) Creating a matrix from a vector, adding row and column names, and extracting elements above a threshold. 4) Creating a list containing different data types and naming its elements. 5) Removing an element from the created list. 6) Creating a three-dimensional array from columns and displaying its content.

Uploaded by

prabhulean14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment – 1 with Answers

1. Create the following vectors in R.


a = (5, 10, 15, 20, ..., 160)
b = (87, 86, 85, ..., 56)
Use vector arithmetic to multiply these vectors and call
the result in d. Select subsets of d to identify the
following.
(a) What are the 19th, 20th, and 21st elements of d?
(b) What are all of the elements of d which are less
than 2000?
(c) How many elements of d are greater than 6000?
Ans :
> a=seq(5,160,by=5)
> b=seq(87,56,by=-1)
> d=a*b
> d[19]
> d[20]
> d[21]
> d[d<2000]
> length(d[d>6000)]
2. Generate a vector with some of your friend's names.
Get the length of vector and access the 2nd and 8th
friend’s names from the above vector.
Ans :
> vec=c(‘aa’,’bb’,’cc’,’dd’,’ee’,’ff’,’gg’,hh’,’ii’,’jj’)
> length(vec)
> vec[c(2,8)]

3. A create a matrix by taking a given vector of


numbers as input and define the column and row names.
Then Display the matrix.
Ans :
> Mat=matrix(c(33,11,22,55,44,66),3,2)
> rn=c(‘r1’,’r2’,’r3’)
> cn=c(‘c1’,’c2’)
> dimnames(Mat)<-list(rn,cn)
> Mat

4. Using R program, extract the elements whose value


is greater than 6 from a matrix created in Question 3.
Ans :
> m=Mat[Mat>6]
5. Create a list containing a strings, logical values,
vector, a matrix and a list and give names to the
elements in the list.
Ans :
>lot=list(c(‘python’,’sql’,’mongo’,’bigdata’),c(T,F,T,
T),c(4,2,7,9,3),matrix(c(2,4,8,1),2,2),list(‘colors’,88:6
6))

6. Remove the second element in the list created in


Question 5.
Ans :
> lot[2]=NULL
> lot

7. Create an array using five given columns, three given


rows, and two given tables and display the content of
the array.
Ans :
> v1=c(22,33,12,65,38,75)
> v2=c(57,93,84,14,6)
> res=array(c(v1,v2),dim=c(3,5,2))
> res

You might also like