You are on page 1of 4

DATA SCIENCE BASIC DATA TYPES AND STRUCTURES

QUESTIONS

1. Create a vector named Country with following values : USA, Canada, Australia, India,
Singapore and China.
2. Create a vector named Data with following values : 11, 11.6, 126, 234.5, 112 and 98.56.
3. Create a vector named NUM with following values : 1.5 to 6.5 (both included).
4. In the vector given below, how will you access the value “Washington”
City<- c("Delhi", "Toronto", "Washington", “Shanghai”)
5. In the vector given below, how will you access the value “Delhi“ and “Washington”
City<- c("Delhi", "Toronto", "Washington", “Shanghai”)
6. In the vector given below, how will you change the value “Toronto“ to “Sydney”
City<- c("Delhi", "Toronto", "Washington", “Shanghai”)
7. Construct a list with the following data: "Welcome", (11, 12 ,13, 14), 32.5, "hello",
Where first and last elements are string, 2nd one is vector of numeric values, third one is
a numeric.
8. Create a matrix by name mat1 which looks like the one below:

9. Create a matrix by name mat1 which looks like the one below:

10. Write R statement to give the following data in 2 by 2 matrix organised in row order
("Delhi","Chennai","Kolkata","Mumbai")
DATA SCIENCE BASIC DATA TYPES AND STRUCTURES
ANSWERS

1. Create a vector named Country with following values – USA, Canada, Australia, India,
Singapore and China.
Ans: Country<- c("USA", "Canada", "India", “Singapore”, “China”)
Output: "USA" "Canada" "India" "Singapore" "China"
2. Create a vector named Data with following values – 11, 11.6. 126, 234.5, 112 and 98.56.
Ans: Data<- c(11, 11.6, 126, 234.5, 112, 98.56)
Output: 11.00 11.60 126.00 234.50 112.00 98.56
3.Create a vector named NUM with following values – 1.5 to 6.5 (both included).
Ans: NUM<- 1.5:6.5
Output: 1.5 2.5 3.5 4.5 5.5 6.5

4. In the vector given below, how will you access the value “Washington”
City<- c("Delhi", "Toronto", "Washington", “Shanghai”)
Ans: City[3]
"Washington"

5. In the vector given below, how will you access the value “Delhi“ and
“Washington”
City<- c("Delhi", "Toronto", "Washington", “Shanghai”)
Ans: City <- c("Delhi", "Toronto", "Washington", "Shanghai")

City[c(1,3)]
Output: "Delhi" "Washington"

6. In the vector given below, how will you change the value “Toronto“ to
“Sydney”
City<- c("Delhi", "Toronto", "Washington", “Shanghai”)

City <- c("Delhi", "Toronto", "Washington", "Shanghai")

City[2]<-"Sydney"
City
Output: "Delhi" "Sydney" "Washington" "Shanghai"

7. Construct a list with the following data: "Welcome", (11 12 13 14),


32.5, "hello", Where first and last elements are string, 2 nd one is vector of
numeric values, third one is a numeric.
Ans: list<-list("Welcome",c(11, 12, 13, 14),32.5,"hello")

8. Create a matrix by name mat1 which looks like the one below:

Ans: mat1<-matrix(c(-1,-2,-3,-4,-5,-6), nrow=2, ncol=3)


mat1
9. Create a matrix by name mat1 which looks like the one below:

Ans: mat2<-matrix(c("Delhi","Chennai","Kolkata","Mumbai"),nrow=2,ncol=2)
mat2
10. Write R statement to give the following data in 2 by 2 matrix organised in row order
("Delhi","Chennai","Kolkata","Mumbai")
Ans:mat2<-matrix(c("Delhi","Chennai","Kolkata","Mumbai"), nrow=2,ncol=2, byrow=TRUE)
mat2

You might also like