0

I have a data frame x and subject function.

DATA

dput(head(x))
structure(list(subjects = c(14L, 14L, 14L, 14L, 14L, 14L), 
visit =  structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1", "2"), class = "factor"),
room = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("bedroom", 
"den", "dining room", "family  room", "hall", "kitchen", 
"living room", "office", "study room", "tv room"), class = "factor"), 
value = c(6, 6, 2.75, 2.75, 2.75, 2.75), timepoint = 53:58),
row.names = c(NA, 6L), class = c("LongitudinalData", "data.frame"))

Function

subject<-function(x,id) UseMethod("subject")
subject.LongitudinalData<- function(x ,id) { 
    a<- x[x$subjects==id,]
    b<-noquote(paste("Subject ID:",id))
    out<-list(a,b)
    class(out)<-"subject"
    invisible(out)
}

Now i want to get following print resualt (Diserable outpout, not actual):

out<-subject(x,14)
print(out)
Subject ID: 14
And desirable str output would be like below (A data frame not a list)
str(out)
Classes ‘LongitudinalData’ and 'data.frame':    10 obs. of  5 variables:
$ subjects : int  14 14 14 14 14 14 14 14 14 14 ...
$ visit    : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
$ room     : Factor w/ 10 levels "bedroom","den",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value    : num  6 6 2.75 2.75 2.75 2.75 6 6 2.75 2.75 ...
$ timepoint: int  53 54 55 56 57 58 59 60 61 62 ...

Not working proposed solution and its output

print.subject  <- function(x) {  
x[[2]]
  }
str(out)
List of 2
 $ :Classes ‘LongitudinalData’ and 'data.frame':    6 obs. of  5 variables:
  ..$ subjects : int [1:6] 14 14 14 14 14 14
  ..$ visit    : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1
  ..$ room     : Factor w/ 10 levels "bedroom","den",..: 1 1 1 1 1 1
  ..$ value    : num [1:6] 6 6 2.75 2.75 2.75 2.75
  ..$ timepoint: int [1:6] 53 54 55 56 57 58
 $ : 'noquote' chr "Subject ID: 14"
 - attr(*, "class")= chr "subject"

I do not want a list of two elements but a data frame

Any idea?

2
  • You are getting it as a list, print(out[[2]])# [1] Subject ID: 14 Commented Dec 12, 2018 at 18:56
  • I changed the post Commented Dec 12, 2018 at 19:02

2 Answers 2

1

Why not the following?

print.subject = function (x) {
    cat(x[[2]], '\n')
    invisible(x)
}

Two things to note:

  1. NextMethod is generally a good idea but the above is simple enough and robust. By contrast, I’m not even exactly sure what your solution does.
  2. You need to use [[, not [ subsetting, to access the correct list element (rather than a list slice).
Sign up to request clarification or add additional context in comments.

3 Comments

I am seeking a way to have a function that returns only a data frame, but when I print it, it gives me like "Subject Id : * "
@imi Yes. That is what my answer does.
my problem is the str(output) part, I want a data frame, not a list of two items
0

No need to save output in a list at first place

subject<-function(x,id) UseMethod("subject")
subject.LongitudinalData<- function(x ,id) { 
   a <- x[x$subjects==id,]
   class(a)<-c("subject","LongitudinalData","data.frame")
   invisible(a)
}


print.subject  <- function(x) {
  if(length(unique(x$subjects)) == 0) {
    noquote(paste("NULL"))
  } else {
      noquote(paste("Subject ID:",unique(x$subjects) ))
      }

}

output

> print(out)
[1] Subject ID: 14
> str(out)
Classes ‘subject’, ‘LongitudinalData’ and 'data.frame': 11945 obs. of  5 variables:
 $ subjects : int  14 14 14 14 14 14 14 14 14 14 ...
 $ visit    : Factor w/ 3 levels "0","1","2": 1 1 1 1 1 1 1 1 1 1 ...
 $ room     : Factor w/ 10 levels "bedroom","den",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value    : num  6 6 2.75 2.75 2.75 2.75 6 6 2.75 2.75 ...
 $ timepoint: int  53 54 55 56 57 58 59 60 61 62 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.