[作业问题]
我正在创建一个S3构造函数/对象/脚本。我的脚本中有一个列表,需要打印如下:
ID: 9876 Name: Virgil Gender: M
LDL: 248* HDL: 45+ Triglycerides: 148
我之前定义了一个打印功能。
print.ChlorReads <- function(theObject) {
cat("ID: ", theObject$id, " Name: ", theObject$name, " Gender: ", theObject$gender,
"\n", "LDL: ", theObject$ldl, " HDL: ", theObject$hdl, " Triglycerides: ", theObject$trigl)
}
如何正确使用该脚本,以便以上面指定的格式打印列表中的每个项目?
我可以通过print(lst)来打印列表,但是,列表看起来像这样:
[[1]]
$id
[1] 1111
$name
[1] "Charlene"
$gender
[1] "F"
$ldl
[1] 111
$hdl
[1] 81
$trigl
[1] 136
attr(,"class")
[1] "Patient"
[[2]]
$id
[1] 2222
$name
[1] "Charles"
$gender
[1] "M"
$ldl
[1] 141
$hdl
[1] 78
$trigl
[1] 123
attr(,"class")
[1] "Patient"
[[3]]
$id
[1] 3333
$name
[1] "Kate"
$gender
[1] "F"
$ldl
[1] 121
$hdl
[1] 63
$trigl
[1] 141
attr(,"class")
[1] "Patient"
[[4]]
$id
[1] 4444
$name
[1] "Alice"
$gender
[1] "F"
$ldl
[1] 97
$hdl
[1] 79
$trigl
[1] 111
attr(,"class")
[1] "Patient"
[[5]]
$id
[1] 5555
$name
[1] "Jason"
$gender
[1] "M"
$ldl
[1] 121
$hdl
[1] 85
$trigl
[1] 123
attr(,"class")
[1] "Patient"
[[6]]
$id
[1] 6666
$name
[1] "Spencer"
$gender
[1] "M"
$ldl
[1] 111
$hdl
[1] 81
$trigl
[1] 127
attr(,"class")
[1] "Patient"
[[7]]
$id
[1] 7777
$name
[1] "Mary"
$gender
[1] "F"
$ldl
[1] 108
$hdl
[1] 86
$trigl
[1] 120
attr(,"class")
[1] "Patient"
[[8]]
$id
[1] 8888
$name
[1] "Jarrod"
$gender
[1] "M"
$ldl
[1] 252
$hdl
[1] 38
$trigl
[1] 411
attr(,"class")
[1] "Patient"
[[9]]
$id
[1] 9999
$name
[1] "Cary"
$gender
[1] "M"
$ldl
[1] 121
$hdl
[1] 47
$trigl
[1] 139
attr(,"class")
[1] "Patient"
[[10]]
$id
[1] 1234
$name
[1] "Michael"
$gender
[1] "M"
$ldl
[1] 123
$hdl
[1] 49
$trigl
[1] 134
attr(,"class")
[1] "Patient"
[[11]]
$id
[1] 2345
$name
[1] "Charlotte"
$gender
[1] "F"
$ldl
[1] 135
$hdl
[1] 69
$trigl
[1] 361
attr(,"class")
[1] "Patient"
[[12]]
$id
[1] 3456
$name
[1] "Blake"
$gender
[1] "F"
$ldl
[1] 95
$hdl
[1] 57
$trigl
[1] 126
attr(,"class")
[1] "Patient"
编辑:这里是dput(lst)的输出,正如评论中所要求的那样:
list(structure(list(id = 1111L, name = "Charlene", gender = "F",
ldl = 111L, hdl = 81L, trigl = 136L), .Names = c("id", "name",
"gender", "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
id = 2222L, name = "Charles", gender = "M", ldl = 141L, hdl = 78L,
trigl = 123L), .Names = c("id", "name", "gender", "ldl",
"hdl", "trigl"), class = "Patient"), structure(list(id = 3333L,
name = "Kate", gender = "F", ldl = 121L, hdl = 63L, trigl = 141L), .Names = c("id",
"name", "gender", "ldl", "hdl", "trigl"), class = "Patient"),
structure(list(id = 4444L, name = "Alice", gender = "F",
ldl = 97L, hdl = 79L, trigl = 111L), .Names = c("id",
"name", "gender", "ldl", "hdl", "trigl"), class = "Patient"),
structure(list(id = 5555L, name = "Jason", gender = "M",
ldl = 121L, hdl = 85L, trigl = 123L), .Names = c("id",
"name", "gender", "ldl", "hdl", "trigl"), class = "Patient"),
structure(list(id = 6666L, name = "Spencer", gender = "M",
ldl = 111L, hdl = 81L, trigl = 127L), .Names = c("id",
"name", "gender", "ldl", "hdl", "trigl"), class = "Patient"),
structure(list(id = 7777L, name = "Mary", gender = "F", ldl = 108L,
hdl = 86L, trigl = 120L), .Names = c("id", "name", "gender",
"ldl", "hdl", "trigl"), class = "Patient"), structure(list(
id = 8888L, name = "Jarrod", gender = "M", ldl = 252L,
hdl = 38L, trigl = 411L), .Names = c("id", "name", "gender",
"ldl", "hdl", "trigl"), class = "Patient"), structure(list(
id = 9999L, name = "Cary", gender = "M", ldl = 121L,
hdl = 47L, trigl = 139L), .Names = c("id", "name", "gender",
"ldl", "hdl", "trigl"), class = "Patient"), structure(list(
id = 1234L, name = "Michael", gender = "M", ldl = 123L,
hdl = 49L, trigl = 134L), .Names = c("id", "name", "gender",
"ldl", "hdl", "trigl"), class = "Patient"), structure(list(
id = 2345L, name = "Charlotte", gender = "F", ldl = 135L,
hdl = 69L, trigl = 361L), .Names = c("id", "name", "gender",
"ldl", "hdl", "trigl"), class = "Patient"), structure(list(
id = 3456L, name = "Blake", gender = "F", ldl = 95L,
hdl = 57L, trigl = 126L), .Names = c("id", "name", "gender",
"ldl", "hdl", "trigl"), class = "Patient"))
答案 0 :(得分:2)
您可能需要循环遍历作为参数传递的列表的长度。
尝试这个小修改:
print.ChlorReads <- function(theObject) {
for (i in 1:length(theObject)){
cat("List entry: ", i, "\nID: ", theObject[[i]]$id, " Name: ", theObject[[i]]$name, " Gender: ", theObject[[i]]$gender,
"\n", "LDL: ", theObject[[i]]$ldl, " HDL: ", theObject[[i]]$hdl, " Triglycerides: ", theObject[[i]]$trigl,"\n")
}
}
答案 1 :(得分:1)
鉴于带有list
条目的patient
的示例,我们应该使用两种不同的打印方法。一种处理list
结构的打印方法,通过将每个元素打印命令分配给处理patient
类的另一个元素。
要符合print.*()
S3方法 - a.k.a对包提交传递CRAN检查 - 您必须将对象的名称设为x
,然后...
# Prints the patient information (one item in the list)
print.Patient = function(x, ...){
cat("ID: ", x$id, " Name: ", x$name, " Gender: ", x$gender,
"\nLDL: ", x$ldl, " HDL: ", x$hdl, " Triglycerides: ", x$trigl,"\n", sep="")
}
# Print function defined for list type
print.ChlorReads = function(x, ...) {
# Process each item in the list
for(i in seq_along(x)){
# Call the method to print patient info
print.Patient(x[[i]])
}
}
现在,您需要做的下一步是将lst
的类定义为ChlorReads
:
class(lst) = c('ChlorReads', class(lst))
通过class(lst)
包含当前类可以回退到通用S3支持的类型。
因此,在找到print
语句和开场帖中给出的上述样本对象后,我得到了所需的结果:
class(lst) = c('ChlorReads', class(lst))
print(lst)
这给出了:
ID: 1111 Name: Charlene Gender: F
LDL: 111 HDL: 81 Triglycerides: 136
ID: 2222 Name: Charles Gender: M
LDL: 141 HDL: 78 Triglycerides: 123
ID: 3333 Name: Kate Gender: F
LDL: 121 HDL: 63 Triglycerides: 141
ID: 4444 Name: Alice Gender: F
LDL: 97 HDL: 79 Triglycerides: 111
ID: 5555 Name: Jason Gender: M
LDL: 121 HDL: 85 Triglycerides: 123
ID: 6666 Name: Spencer Gender: M
LDL: 111 HDL: 81 Triglycerides: 127
ID: 7777 Name: Mary Gender: F
LDL: 108 HDL: 86 Triglycerides: 120
ID: 8888 Name: Jarrod Gender: M
LDL: 252 HDL: 38 Triglycerides: 411
ID: 9999 Name: Cary Gender: M
LDL: 121 HDL: 47 Triglycerides: 139
ID: 1234 Name: Michael Gender: M
LDL: 123 HDL: 49 Triglycerides: 134
ID: 2345 Name: Charlotte Gender: F
LDL: 135 HDL: 69 Triglycerides: 361
ID: 3456 Name: Blake Gender: F
LDL: 95 HDL: 57 Triglycerides: 126