c() returns weird results

I'm new to R and i'm trying to read some files with an annotation string for each filename. Using the c() function to build a list of annotated filenames i get inconsistent results, as in the following example. Why does c() result in two different types when using similar strings?

> print(c("_latency_measurements_2022-10-28-02-18-delay35.txt", "35ms"))
[1] "_latency_measurements_2022-10-28-02-18-delay35.txt"
[2] "35ms"                                              
> print(c("_latency_measurements_2022-10-28-02-18-delay3.txt", "35ms"))
[1] "_latency_measurements_2022-10-28-02-18-delay3.txt" "35ms"  

Hi welcome to the forum.

I do not sure what the problem is but you have one extra character in the first vector. delay35 versus delay3

Otherwise we need a bit more information

See FAQ: How to do a minimal reproducible example ( reprex ) for beginners

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

As @jrkrideau statet, those strings are not identical. The latter is shorter, hence all character symbols can go on one line. If both strings are too long to go on one line, there will simply be another line. It is not inconsistent at all.

> 1:50
 [1]  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] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
> 1:50
 [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

in the first 1:50 I had the Rstudio console pane wider, and for the second i narrowed it; since R had to print on more rows the numbering it shows as to what number element begins the line necessarily changed.
As you can see; c() wasnt involved in this; theres a more fundamental issue of how R prints vectors to the console.

All good eh?

Oh man this is quite embarrassing. Thanks for the explanation! I thought print() putting two lines [1], [2] instead of only one means the underlying vector has different dimensions. That really confused me.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.