1

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 ...
iKcon
  • 71
  • 7
  • I don't think there's anything custom in your code about S3 classes/methods - nor does there need to be. – Gregor Thomas Dec 07 '21 at 20:30
  • The point of S3 is to call different implementations of functions on inputs of different classes. So that, e.g., `sort()` will sort a vector nicely whether you give it a `numeric` vector, a `character` vector, a `Date` vector, etc. There's nothing in your question that seems like you want to do **different** things based on the `class` type. It seems like you want to do the same thing on different data sets, which would just be a regular old function. – Gregor Thomas Dec 07 '21 at 20:36
  • If you want to learn about S3 classes, I'd suggest [the Advanced R chapter on S3](https://adv-r.hadley.nz/s3.html). But what I really think you should start with is [the Functions chapter from R for Data Science.](https://r4ds.had.co.nz/functions.html) from – Gregor Thomas Dec 07 '21 at 20:38
  • @GregorThomas So I should run the function again with the new data set and the code as normal? What if there's a lot of data sets to be checked through? Sorry really confused on this. i thought, just like ```print()``` I could have an execution code – iKcon Dec 07 '21 at 20:48
  • You should write a custom function. You haven't done that, you are stringing together other functions, but you haven't wrapped them in your own function to make it easy to execute the same code again on a different input. You write your own function with the `function()` command, e.g., `my_function <- function(...){...}`. Then you can call your function on different datasets, e.g., `my_function(datadump1111)`, then `my_function(datadump2222)`. Please read the Functions chapter from R for Data Science for a good introduction. – Gregor Thomas Dec 07 '21 at 21:19

0 Answers0