S3 classes converting a dataframe to an R object

157 views
Skip to first unread message

SB

unread,
Dec 28, 2016, 7:09:19 AM12/28/16
to R-help-archive

Hi there,

I was using another dataset to make S3 classes. I wanted to make a class called “LongitudinalData” that characterizes the structure of this longitudinal dataset. Then I wanted to design classes to represent the concept of a “subject”, a “visit”, and a “room”.

Furthermore, once I have class defined I wanted to  to implement the following functions, Can anyone point me in right direction . thanks in advance

  1. make_LD: a function that converts a data frame into a “LongitudinalData” object
  2. subject: a generic function for extracting subject-specific information
  3. visit: a generic function for extracting visit-specific information
  4. room: a generic function for extracting room-specific information

However, following your student example I am not able to get my class defined. I am kind of stuck how should I proceed with this problem. 

My code : Could you please give some hints or suggestion to solve this problem. Thank in advance

Ldat <- read_csv("MIE.csv")

1- Create the class

make_LD <- function(id, visit, room){

  me <- list(

   id = id,

    visit = visit,

    room = room

  )

  ## Set the name for the class

  class(me) <- append(class(me),"LongitudinalData")

  return(me)

}


> x <- make_LD(dat)

Error in make_LD(dat) : argument "visit" is missing, with no default

> print(class(x))

[1] "list"             "LongitudinalData"

> # [1] "LongitudinalData"

> print(x)

$subject

NULL

$visit

NULL

$room

NULL

attr(,"class")

[1] "list"             "LongitudinalData"

"""

> x <- (data)
Error in LongitudinalData”(data) : argument "visit" is missing, with no default
> print(class(x))
Error in print(class(x)) : object 'x' not found
> print(x)
Error in print(x) : object 'x' not found

""


print.student <- function(object) {
  cat("\nID =", object$id,
      "\nVisit =", object$visit, 
      "\nRoom =", object$room, 
      "\nValue =", object$value, 
      "\nTimepoint =", object$timepoint, "\n") 
}



##

find_element <- function(arr, val) {
  count = 1
  for (i in arr) {
    if (i == val) {
      return(count)
    } else
      count = count + 1
  }
  return("No Match")
}

This will yield

arr <- 1:10
find_element(arr, 10)
#[1] 10
find_element(arr, 12)
#[1] "No Match"




MIE.csv
Reply all
Reply to author
Forward
0 new messages