10

How to print a long list without indexes ?

The output I get :

> print(1:100)
 [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
 [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
 [91]  91  92  93  94  95  96  97  98  99 100

Desired output :

> print(1:100)
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
 37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
 55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
 73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
 91  92  93  94  95  96  97  98  99 100

I tried with cat() function but it doesn't do line returns.

Thanks in advance.

1
  • see my edit. doesn't give what I want Jun 16, 2015 at 14:47

3 Answers 3

11

A neat solution is cat but using its fill argument, in combination with the format() function.

cat(format(1:100), fill = getOption("width"))

which gives

> cat(format(1:100), fill = getOption("width"))
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
 21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
 41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
 61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
 81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100

I haven't tried this with all kinds of vectors to know if the filling/splitting is infallible but it might be sufficient for your needs if alignment is important.

The advantage of using format() here (rather than paste()) is that format() pads the individual strings to a common length

> format(1:10)
 [1] " 1" " 2" " 3" " 4" " 5" " 6" " 7" " 8" " 9" "10"

If you just fill the catted numeric vector or a pasted representation, the alignment is still off:

> cat(1:100, fill = getOption("width"))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
> cat(paste(1:100), fill = getOption("width"))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
10

Try the option

 fill =TRUE

with

 cat()

Here is an example:

 > cat(paste(1:100), fill=TRUE)

It will break lines depending on the width of your screen.

1
4

Consider using writeLines() and strwrap() on a suitably composed string:

writeLines(strwrap(paste(1:100, collapse = " ")))

which gives

> writeLines(strwrap(paste(1:100, collapse = " ")))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
100

It's not quite what you want, but close. You would need to format each of the individual character elements to get the padding correct.

Part of the problem is that strwrap() destroys all whitespace...

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.