Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a long list without indexes?

Tags:

r

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.

like image 859
user2682877 Avatar asked Jun 16 '15 14:06

user2682877


People also ask

How do I print a nested list without brackets in Python?

There are three methods to print a list without square brackets in Python Programming. Use for loop. Use join() Function. Use asterick operator.

How do I print individual items in a list Python?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.


2 Answers

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
like image 143
Gavin Simpson Avatar answered Oct 30 '22 08:10

Gavin Simpson


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.

like image 26
erasmortg Avatar answered Oct 30 '22 09:10

erasmortg