Hi All Did I answer the question 1 and 2 below correctly 1

Hi All,

Did I answer the question 1 and 2 (below) correctly ?

1. Create an S3 class, named “patient3”, for a list object with three variables: id, age, and trt with a single entry having values (“VCUMC005”, 56, FALSE). Create a method “summary” to show the content of a “patient3” object. Demonstrate the usage of your “summary” method.

2. Repeat the problem 4 with an S4 class “patient4”.

______________________________

Answer of Question 1

> S3 = list(id=c(\"VCUMC005\"),age = c(56),trt = c(FALSE))
> class(S3) = \"patient3\"
> attributes(S3)
$names
[1] \"id\" \"age\" \"trt\"

$class
[1] \"patient3\"

> summary(S3)
Length Class Mode   
id 1 -none- character
age 1 -none- numeric
trt 1 -none- logical

> summary.patient3 = function(a){

+ cat (\"Summary of patient objetc \ \")

+ cat (\"Number of patients = \", length(a$id),\"\ \ \")

+ cat (\" id age trt \ \")

+ for ( i in 1 : length(a$id)) {

+ cat (a$id[i],\"\\t\",a$age[i],\"\\t\",a$trt,\"\ \")

+ }

+ }

> summary(S3)

Summary of patient objetc

Number of patients = 1

id age trt

VCUMC005 56 FALSE

________________________

Answer of question 2:

> S4 = new(\"patient4\",id=c(\"VCUMC005\"),age=c(56),trt=c(FALSE))
> show(S4)
An object of class \"patient4\"
Slot \"id\":
[1] \"VCUMC005\"

Slot \"age\":
[1] 56

Slot \"trt\":
[1] FALSE

> summary(S4)
Length Class Mode
1 patient4 S4

> setMethod(\"summary\",\"patient4\",
+ function(object){
+ cat(\"Summary of patient4 object \ \ \")
+ for (i in 1:length(object@id)){
+ cat(\"patient\",object@id[i],\"is of age\",object@age[i],\"and treatemnt status is\",object@trt[i],\"\ \")}
+ }
+ )
[1] \"summary\"
> summary(S4)
Summary of patient4 object

patient VCUMC005 is of age 56 and treatemnt status is FALSE

Solution

Hey there, Your implementation is correct and your answer is right. That was what the questions needed. We have to first create an object for the S3 class , initialise the variable and print the summary.

The same must be done for the S4 class i.e., initialise and print the summary.

At last the demonstration of the implementation of summary function was asked and it is done by printing the details of class S3 and S4

Hi All, Did I answer the question 1 and 2 (below) correctly ? 1. Create an S3 class, named “patient3”, for a list object with three variables: id, age, and trt
Hi All, Did I answer the question 1 and 2 (below) correctly ? 1. Create an S3 class, named “patient3”, for a list object with three variables: id, age, and trt

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site