Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output in R, Avoid Writing "[1]"

Tags:

r

I use print to output from a function in R, for example:

print("blah blah blah") 

This outputs

[1] "blah blah blah" 

To the console. How can I avoid the [1] and the quotes?

like image 225
John Montague Avatar asked Feb 04 '12 02:02

John Montague


People also ask

What does 1 mean in R output?

It means that [1] represents the first value returned by the function called? – Shaun. Jun 2, 2015 at 11:48. yes it is indicating that only try list(s=c(1:2),b=c(2:5)) you will get two [1] since two vectors are being printed.

How do you stop input in R?

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using. However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.

How do you write output in R?

Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output. To do this just select the output variable and press run button.


2 Answers

Use cat("Your string") (type ?cat to see help page) to output concatenated objects to standard output.

like image 177
Alex Reynolds Avatar answered Sep 22 '22 04:09

Alex Reynolds


message is probably the best function to replace print for your needs. cat is also a good function to look at but message will print a new line for you as well. It is also better to use since it is easier to suppress the output from message than it is to suppress the output from cat.

If you just want to remove the quotes but don't mind the [1] printing then you could use the quote=FALSE option of print.


Edit: As noted in the comments, message isn't the same as a call to print as it sends the output to a different connection. Using cat will do what you want as others have noted but you'll probably want to add a new line on after your message.

Example

cat("This is a message\n") # You'll want to add \n at the end 
like image 45
Dason Avatar answered Sep 20 '22 04:09

Dason