Reputation: 2703
I made a custom method class for the print
function, now I'm happy with the output except for a minor detail. How do I change the output numbering?
I've marked what I mean below, they all start by [1]
now and I would like them to increment by 1.
Output
> 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]
Code
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))
}
Upvotes: 1
Views: 115
Reputation: 206242
That [1]
comes from R printing a vector. Each line beings with the index of the first element of that line. If you want the index to increase, you need to print the whole vector at once. You can skip the loop and just do
print(paste("#Graph:", names(x)[-1]))
However R may choose to print two messages on the same line if the names are short enough so this might not be ideal. If that indexing is important to you, you should take control of it rather than relying on side effects of default print methods. You could do something like
cat(paste0("[", seq_along(x[-1]), "] #Graph: ", names(x)[-1],"\n"), sep="")
Upvotes: 1