This was an attempt to understand OOP - taking a tutorial in it now. I'm very new at R and I would consider deleting the question as it is not well formulated. I would not use it for learning or guidance
Part 1 - I want to build a class in S3 using datadump1111
data. I want to call the S3 object a50survey
then I want to ouput stuff. This seems to work but I'm not sure I made a proper S3 class or the function is working like it normally should.
a50DATA <- datadump1111
inputdata <- sapply(a50DATA, function(x) t(sapply(x, table)))
a50survey <- sapply(inputdata, function(x) colSums(prop.table(x)))
print(a50survey)
class(a50survey)
a50survey$drugs
> a50survey$drugs
Not Tried once Occasional Regular
0.72 0.12 0.14 0.02
Part 2 What I'm really aiming for is to introduce new data datadump2222
instead of the original datadump1111
I was trying to do that by
a50DATA <- datadump2222
a50survey$drugs
What I get is
> a50survey$drugs
Not Tried once Occasional Regular
0.72 0.12 0.14 0.02
What I should get is
$drugs
Not Tried once Occasional Regular
0.52 0.14 0.34 0.00
...and when I run a50survey
I was hoping that the the addition of the new a50DATA
would get picked up by the S3 object a50survey
and give me another set of outputs correct for the new dataset i.e. datadump2222
. But it returns the data output from datadump1111
Request for help Can you guide me simply to the solution as I want to understand and replicate this? Thank you
The ouput from datadump1111
is here
> dim(datadump1111)
[1] 50 4
> str(datadump1111)
'data.frame': 50 obs. of 4 variables:
$ alcohol: Factor w/ 5 levels "Not","Once or Twice a week",..: 3 2 3 2 4 4 3 4 2 4 ...
$ drugs : Factor w/ 4 levels "Not","Tried once",..: 1 3 1 1 3 1 2 3 1 1 ...
$ smoke : Factor w/ 3 levels "Not","Occasional",..: 1 3 1 1 1 3 3 3 1 2 ...
$ sport : Factor w/ 2 levels "Not regular",..: 1 1 1 1 2 2 2 2 1 2 ...