R - Programming
Assignment 1
Assignment to evaluate understanding of various data structures in R
Please select correct option for below multiple-choice questions
1. Is R a programming language? (1 Point)
TRUE
FALSE
2. Which function is used to list objects in current R environment? (1 Point)
list
ls
rm
None of the above
3. Which ones of below are valid variable names in R? (2 Points)
sales
salesq.1
salesq#1
.1q_sales
10/12/2020
4. Which one of below is a valid logical value in R? (1 Point)
TRUE
True
true
'TRUE'
5. Which parameter is used in sort() to sort values in descending order in R? (1 Point)
descending
decreasing
desc
None of the above
6. Which function is used in R to find number of elements/values in a vector? (1 Point)
class
str
dim
length
7. Which command is used to create a matrix with below structure?
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
(1 Point)
matrix(1:6, 2, byrow= TRUE)
matrix(1:6, 2, byrow= FALSE)
matrix(1:6, 3, byrow= TRUE)
matrix(1:6,2)
8. Which function is used to create a vector from list in R? (1 Point)
list
vector
to.vector
unlist
9. Which function is used to create a data frame in R? (1 Point)
data.frame
dataframe
data_frame
df
10. Which command is used to get output (mentioned in II) if the data frame is as below (mentioned in I)?
I) df1
v1 v2 v3
1 1 John 28.3
2 2 Sam 22.7
3 3 Nicole 29.5
II)
'data.frame' : 3 obs. of 3 variables:
$ v1: int 12 3
$ v2: chr John Sam Nicole
$ v3: num 28.3 22.7 29.5
(1 Point)
str(df1)
summary(df1)
dim(df1)
class(df1)
11. Which command is used to get output (mentioned in II) if the data frame is as below (mentioned in I)?
I) df1
v1 v2 v3
1 1 John 28.3
2 2 Sam 22.7
3 3 Nicole 29.5
II)
v1 v2 v3
Min. : 1.0 Length:3 Min. : 22.70
1st Qu. : 1.5 Class: character 1st Qu. : 25.20
Median : 2.0 Mode: character Median : 28.30
Mean : 2.0 Mean : 26.83
3rd Qu. : 2.5 3rd Qu. : 28.90
Max. : 3.0 Max. : 29.50
(1 Point)
str(df1)
summary(df1)
dim(df1)
class(df1)
12. What is the use of parameter/argument levels in factor function? (1 Point)
To specify values permissible for use in the factor
To only order levels
13. Parameter/argument stringsAsFactors is used in which function in R? (1 Point)
matrix
data.frame
list
None of the above
14. Default value of parameter/argument stringsAsFactors is? (1 Point)
TRUE
FALSE Note: TRUE and FALSE both options are correct.
So if you have chosen either TRUE or FALSE, 1 mark is given.
NA
None of the above
15. If a vector v1 is defined as below, what is the output returned for v1[0]?
v1 = 1:3
(1 Point)
TRUE
NA
integer(0)
16. In R, data structure list can be used to? (1 Point)
Store homogeneous values
Store heterogeneous values
Store object
Create a tabular structure
17. Which function is used to delete/remove an object from R? (1 Point)
rm
delete
both (delete and rm)
None of the above
Section 3 - Assignment: Descriptive questions
Please enter answers for below questions with complete information
18. Specify differences and similarities between str and summary function. (2 Points)
Similarity:
1. Both can be applied on objects in R to derive additional information.
Difference:
1. Str() - Provides column/variable name, data type.
- Displays number of observations and variables in the data.frame.
- For factors, it displays whether it is ordered or unordered.
2. Summary() - Provides statistical summary for each variable.
- Displays mean and quartile values.
19. Specify differences and similarities between a vector and a list. (2 Points)
Similarity:
1. Both list and vector are 1 dimensional object in R.
Difference:
1. Vector can hold homogeneous values.
2. List is capable of holding heterogeneous values.
3. It is possible to store objects in list which is not possible in vector.
4. Implicit coercion happens in vectors.
20. Specify differences and similarities between a vector and a factor. (2 Points)
Similarity:
1. Both factor and vector are 1 dimensional object in R.
2. Both accept homogeneous values.
Difference:
1. Vector can hold values which could be discrete(categorical) or continuous.
2. Factor holds homogeneous values which is discrete(categorical).
3. A factor could be ordered or unordered.
4. Default levels of a factor are distinct values in the variable unless it is mentioned as a value
for parameter levels in the factor().
21. Specify differences and similarities between implicit and explicit coercion in R. (2 Points)
Similarity – Both perform data type conversion.
Difference:
1. Explicit coercion is used by coder/developer to change/update the type of data
an object holds.
2. Type specific functions are used for explicit coercion like as.numeric() to change
the data type of object to numeric.
3. Implicit coercion happens automatically in case of vectors when values of
different data types are added/mentioned as elements in the vector.
4. For implicit coercion, below hierarchy is followed:
Character > Numeric > Integer > Logical
22. Specify differences and similarities between head and tail functions in R. (2 Points)
Similarity:
1. Both head and tail functions are used to subset a 2-dimensional object in R.
2. Both functions return certain number of rows/observations from the object.
3. By default, both functions return 6 observations.
Difference:
1. head() returns first n rows from the object.
2. tail() returns last n rows from the object.
23. Specify differences and similarities cbind and rbind functions in R. (2 Points)
Similarity:
1. Both cbind and rbind functions are used to add either variable/observation to a
2-dimensional object in R.
Difference:
1. cbind() is used to add a column/variable to a 2-dmensional object.
2. rbind() is used to add a row/observation to a 2-dmensional object.