Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do printf in r?

Tags:

r

I'm looking for how to do printf in r, ie I want to type:

printf("hello %d\n", 56 ) 

and get the same output as typing:

print(sprintf("hello %d\n", 56 ) 

I've read the following links:

  • Printing newlines with print() in R
  • what's a prettier way to print info with R?

... so I'm aware I could use cat("hello", 56) , and that's probably what I will do, but just wondering if there is some shortcut way of writing print(sprintf(...))?

Sorry if this question is a duplicate (I dont know). It's awfully hard to search for 'printf r', since it returns results for php, c, ...

like image 513
Hugh Perkins Avatar asked Oct 23 '12 03:10

Hugh Perkins


People also ask

How do you print in R?

Print output using paste() function inside print() function R provides a method paste() to print output with string and variable together. This method defined inside the print() function. paste() converts its arguments to character strings. One can also use paste0() method.

How do I print on the same line in R?

You can use the cat() function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat(variable1, variable2, variable3, ...) The following examples show how to use this syntax in different scenarios.

Can we use %d in printf?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


1 Answers

printf <- function(...) invisible(print(sprintf(...))) 

The outer invisible call may be unnecessary, I'm not 100% clear on how that works.

like image 92
zwol Avatar answered Sep 19 '22 12:09

zwol