472,985 Members | 2,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,985 developers and data experts.

Lesson 2 getting started with r (console and expressions)

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 4858
5,501 Expert Mod 4TB
Please provide proper citations for these articles.
Sep 29 '14 #2

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

Similar topics

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...
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: ...
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...
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...
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...
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.