如何编号自定义方法类打印功能输出

时间:2015-08-03 15:34:40

标签: r

我为print函数创建了一个自定义方法类,现在我对输出感到满意,除了细节之外。如何更改输出编号?

我已在下面标明了我的意思,它们现在都以[1]开头,我希望它们增加1.

输出

> overview(x)
[1] "WARNIG: Outputted table except [observations] represent MEAN values"
$`Overview - Data Frame`
   areaSize listPrice soldPrice priceDiff  rent livingArea constructionYear observations
1         0    500000    600000    100000   273        1.0             1988            1
2        10   1408930   1630412    221482  1130       17.9             1930          170
3        20   1783503   2033009    248614  1503       25.6             1936         2463
4        30   2127803   2384254    256321  1985       35.2             1937         5191
5        40   2325672   2569010    241996  2478       44.0             1943         6429
6        50   2539673   2773736    232958  2985       54.8             1948         6417
7        60   3014995   3270410    252820  3345       64.5             1954         5006
8        70   3190774   3456121    259276  3827       74.3             1960         4565
9        80   3795549   4070094    269575  4249       84.1             1967         3097
10       90   4393370   4695931    303045  4704       94.0             1967         1707
11      100   4992324   5292759    294808  4916      103.9             1963         1083
12      110   5853922   6184401    327407  5110      114.2             1955          578
13      120   6485117   6714685    232000  5367      123.8             1953          302
14      130   7530543   7805881    281549  5400      134.4             1948          177
15      140   8069008   8406025    314545  5661      144.0             1940          122
16      150   9190260   9386167    176247  5842      154.0             1942           78
17      160  10796875  10939250    142375  6259      163.3             1920           40
18      170  10004038  10777407    735962  6148      174.0             1915           27
19      180  13643235  13910294    267059  6396      183.9             1903           17
20      190  11376429  11792667    508571  6046      194.4             1898           15
21      200  12220417  12095833   -124583  7167      203.8             1922           12
22      210  15231250  15825000    593750  6448      215.0             1921            4
23      220  16120000  16158333   -580000  7125      224.7             1897            6
24      230  19800000  19162500    733333  7422      233.2             1909            4
25      250  22750000  25750000   3000000   NaN      255.0             1912            2
26      260  29500000  22000000  -7500000  7932      260.0             1888            1
27      270  21000000  19750000  -1250000 10602      277.0              NaN            1
28      280  22950000  21500000  -1450000  3721      287.5             1905            2
29      300       NaN  27000000       NaN  7990      308.0             1907            1
30      330  24000000  20500000  -3500000 12415      332.0             1912            1

[1] "#Graph: Difference in sold-price between categories of area-size" #They all start by [1]
[1] "#Graph: Difference in list price between categories of area-size" #They all start by [1]
[1] "#Graph: Number of observations per group of area-size" #They all start by [1]

代码

print.overview  <- function(x) { 

  print("WARNIG: Outputted table except [observations] represent MEAN values")
  print(x[1])
  lapply(x[-1], print)

  for (name in names(x)[-1]) print(paste("#Graph:", name))

} 

1 个答案:

答案 0 :(得分:1)

[1]来自R打印矢量。每条线都具有该线的第一个元素的索引。如果希望索引增加,则需要立即打印整个矢量。您可以跳过循环,然后执行

print(paste("#Graph:", names(x)[-1]))

但是,如果名称足够短,R可能会选择在同一行打印两条消息,因此这可能不太理想。如果索引对您很重要,则应该控制它而不是依赖于默认打印方法的副作用。你可以做点什么

cat(paste0("[", seq_along(x[-1]), "] #Graph: ", names(x)[-1],"\n"), sep="")