473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

Lesson 2 – getting started with r (console and expressions)

nbiswas
149 100+
R scripts can be executed within the R Console or outside of the R Console as batch operations.
We will examine some simple R Expressions and learn how to obtain help along the way.
The starting point for every R user is the R Console.
The R Console can be used to issue interactive R functions or expressions. The greater than (>) symbol is the default prompt for R.
When an R expression is complete, within the R Console, it can be executed.
If the expression is not complete, the R Console prompt will change to the plus (+) symbol.
Each R session has a working directory.
A working directory contains a workspace file, named .Rdata, which contains a list of R objects.
R will also keep file with the history of previously issues R commands.
The R working directory can be changed using the setwd() function.
To determine the current working directory simply issue the getwd() function.
When you complete a session of using the R Console you will be prompted to consider saving your workspace. You may want to save your workspace so you can return to your analysis at a later time, but it is always a good practice to create R scripts from your R Console sessions so you do not accidently overwrite your workspace file.
Executing R scripts outside of the R Console can help automate your data analysis activity.
Let's consider a scenario where you have created an R script that performs data analysis and generates a set of data plots as PDF files. It would be desirable to automate this task and avoid using the R Console altogether.
There are a few different methods of executing R scripts in a batch mode.
The command R CMD BATCH can be used across all platforms.
There is also a tool called Rscript that can be used across all platforms to run scripts outside of the console. This tool gives you the ability to obtain input from the user as the script executes, if required.

On Linux, R script files can be changed into an executable script by simply adding a reference to the Rscript executable in the first line of the script file. Be sure to change the file permissions of your script to be executable if you decide to use this approach.
By default, there is an output file created for any information sent to standard output.
The name of the output file is the same name as the script with the extension .Rout.
Let's consider using R as a simple calculator. R code consists of a series of expressions.
These expressions can be generally classified as: data, computation, or control flow operations.
In this first example we are storing the value 5 into an object or variable called 'a'. The data type of object is automatically determined by R.
It is possible to explicitly state the data type of a value, but for now we will let R decide. The recommended assignment operator in R is the Less Than sign (<-) followed by a dash.
A single equals sign (=) can be used, but it not recommended. The addition operator is a built-in function in R and here we see the result of adding the two numbers together
The output is displayed in the R Console, but it has not been assigned to a object and therefore it is no longer accessible. The ls(), or List objects, function is useful while using the R Console as it provides a displayed list of all of the currently active objects.
You may have noticed that the output displayed contained the value 1 inside of square brackets.
This is how R indicates that the data is the first of possibly many items.
There are various other R functions that can be used to describe an R object.
For example, here we are interested in the data type of the object 'a' and we discover that R decided that the datatype for 'a' is a double precision floating point number.
As we will learn later in the course, it is possible to ensure that the values stored in the object 'a' are limited to integers only.
As we become more familiar with R, it is important to know where to go for help.
The help.start() function will launch a local web server with links to various R reference materials.
If you know the name of an R function, but you can't remember its input arguments or its output, use the help() function or use a single question mark to obtain more information.
A set of two question marks (??) followed by a search term can used to perform a search across all of the installed R packages on your system.
Many functions have built-in examples available to help you understand how to use them. The example() function can be invoked to access these built-in samples.
When you are ready to terminate a session within the R Console, use the q() or quit() function.
R has excellent support for creating high quality graphics with minimal effort.
Here are a few simple examples.
First, we have a scatterplot showing the trends in naming females born in Ontario, Canada from 1917 through 2010. The name 'Emma' was selected for this plot.
The next example is a box plot showing the fuel consumption of cars available in Canada
in 2013. The data has been categorized by the number of cylinders the vehicle has.
The final plot is of a simple pie-chart showing the distribution of 4, 6, and 8 cylinder cars available in Canada in 2013.
We will learn how to create these and other plots in future lessons.
Sep 4 '14 #1
1 4967
zmbd
5,501 Expert Mod 4TB
nbiswas:
Please provide proper citations for these articles.
-z
Sep 29 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: soni29 | last post by:
Hi, I'm going to be starting a project with a friend of mine in C#, we're doing it to learn the language a little better, to get more experience with it. We've already come up with an idea of the...
84
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
6
by: Jack Duijf | last post by:
Hello, I am looking for a person in The Netherlands that is willing to help me getting started with Vb.net. Please send a message to jack@aicn.nl if you can help me getting started with the...
6
by: nibiery | last post by:
I am just getting started with Python, and I think I may be thinking about it wrong. I'd like to be able to work interactively with some code that I've got in a file. The only interpreted language...
1
by: =?Utf-8?B?Q29kZVNsaW5nZXI=?= | last post by:
I plan to build my own 2008 Server/Hyper-V system and will not be using one of the tested Dell or HP systems from the release notes and could use some pointers as to my assumnptions and answers to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.