0
0
R Programmingprogramming~10 mins

S4 object system in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a new S4 class named "Person".

R Programming
setClass("Person", slots = list(name = "character", age = [1]))
Drag options to blanks, or click blank then click option'
Anumeric
Blogical
Cinteger
Ddata.frame
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'numeric' instead of 'integer' for age slot.
Using 'logical' which is for TRUE/FALSE values.
2fill in blank
medium

Complete the code to create a new object of class "Person" with name "Alice" and age 30.

R Programming
alice <- new("Person", name = "Alice", age = [1])
Drag options to blanks, or click blank then click option'
A"30"
B30L
C30.0
Das.character(30)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "30" instead of an integer.
Using a numeric 30.0 which is double, not integer.
3fill in blank
hard

Fix the error in the method definition for the "show" function for class "Person".

R Programming
setMethod("show", "Person", function(object) {
  cat("Name:", object@name, "\n")
  cat("Age:", object@[1], "\n")
})
Drag options to blanks, or click blank then click option'
AAge
Bages
CAGE
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalized or plural forms of the slot name.
Using a non-existent slot name.
4fill in blank
hard

Fill both blanks to define a method "birthday" that increases the age by 1 for a "Person" object.

R Programming
setGeneric("birthday", function(object) standardGeneric("birthday"))

setMethod("birthday", "Person", function(object) {
  object@[1] <- object@[2] + 1
  return(object)
})
Drag options to blanks, or click blank then click option'
Aage
Bname
Attempts:
3 left
💡 Hint
Common Mistakes
Using different slot names for assignment and addition.
Trying to update the name slot instead of age.
5fill in blank
hard

Fill all three blanks to create a valid S4 class "Employee" that extends "Person" and adds a slot "salary" of type numeric.

R Programming
setClass("Employee",
  slots = list([1] = [2]),
  contains = [3]
)
Drag options to blanks, or click blank then click option'
Asalary
Bnumeric
C"Person"
Dcharacter
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong slot names or types.
Not quoting the class name in 'contains'.