R - Assignment1 - Solution
R - Assignment1 - Solution
Assignment 1
Assignment to evaluate understanding of various data structures in R
TRUE
FALSE
list
ls
rm
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'
descending
decreasing
desc
class
str
dim
length
7. Which command is used to create a matrix with below structure?
matrix(1:6,2)
list
vector
to.vector
unlist
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)
matrix
data.frame
list
TRUE
NA
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 object
rm
delete
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.
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)
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.