30 November 2016

The best ways to run commands in R with tips

#How to Run-commands in R:
#How to Run-commands in R:
The next step after installing R is how to run commands. You can run directly by entering commands. The other way is you need to write a R script, that contains all the series of commands. The benefit of script is you can save your commands, it saves your time. Second as a script, you can run it whenever you need.

Entering Commands
Commands can be entered directly into the R console (the window that opens when you start R),
following the red > prompt, and sent to the computer by pressing enter. For example, typing 1 + 2 and pressing enter will output the result 3:
> 1+2
[1] 3
Your entered code always follows the > prompt, and output always follows a number in square
brackets.

  • Each command should take its own line of code, or else a line of code should be continued with { } 
  • It is possible to press enter before the line of code is completed, and often R will recognize this. For example, if you were to type 1 + but then press enter before typing 2, R knows that 1+ by itself doesn’t make any sense, so prompts for you to continue the line with a + sign. At this point you could continue the line by pressing 2 then enter. This commonly occurs if you forget to close parentheses or brackets. 
  • If you keep pressing enter and keep seeing a + sign rather than the regular > prompt that allows you to type new code, and if you can’t figure out why, often the easiest option is to simply press ESC, which will get you back to the normal > prompt and allow you to enter a new line of code. 
Capitalization and punctuation need to be exact in R, but spacing doesn’t matter. If you get errors when entering code, you may want to check for these common mistakes:

- Did you start your line of code with a fresh prompt (>)? If not, press ESC.
- Are your capitalization and punctuation correct?
- Are all your parentheses and brackets closed? For every forward (, {, or [, make sure there is a corresponding backwards ), }, or ]

R Script
Rather than entering commands into the console directly however, we recommend creating and using an R Script, basically a text editor for your code. A new script can be created by File -> New Script.

Code (commands) can be typed here, and then entered into the console in one of three ways:

1) Copy the code in the R script and paste in the console
2) Right-click on a line or highlighted group of lines and choose “Run line or selection”
3) Place your cursor on a line or highlight a group of lines and press CTRL+R.

Using a separate R script is nice because you can save only the code that works, making it easy to rerun and edit in the future, as opposed to the R console in which you would also have to save all your mistakes and all the output.

We recommend always saving your R Scripts so you have the commands easily accessible and editable for future use.

No comments:

Post a Comment

Thanks for your message. We will get back you.

© 2010-2017 Biganalytics.me. All rights reserved.. Powered by Blogger.

Total Pageviews

All material, files, logos and trademarks within this site are properties of their respective organizations.