[R] Create a variable lenght string that can be used in a dimnames statement

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Tue Jul 4 09:32:53 CEST 2023


(Sorry for the double post.)

On Tue, 4 Jul 2023 10:14:43 +0300
Ivan Krylov <krylov.r00t using gmail.com> wrote:

> Try replacing the _second_ paste() in the example above with a c().

What I had forgotten to mention is that you also need to replace the
initial assignment

>  string=""

with the following:

   string = character()

(NULL and c(), as mentioned by Rui, are also valid options here.)

If you don't do that, the empty string remains an element in the
resulting vector c('', 'xxx1', 'xxx2'), which is not the desired
result. It's awkward to build data structures in an iterative manner,
which is why solutions like Rui's paste0("xxx", 1:2) are considered
more idiomatic.

-- 
Best regards,
Ivan



More information about the R-help mailing list