Thursday, December 20, 2012

R OVERVIEW

R OVERVIEW



REVISED: Saturday, March 2, 2013




R

I.  R WORKING DIRECTORY

Double left mouse click your "R icon" on your desktop to start R. Your “R Gui” and your “R Console” windows will open. If you want to "clear your R Console", you can either go to the "tool bar" across the top of the screen and left mouse click "Edit" and then left mouse click "Clear Console", or you can "hold down Ctrl and press the lower case letter l (el)". At the user prompt on the R Console type the R function getwd( ) and press Enter to see your "current working directory" (CWD). This is the directory R uses to write and read files to and from your computer. If you want to change to a different working directory left mouse click "File", then left mouse click “Change dir…” and select the working directory you prefer. Type the R function getwd( ) again and press Enter to ensure the directory properly changed.

If you are using Windows, like I am, R gets confused if you use a path in your code using a single "back slash" \; e.g.:

c:\mydocuments\myfile.txt

R sees a single "\" as an escape character. Instead of a single "back slash", use a double "back slash":

c:\\my documents\\myfile.txt

or use a single "forward slash":

c:/mydocuments/myfile.txt


The setwd( ) function can be used to select a different working directory, and avoid entering the full path of the data files.

> setwd(" ")  sets the working directory to " ".

Even on a Windows platform, the forward slash should be used as the path separator.

> setwd("C:/MyRwd")  sets the working directory to "C:\MyRwd".

Use the list.files( ) function to see what files are in the current working directory.

> q( )

You can use the function q( ) to quit R. You will be prompted to save the workspace.

II. R SEMICOLON

The R semicolon (;) can be used to have more than one expression on a line. Each expression on the same line must be separated by a semicolon.

III.  R PACKAGES

To load a package type package(packagename). You can use the data( ) function to see which data sets are currently loaded for you to use for demonstrations or just to play around with.

The search( ) function tells you the packages currently available for use.

> search(  )
 [1] ".GlobalEnv"          "package:stringr"     "tools:rstudio"    
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "package:methods"  
[10] "Autoloads"         "package:base"     
>

IV.  R SESSION

R is used as an interactive program.

The assignment operator "<-" also called gets; works in both directions "->" accomplishing the same task in either direction.

The # character indicates the beginning of a comment. Everything to the right of the #, including the # itself, is ignored.

All of a R session occurs in memory.

The ls( ) and objects( ) functions will show you the names of the objects (variables) in the global environment of your current session. 

The search( ) function returns the packages and data sets that are in your work space. R assigns everything to a vector by default. When you do a function call, R looks in the files for the function called in the order the files are listed, in the vector returned by search( ). The sequence of the packages listed by search( ) has meaning. ".GlobalEnv" is always listed in  position [1] in the vector. (The [1] is an index of the vector.)

To use a package, you need to attach it in the session:

> require(packagename)

At the start of each session where you want to use a particular package you are required to invoke the require command and use the package as an argument.

Enjoy R!


Elcric Otto Circle







-->




-->




-->














How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment