'"%s" random concatenation in R

I am looking for a way to concatenate a string or a number (3 digits at least) into a save file. For instance, in python I can use '%s' % [format(str), format(number)]) and add it to csv file with a random generator.

How do I generate a random number into a format in R? That is my save file and I want to add a random string or a number in the end of the file name:

file = paste(path, 'group1N[ADD FORMAT HERE].csv',sep = '')

file = paste(path, 'group1N.csv',sep = '') to become -- >

file = paste(path, 'group1N212.csv',sep = '') or file = paste(path, 'group1Nkut.csv',sep = '') 

after using a random generator of strings or numbers and appending it to the save .csv file, each time it is saved, as a random generated end of file



Solution 1:[1]

You could use the built-in tempfile() function:

 tempfile(pattern="group1N", tmpdir=".", fileext=".csv")
[1] "./group1N189d494eaaf2ea.csv"

(if you don't specify tmpdir the results go to a session-specific temporary directory).

This won't write over existing files; given that there are 14 hex digits in the random component, I think the "very likely to be unique" in the description is an understatement ... (i.e. at a rough guess the probability of collision might be something like 16^(-14) ...)

The names are very likely to be unique among calls to ‘tempfile’ in an R session and across simultaneous R sessions (unless ‘tmpdir’ is specified). The filenames are guaranteed not to be currently in use.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1