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
list,print(out[[2]])# [1] Subject ID: 14