r code for assignment

Secure Your Spot in Our Statistical Methods in R Online Course Starting on September 9 (Click for More Info)

Joachim Schork Image Course

Assignment Operators in R (3 Examples) | Comparing = vs. <- vs. <<-

On this page you’ll learn how to apply the different assignment operators in the R programming language .

The content of the article is structured as follows:

Let’s dive right into the exemplifying R syntax!

Example 1: Why You Should Use <- Instead of = in R

Generally speaking, there is a preference in the R programming community to use an arrow (i.e. <-) instead of an equal sign (i.e. =) for assignment.

In my opinion, it makes a lot of sense to stick to this convention to produce scripts that are easy to read for other R programmers.

However, you should also take care about the spacing when assigning in R. False spacing can even lead to error messages .

For instance, the following R code checks whether x is smaller than minus five due to the false blank between < and -:

A properly working assignment could look as follows:

However, this code is hard to read, since the missing space makes it difficult to differentiate between the different symbols and numbers.

In my opinion, the best way to assign in R is to put a blank before and after the assignment arrow:

As mentioned before, the difference between <- and = is mainly due to programming style . However, the following R code using an equal sign would also work:

In the following example, I’ll show a situation where <- and = do not lead to the same result. So keep on reading!

Example 2: When <- is Really Different Compared to =

In this Example, I’ll illustrate some substantial differences between assignment arrows and equal signs.

Let’s assume that we want to compute the mean of a vector ranging from 1 to 5. Then, we could use the following R code:

However, if we want to have a look at the vector x that we have used within the mean function, we get an error message:

Let’s compare this to exactly the same R code but with assignment arrow instead of an equal sign:

The output of the mean function is the same. However, the assignment arrow also stored the values in a new data object x:

This example shows a meaningful difference between = and <-. While the equal sign doesn’t store the used values outside of a function, the assignment arrow saves them in a new data object that can be used outside the function.

Example 3: The Difference Between <- and <<-

So far, we have only compared <- and =. However, there is another assignment method we have to discuss: The double assignment arrow <<- (also called scoping assignment).

The following code illustrates the difference between <- and <<- in R. This difference mainly gets visible when applying user-defined functions .

Let’s manually create a function that contains a single assignment arrow:

Now, let’s apply this function in R:

The data object x_fun1, to which we have assigned the value 5 within the function, does not exist:

Let’s do the same with a double assignment arrow:

Let’s apply the function:

And now let’s return the data object x_fun2:

As you can see based on the previous output of the RStudio console, the assignment via <<- saved the data object in the global environment outside of the user-defined function.

Video & Further Resources

I have recently released a video on my YouTube channel , which explains the R syntax of this tutorial. You can find the video below:

The YouTube video will be added soon.

In addition to the video, I can recommend to have a look at the other articles on this website.

  • R Programming Examples

In summary: You learned on this page how to use assignment operators in the R programming language. If you have further questions, please let me know in the comments.

assignment-operators-in-r How to use different assignment operators in R – 3 R programming examples – R programming language tutorial – Actionable R programming syntax in RStudio

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy .

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Post Comment

Joachim Schork Statistician Programmer

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy .

Related Tutorials

Indent Multiple Lines of Code in RStudio (Example)

Indent Multiple Lines of Code in RStudio (Example)

Apply Function to Each List Element in R (3 Examples)

Apply Function to Each List Element in R (3 Examples)

Assignment Operators in R

R provides two operators for assignment: <- and = .

Understanding their proper use is crucial for writing clear and readable R code.

Using the <- Operator

For assignments.

The <- operator is the preferred choice for assigning values to variables in R.

It clearly distinguishes assignment from argument specification in function calls.

Readability and Tradition

  • This usage aligns with R’s tradition and enhances code readability.

Using the = Operator

The = operator is commonly used to explicitly specify named arguments in function calls.

It helps in distinguishing argument assignment from variable assignment.

Assignment Capability

  • While = can also be used for assignment, this practice is less common and not recommended for clarity.

Mixing Up Operators

Potential confusion.

Using = for general assignments can lead to confusion, especially when reading or debugging code.

Mixing operators inconsistently can obscure the distinction between assignment and function argument specification.

  • In the example above, x = 10 might be mistaken for a function argument rather than an assignment.

Best Practices Recap

Consistency and clarity.

Use <- for variable assignments to maintain consistency and clarity.

Reserve = for specifying named arguments in function calls.

Avoiding Common Mistakes

Be mindful of the context in which you use each operator to prevent misunderstandings.

Consistently using the operators as recommended helps make your code more readable and maintainable.

Quiz: Assignment Operator Best Practices

Which of the following examples demonstrates the recommended use of assignment operators in R?

  • my_var = 5; mean(x = my_var)
  • my_var <- 5; mean(x <- my_var)
  • my_var <- 5; mean(x = my_var)
  • my_var = 5; mean(x <- my_var)
  • The correct answer is 3 . my_var <- 5; mean(x = my_var) correctly uses <- for variable assignment and = for specifying a named argument in a function call.
assignOps {base}R Documentation

Assignment Operators

Description.

Assign a value to a name.

a variable name (possibly quoted).

a value to be assigned to .

There are three different assignment operators: two of them have leftwards and rightwards forms.

The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The operators <<- and ->> are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment. Note that their semantics differ from that in the S language, but are useful in conjunction with the scoping rules of R . See ‘The R Language Definition’ manual for further details and examples.

In all the assignment operator expressions, x can be a name or an expression defining a part of an object to be replaced (e.g., z[[1]] ). A syntactic name does not need to be quoted, though it can be (preferably by backtick s).

The leftwards forms of assignment <- = <<- group right to left, the other from left to right.

value . Thus one can use a <- b <- c <- 6 .

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language . Wadsworth & Brooks/Cole.

Chambers, J. M. (1998) Programming with Data. A Guide to the S Language . Springer (for = ).

assign (and its inverse get ), for “subassignment” such as x[i] <- v , see [<- ; further, environment .

Revolutions

Milestones in ai, machine learning, data science, and visualization with r and python since 2008.

« New Feature: R Community Calendar | Main | Get advance notice of changes to R »

December 16, 2008

Use = or <- for assignment.

A recent thread on the r-help mailing list raises a common question for beginning R users: should you use =  (equals) or <-  (back arrow) for assignments?  In R, both of the following statements have the effect of assigning the value 3 to the variable x:

x = 3 x <- 3

So if they have the same effect, does it matter which you use?

clearly means "assign 3 to x ", whereas

clearly means "call function f , setting the argument x to 3".

f(x <- 3)

which means "assign 3 to x , and call f with the first argument set to the value 3".  This is a contrived example though, and never really occurs in real-world programming.  [UPDATE: In fact, constructs like this are best avoided for reasons given in the comments below.]

Posted by David Smith at 09:44 in beginner tips , R | Permalink

Feed

Duncan Murdoch points out a slight error: f(x <- 3) isn't the only syntax you can use to assign in a function call: f( (x=3) ) would also work. More in this r-help post .

Posted by: David Smith | March 11, 2009 at 09:15

Jens Oehlschlägel makes the point that because of lazy evaluation , f(x <- 3) isn't guaranteed to perform any assignment to x. His (good) advice: "do not assign in function arguments unless you have good reasons".

Posted by: David Smith | March 12, 2009 at 17:03

Using 2 keystrokes for an assignment operator is a pain, although it's nicer to read. Does anyone bother reassigning a key (is there an easy way to do this?) to print out an arrow operator, as per the original APL keyboard?

Posted by: Bulgakov | March 21, 2009 at 18:10

@Bulgakov the ESS package for emacs sets the underscore '_' to emit the arrow (<-).

Also I have a vague recollection that older versions of R allowed '_' to be used in place of the arrow; however this is no longer true.

Posted by: TSM | July 27, 2009 at 13:04

I routinely use Tinn-R's macro recorder to load <- at the start of a session and use <- . Personally I think it makes for easier reading of the code

Posted by: John Kane | September 17, 2009 at 15:10

If you run R as an inferior process to emacs you can get <- with spaces before and after by simply pressing the key _ (underscore). Of course you need the ESS package for emacs.

Posted by: skywalker | September 20, 2009 at 19:46

Thanks for the clarification. I also noticed in the link you shared that <<- was used at some point. I've neither used nor tried that one.

Posted by: isomorphismes | October 20, 2012 at 18:20

R-Studio has Alt- as shortcut key combination for " <- ". Yes, it adds the surrounding spaces!

Posted by: Jim P | January 22, 2013 at 13:06

The vim R package also substitute "_" with " <- " just like the ESS package for Emacs.

http://www.vim.org/scripts/script.php?script_id=2628

Posted by: Grunde | January 28, 2013 at 06:13

Given the absolute noise of programming, the extra keystroke for 'assigns' (that is '<-' vs '=') is no convenience. We are not that close to ultimate efficiency.

Posted by: sean | August 13, 2013 at 15:24

The comments to this entry are closed.

Information

  • About this blog
  • Comments Policy
  • About Categories
  • About the Authors
  • Local R User Group Directory
  • Tips on Starting an R User Group

Search Revolutions Blog

  • academia (41)
  • advanced tips (218)
  • airoundups (20)
  • announcements (201)
  • applications (288)
  • beginner tips (106)
  • big data (272)
  • courses (60)
  • current events (126)
  • data science (227)
  • developer tips (90)
  • events (280)
  • finance (126)
  • government (25)
  • graphics (378)
  • high-performance computing (115)
  • life sciences (35)
  • Microsoft (315)
  • open source (78)
  • other industry (58)
  • packages (388)
  • popularity (54)
  • predictive analytics (163)
  • profiles (15)
  • python (69)
  • R is Hot (8)
  • random (464)
  • reviews (22)
  • Revolution (422)
  • Rmedia (136)
  • roundups (121)
  • sports (55)
  • statistics (297)
  • user groups (127)
  • R on Azure Developer's guide and documentation
  • Find R packages CRAN package directory at MRAN
  • Download Microsoft R Open Free, high-performance R
  • R Project site Information about the R project

Recommended Sites

  • @RLangTip Daily tips on using R
  • FlowingData Modern data visualization
  • Probability and statistics blog Monte Carlo simulations in R
  • R Bloggers Daily news and tutorials about R, contributed by R bloggers worldwide.
  • R Project group on analyticbridge.com Community and discussion forum
  • Statistical Modeling, Causal Inference, and Social Science Andrew Gelman's statistics blog
  • January 2023
  • August 2022
  • September 2021
  • February 2021
  • January 2021

assignOps: Assignment Operators

assignOpsR Documentation

Assignment Operators

Description.

Assign a value to a name.

a variable name (possibly quoted).

a value to be assigned to .

There are three different assignment operators: two of them have leftwards and rightwards forms.

The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The operators <<- and ->> are normally only used in functions, and cause a search to be made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment. Note that their semantics differ from that in the S language, but are useful in conjunction with the scoping rules of R . See ‘The R Language Definition’ manual for further details and examples.

In all the assignment operator expressions, x can be a name or an expression defining a part of an object to be replaced (e.g., z[[1]] ). A syntactic name does not need to be quoted, though it can be (preferably by backticks).

The leftwards forms of assignment <- = <<- group right to left, the other from left to right.

value . Thus one can use a <- b <- c <- 6 .

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language . Wadsworth & Brooks/Cole.

Chambers, J. M. (1998) Programming with Data. A Guide to the S Language . Springer (for = ).

assign (and its inverse get ), for “subassignment” such as x[i] <- v , see [<- ; further, environment .

R Package Documentation

Browse r packages, we want your feedback.

r code for assignment

Add the following code to your website.

REMOVE THIS Copy to clipboard

For more information on customizing the embed code, read Embedding Snippets .

Introduction

  • R installation
  • Working directory
  • Getting help
  • Install packages

Data structures

Data Wrangling

  • Sort and order
  • Merge data frames

Programming

  • Creating functions
  • If else statement
  • apply function
  • sapply function
  • tapply function

Import & export

  • Read TXT files
  • Import CSV files
  • Read Excel files
  • Read SQL databases
  • Export data
  • plot function
  • Scatter plot
  • Density plot
  • Tutorials Introduction Data wrangling Graphics Statistics See all

R operators

Learn all about the R programming language operators

There are several operators in R, such that arithmetic operators for math calculations, logical, relational or assignment operators or even the popular pipe operator. In this tutorial we will show you the R operators divided into operator types. In addition, we will show examples of use of every operator.

Arithmetic operators

The R arithmetic operators allows us to do math operations , like sums, divisions or multiplications, among others. The following table summarizes all base R arithmetic operators.

Arithmetic operator in R Description
+ Plus
Minus
* Multiplication
/ Division
^ Exponential
** Exponential
%% Modulus
%/% Integer divide
%*% Matrix multiplication
%o% Outer product
%x% Kronecker product

In the next block of code you will find examples of basic calculations with arithmetic operations with integers.

You can also use the basic operations with R vectors of the same length . Note that the result of these operations will be a vector with element-wise operation results.

Furthermore, you can use those arithmetic operators with matrix objects, besides the ones designed for this type of object (matrix multiplication types). Check our tutorial about matrix operations to learn more.

Logical / boolean operators

In addition, boolean or logical operators in R are used to specify multiple conditions between objects. These comparisons return TRUE and FALSE values.

Logical operator in R Description
& Elementwise logical ‘AND’
&& Vector logical ‘AND’
| Elementwise logical ‘OR’
|| Vector logical ‘OR’
! Logical negation ‘NOT’
xor() Elementwise exclusive ‘OR’ equivalent to !( x | y)

Relational / comparison operators in R

Comparison or relational operators are designed to compare objects and the output of these comparisons are of type boolean. To clarify, the following table summarizes the R relational operators.

Relational operator in R Description
> Greater than
< Lower than
>= Greater or equal than
<= Lower or equal than
== Equal to
!= Not equal to

For example, you can compare integer values with these operators as follows.

If you compare vectors the output will be other vector of the same length and each element will contain the boolean corresponding to the comparison of the corresponding elements (the first element of the first vector with the first element of the second vector and so on). Moreover, you can compare each element of a matrix against other.

Assignment operators in R

The assignment operators in R allows you to assign data to a named object in order to store the data .

Assignment operator in R Description
Left assignment
= Left assignment (not recommended) and argument assignment
Right assignment
Left lexicographic assignment (for advanced users)
Right lexicographic assignment (for advanced users)

Note that in almost scripting programming languages you can just use the equal (=) operator. However, in R it is recommended to use the arrow assignment ( <- ) and use the equal sign only to set arguments.

The arrow assignment can be used as left or right assignment, but the right assignment is not generally used. In addition, you can use the double arrow assignment, known as scoping assignment, but we won’t enter in more detail in this tutorial, as it is for advanced users. You can know more about this assignment operator in our post about functions in R .

In the following code block you will find some examples of these operators.

If you need to use the right assignment remember that the object you want to store needs to be at the left, or an error will arise.

There are some rules when naming variables. For instance, you can use letters, numbers, dots and underscores in the variable name, but underscores can’t be the first character of the variable name.

Reserved words

There are also reserved words you can’t use, like TRUE , FALSE , NULL , among others. You can see the full list of R reserved words typing help(Reserved) or ?Reserved .

However, if for some reason you need to name your variable with a reserved word or starting with an underscore you will need to use backticks:

Miscellaneous R operators

Miscellaneous operators in R are operators used for specific purposes , as accessing data, functions, creating sequences or specifying a formula of a model. To clarify, the next table contains all the available miscellaneous operators in R.

Miscellaneous operator in R Description
$ Named list or dataframe column subset
: Sequence generator
:: Accessing functions of packages It is not usually needed
::: Accessing internal functions of packages
~ Model formulae
@ Accessing slots in S4 classes (Advanced)

In addition, in the following block of code we show several examples of these operators:

Infix operator

You can call an operator as a function . This is known as infix operators. Note that this type of operators are not generally used or needed.

Pipe operator in R

The pipe operator is an operator you can find in several libraries, like dplyr . The operator can be read as ‘AND THEN’ and its purpose is to simplify the syntax when writing R code. As an example, you could subset the cars dataset and then create a summary of the subset with the following code:

R PACKAGES IO

Explore and discover thousands of packages, functions and datasets

R CHARTS

Learn how to plot your data in R with the base package and ggplot2

PYTHON CHARTS

PYTHON CHARTS

Learn how to create plots in Python with matplotlib, seaborn, plotly and folium

Related content

Convert objects to numeric with as.numeric()

Convert objects to numeric with as.numeric()

Introduction to R

Use the as.numeric function in R to coerce objects to numeric and learn how to check if an object is numeric with is.numeric

Data types in R

Data types in R

Review of data types in R programming ⚡ NUMERIC, LOGICAL, COMPLEX, STRING or CHARACTER and RAW data types. Learn how to check data type in R and coercion

Square root in R

Square root in R

R introduction

Use the sqrt function to compute the square root of any positive number and learn how to calculate the nth root for any positive number, such as the cube root

Try adjusting your search query

👉 If you haven’t found what you’re looking for, consider clicking the checkbox to activate the extended search on R CHARTS for additional graphs tutorials, try searching a synonym of your query if possible (e.g., ‘bar plot’ -> ‘bar chart’), search for a more generic query or if you are searching for a specific function activate the functions search or use the functions search bar .

r code for assignment

UC Business Analytics R Programming Guide

Assignment & evaluation.

The first operator you’ll run into is the assignment operator. The assignment operator is used to assign a value. For instance we can assign the value 3 to the variable x using the <- assignment operator. We can then evaluate the variable by simply typing x at the command line which will return the value of x . Note that prior to the value returned you’ll see ## [1] in the command line. This simply implies that the output returned is the first output. Note that you can type any comments in your code by preceding the comment with the hashtag ( # ) symbol. Any values, symbols, and texts following # will not be evaluated.

Interestingly, R actually allows for five assignment operators:

The original assignment operator in R was <- and has continued to be the preferred among R users. The = assignment operator was added in 2001 primarily because it is the accepted assignment operator in many other languages and beginners to R coming from other languages were so prone to use it. However, R uses = to associate function arguments with values (i.e. f(x = 3) explicitly means to call function f and set the argument x to 3. Consequently, most R programmers prefer to keep = reserved for argument association and use <- for assignment.

The operators <<- is normally only used in functions which we will not get into the details. And the rightward assignment operators perform the same as their leftward counterparts, they just assign the value in an opposite direction.

Overwhelmed yet? Don’t be. This is just meant to show you that there are options and you will likely come across them sooner or later. My suggestion is to stick with the tried and true <- operator. This is the most conventional assignment operator used and is what you will find in all the base R source code…which means it should be good enough for you.

Lastly, note that R is a case sensitive programming language. Meaning all variables, functions, and objects must be called by their exact spelling:

Difference between assignment operators in R

For R beginners, the first operator they use is probably the assignment operator <- . Google’s R Style Guide suggests the usage of <- rather than = even though the equal sign is also allowed in R to do exactly the same thing when we assign a value to a variable. However, you might feel inconvenient because you need to type two characters to represent one symbol, which is different from many other programming languages.

As a result, many users ask Why we should use <- as the assignment operator?

Here I provide a simple explanation to the subtle difference between <- and = in R.

First, let’s look at an example.

The above code uses both <- and = symbols, but the work they do are different. <- in the first two lines are used as assignment operator while = in the third line does not serves as assignment operator but an operator that specifies a named parameter formula for lm function.

In other words, <- evaluates the the expression on its right side ( rnorm(100) ) and assign the evaluated value to the symbol (variable) on the left side ( x ) in the current environment. = evaluates the expression on its right side ( y~x ) and set the evaluated value to the parameter of the name specified on the left side ( formula ) for a certain function.

We know that <- and = are perfectly equivalent when they are used as assignment operators.

Therefore, the above code is equivalent to the following code:

Here, we only use = but for two different purposes: in the first and second lines we use = as assignment operator and in the third line we use = as a specifier of named parameter.

Now let’s see what happens if we change all = symbols to <- .

If you run this code, you will find that the output are similar. But if you inspect the environment, you will observe the difference: a new variable formula is defined in the environment whose value is y~x . So what happens?

Actually, in the third line, two things happened: First, we introduce a new symbol (variable) formula to the environment and assign it a formula-typed value y~x . Then, the value of formula is provided to the first paramter of function lm rather than, accurately speaking, to the parameter named formula , although this time they mean the identical parameter of the function.

To test it, we conduct an experiment. This time we first prepare the data.

Basically, we just did similar things as before except that we store all vectors in a data frame and clear those numeric vectors from the environment. We know that lm function accepts a data frame as the data source when a formula is specified.

Standard usage:

Working alternative where two named parameters are reordered:

Working alternative with side effects that two new variable are defined:

Nonworking example:

The reason is exactly what I mentioned previously. We reassign data to data and give its value to the first argument ( formula ) of lm which only accepts a formula-typed value. We also try to assign z~x+y to a new variable formula and give it to the second argument ( data ) of lm which only accepts a data frame-typed value. Both types of the parameter we provide to lm are wrong, so we receive the message:

From the above examples and experiments, the bottom line gets clear: to reduce ambiguity, we should use either <- or = as assignment operator, and only use = as named-parameter specifier for functions.

In conclusion, for better readability of R code, I suggest that we only use <- for assignment and = for specifying named parameters.

Popular Tutorials

Popular examples, learn python interactively, r introduction.

  • R Reserved Words
  • R Variables and Constants

R Operators

  • R Operator Precedence and Associativitys

R Flow Control

  • R if…else Statement

R ifelse() Function

  • R while Loop
  • R break and next Statement
  • R repeat loop
  • R Functions
  • R Return Value from Function
  • R Environment and Scope
  • R Recursive Function

R Infix Operator

  • R switch() Function

R Data Structures

  • R Data Frame

R Object & Class

  • R Classes and Objects
  • R Reference Class

R Graphs & Charts

  • R Histograms
  • R Pie Chart
  • R Strip Chart

R Advanced Topics

  • R Plot Function
  • R Multiple Plots
  • Saving a Plot in R
  • R Plot Color

Related Topics

R Operator Precedence and Associativity

R Program to Add Two Vectors

In this article, you will learn about different R operators with the help of examples.

R has many operators to carry out different mathematical and logical operations. Operators perform tasks including arithmetic, logical and bitwise operations.

  • Type of operators in R

Operators in R can mainly be classified into the following categories:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • R Arithmetic Operators

These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus(Remainder from division)
%/% Integer Division

Let's look at an example illustrating the use of the above operators:

  • R Relational Operators

Relational operators are used to compare between values. Here is a list of relational operators available in R.

Operator Description
Less than
> Greater than
Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

Let's see an example for this:

  • Operation on Vectors

The above mentioned operators work on vectors . The variables used above were in fact single element vectors.

We can use the function c() (as in concatenate) to make vectors in R.

All operations are carried out in element-wise fashion. Here is an example.

When there is a mismatch in length (number of elements) of operand vectors, the elements in the shorter one are recycled in a cyclic manner to match the length of the longer one.

R will issue a warning if the length of the longer vector is not an integral multiple of the shorter vector.

  • R Logical Operators

Logical operators are used to carry out Boolean operations like AND , OR etc.

Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR
|| Logical OR

Operators & and | perform element-wise operation producing result having length of the longer operand.

But && and || examines only the first element of the operands resulting in a single length logical vector.

Zero is considered FALSE and non-zero numbers are taken as TRUE . Let's see an example for this:

  • R Assignment Operators

These operators are used to assign values to variables.

Operator Description
Leftwards assignment
->, ->> Rightwards assignment

The operators <- and = can be used, almost interchangeably, to assign to variables in the same environment.

The <<- operator is used for assigning to variables in the parent environments (more like global assignments). The rightward assignments, although available, are rarely used.

Check out these examples to learn more:

  • Add Two Vectors
  • Take Input From User
  • R Multiplication Table

Table of Contents

  • Introduction

Sorry about that.

R Tutorials

Programming


                    --->

Learn R Programming

Welcome to the learn-r.org interactive R tutorial with Examples and Exercises.

If you want to learn R for statistics, data science or business analytics, either you are new to programming or an experienced programmer this tutorial will help you to learn the R Programming language fast and efficient. 

R is a programming language used extensively for statistics and statistical computing, data science and business analytics. There are different libraries in R which are used for statistics and graphical techniques for simple stats tests, linear and time series modeling, classification, clustering, regression analysis and many more.

Why learn R 

Learning r means more job opportunities.

The field of data science is exploding these days and R and Python are the two languages mainly used for data analytics techniques due to their syntax, ease of use and application. R has many libraries for statistical computing and data analysis. If you learn R programming you can expect a salary starting from $75k while the average salary is $120k in data science jobs in USA. Data analysts and data scientists are in demand and there are a number of opportunities if one knows the skill. For current salaries and recent openings you may google it yourself.

Open source

R is opensource which means anyone and everyone can use it without paying. It is free under GNU license. Most of R packages are also available as free and you can use them for non-commercial as well as commercial activities. Statistical and analysis softwares usually cost from a few hundred to thousands of dollar, R provides the same functionality free of cost. If you have some extra bucks you may try costly softwares though.

Cross platform

R runs equally well on all platforms windows, linux or mac. Hence you may have a linux environment at office, windows at home and mac laptop for travelling, you will have the same experience at all platforms. The software development environment is same and also the applications run seamlessly at all platforms.  

R is ranked as number 5 in most popular programming languages by IEEE. It shows the interest in R is increasing and the fields of analytics, data science, machine learning and deep learning are exploding.

R is used by renowned companies

The effectiveness and application of R programming is illustrated by the fact that many tech giants are using it. Companies like Google, Microsoft, Twitter, Ford etc are using R. This explains the concreteness and robustness of R. 

  • How to Learn R

Usually it is said that the learning curve of R is steep. Well, individuals with some programming experience may learn it without any hurdle. People without any programming background can also learn it with ease with a complete learning schedule and learning it step by step. Remember Rome was not built in a day. You can not expect to learn R in one sitting or a day or a few days. Regular practice of R coding and understanding the logic and philosophy are the key to success in learning R. 

In this tutorial each R topic is divided into segments starting from a simple concept and then building on that knowledge moving towards complex ideas. The method to learn R is divide and conquer. Learn one topic at a time and get a good grasp over the concept and logic and write some R programs about the topic you are learning. Also try to solve the challenges given at the end of each tutorial. In this way you will learn this language fast and will set a solid foundation which will help you at advanced stages of data analysis. 

Start Learning R

Good enough introduction of R. There is no time to waste! lets start first concept of R programming right now. We provide R tutorial for total beginners even those who have never used any programming language before. So we start from the idea of variables.

The first idea to learn in every programming language is the concept of variables. Variables are like boxes or containers which store a value or some data. In a programming language we have to use numbers, characters, words etc but before using them we have to store them in some box or container so that we may use them later. Every variable has a name and some type, usually called as data type. In C, C++ or Java one has to tell the data type of variable, however in R you don't have to worry about it. Simply give a name to variable and thats it. Lets suppose we want to store or save age of a person in R. We give the name age to variable that will store the number in it. In R the code will be 

> age  <- 25

Here the first greater than sign       >      indicates the R prompt. We write code after that. 

<- or arrow is assignment operator in R. It assigns the value at its right to the variable at its left. Here it is assigning 25 to variable age or simply it is storing 25 number in age variable or box. 

And now if you want to print this variable use the print function like this. 

> print(age)

Wow, you have succeeded in assigning a value to a variable, storing some value in box and then later used that value in a function from that box. print function simply prints the value of any variable on R console.

This is easy, isn't it? if you follow this site, you will be able to learn R in the same simple and easy way, step by step & Fast.

R for Statistics, Data Science

  • R Hello World!
  • Set working directory
  • if else statement
  • Repeat Loop
  • R break & next
  • R Read CSV file
  • R Recursion
  • R switch function
  • ifelse() Function
  • R append() Function
  • R Standard deviation
  • R Code Examples

R Tutor Online

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

6 life-altering rstudio keyboard shortcuts.

Posted on January 4, 2021 by Business Science in R bloggers | 0 Comments

[social4i size="small" align="align-left"] --> [This article was first published on business-science.io , and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here ) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This article is part of a R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.

The RStudio IDE is amazing. You can enhance your R productivity even more with these simple keyboard shortcuts.

Here are the links to get set up. ?

  • Get the Code
  • YouTube Tutorial

6 Keyboard Shortcuts (that will change your life)

Let’s speed up common activities with these 6 super-useful keyboard shortcuts.

r code for assignment

1: Commenting & Uncommenting Code [Ctrl + Shift + C]

I use this all the time to turn text into commented text. Works with multiple lines too.

Go from this…

r code for assignment

To this…

r code for assignment

2: Add the Pipe %>% [Ctrl + Shift + M]

My students absolutely love this. You can easily add the Pipe %>% in any spot you’d like! Perfect for data wrangling with dplyr.

r code for assignment

3: Insert The Assignment Operator [Alt + -]

My code has tons of assignment operators. This is a simple, time-saver that will make you more productive in building functions and assigning variables values.

r code for assignment

4: Cursor-Select Multiple Lines [Ctrl + Alt + Up/Down/Click]

This is a recent addition to my portfolio of must-know keyboard shortcuts. Using Multi-Cursor Select has now become a go-to for editing R code .

Multi-Line Select

r code for assignment

…And edit!

r code for assignment

5: Find in Files [Ctrl + Shift + F]

THIS IS A SUPER POWER. Seriously. Learn to use this one right now!

Find in Files

r code for assignment

Found every instance of ggplot by file!

r code for assignment

6: Keyboard Shortcut Cheat Sheet [Alt + Shift + K]

More shortcuts!!! Run this to get a Keyboard Shortcut Cheat Sheet.

r code for assignment

Your coworkers will be jealous of your productivity. ?

But if you really want to improve your productivity…

Here’s how to master R. ?

What happens after you learn R for Business.

The look on your boss’s face after you’ve launched your first Shiny App . ?

This is career acceleration.

SETUP R-TIPS WEEKLY PROJECT

Sign Up to Get the R-Tips Weekly (You’ll get email notifications of NEW R-Tips as they are released): https://mailchi.mp/business-science/r-tips-newsletter

Set Up the GitHub Repo: https://github.com/business-science/free_r_tips

Check out the setup video (https://youtu.be/F7aYV0RPyD0). Or, Hit Pull in the Git Menu to get the R-Tips Code

Once you take these actions, you’ll be set up to receive R-Tips with Code every week. =)

  • Interactive Principal Component Analysis in R
  • How to Forecast with ARIMA Models in R
  • Automate Excel in R
  • Detect Relationships with Linear Regression

To leave a comment for the author, please follow the link and comment on their blog: business-science.io . R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job . Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Copyright © 2022 | MH Corporate basic by MH Themes

Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts. (You will not see this message again.)

  • Subscription

Tutorial: Getting Started with R and RStudio

In this tutorial we’ll learn how to begin programming with R using RStudio. We’ll install R , and RStudio RStudio , an extremely popular development environment for R. We’ll learn the key RStudio features in order to start programming in R on our own.

If you already know how to use RStudio and want to learn some tips, tricks, and shortcuts, check out this Dataquest blog post .

Table of Contents

1. install r, 2. install rstudio, 3. first look at rstudio, 4. the console, 5. the global environment.

  • 6. Install the  tidyverse  Packages
  • 7. Load the  tidyverse  Packages into Memory

8. Identify Loaded Packages

9. get help on a package, 10. get help on a function, 11. rstudio projects, 12. save your “real” work. delete the rest., 13. r scripts, 14. run code, 15. access built-in datasets, 17. reproducible reports with r markdown, 18. use rstudio cloud, 19. get your hands dirty, additional resources, bonus: cheatsheets, getting started with rstudio.

RStudio is an open-source tool for programming in R. RStudio is a flexible tool that helps you create readable analyses, and keeps your code, images, comments, and plots together in one place. It’s worth knowing about the capabilities of RStudio for data analysis and programming in R.

RStudio Layout

Using RStudio for data analysis and programming in R provides many advantages. Here are a few examples of what RStudio provides:

  • An intuitive interface that lets us keep track of saved objects, scripts, and figures
  • A text editor with features like color-coded syntax that helps us write clean scripts
  • Auto complete features save time
  • Tools for creating documents containing a project’s code, notes, and visuals
  • Dedicated Project folders to keep everything in one place

RStudio can also be used to program in other languages including SQL, Python, and Bash, to name a few.

But before we can install RStudio, we’ll need to have a recent version of R installed on our computer.

R is available to download from the official R website . Look for this section of the web page:

Download R

The version of R to download depends on our operating system. Below, we include installation instructions for Mac OS X, Windows, and Linux (Ubuntu).

  • Select the Download R for (Mac) OSX option.
  • Look for the most up-to-date version of R (new versions are released frequently and appear toward the top of the page) and click the .pkg file to download.
  • Open the .pkg file and follow the standard instructions for installing applications on MAC OS X.
  • Drag and drop the R application into the Applications folder.
  • Select the Download R for Windows option.
  • Select base , since this is our first installation of R on our computer.
  • Follow the standard instructions for installing programs for Windows. If we are asked to select Customize Startup or Accept Default Startup Options , choose the default options.

Linux/Ubuntu

  • Select the Download R for Linux option.
  • Select the Ubuntu option.
  • Alternatively, select the Linux package management system relevant to you if you are not using Ubuntu .

RStudio is compatible with many versions of R (R version 3.0.1 or newer as of July, 2020). Installing R separately from RStudio enables the user to select the version of R that fits their needs.

Now that R is installed, we can install RStudio. Navigate to the RStudio downloads page .

When we reach the RStudio downloads page, let’s click the “Download” button of the RStudio Desktop Open Source License Free option:

Our operating system is usually detected automatically and so we can directly download the correct version for our computer by clicking the “Download RStudio” button. If we want to download RStudio for another operating system (other than the one we are running), navigate down to the “All installers” section of the page.

RStudio Desktop

When we open RStudio for the first time, we’ll probably see a layout like this:

RStudio Desktop

When we open RStudio, R is launched as well. A common mistake by new users is to open R instead of RStudio. To open RStudio, search for RStudio on the desktop, and pin the RStudio icon to the preferred location (e.g. Desktop or toolbar).

Let’s start off by introducing some features of the Console . The Console is a tab in RStudio where we can run R code.

Notice that the window pane where the console is located contains three tabs: Console , Terminal and Jobs (this may vary depending on the version of RStudio in use). We’ll focus on the Console for now.

When we open RStudio, the console contains information about the version of R we’re working with. Scroll down, and try typing a few expressions like this one. Press the enter key to see the result.

As we can see, we can use the console to test code immediately. When we type an expression like 1 + 2 , we’ll see the output below after hitting the enter key.

Console Example

We can store the output of this command as a variable. Here, we’ve named our variable result:

The <- is called the assignment operator. This operator assigns values to variables. The command above is translated into a sentence as:

> The result variable gets the value of one plus two.

One nice feature from RStudio is the keyboard shortcut for typing the assignment operator <- :

  • Mac OS X: Option + -
  • Windows/Linux: Alt + -

We highly recommend that you memorize this keyboard shortcut because it saves a lot of time in the long run!

When we type result into the console and hit enter, we see the stored value of 3 :

When we create a variable in RStudio, it saves it as an object in the R global environment . We’ll discuss the environment and how to view objects stored in the environment in the next section.

We can think of the global environment as our workspace. During a programming session in R, any variables we define, or data we import and save in a dataframe, are stored in our global environment. In RStudio, we can see the objects in our global environment in the Environment tab at the top right of the interface:

Global Environment

We’ll see any objects we created, such as result , under values in the Environment tab. Notice that the value, 3 , stored in the variable is displayed.

Sometimes, having too many named objects in the global environment creates confusion. Maybe we’d like to remove all or some of the objects. To remove all objects, click the broom icon at the top of the window:

Broom Icon

To remove selected objects from the workspace, select the Grid view from the dropdown menu:

Grid

Here we can check the boxes of the objects we’d like to remove and use the broom icon to clear them from our Global Environment .

6. Install the tidyverse Packages

Much of the functionality in R comes from using packages. Packages are shareable collections of code, data, and documentation. Packages are essentially extensions, or add-ons, to the R program that we installed above.

One of the most popular collection of packages in R is known as the “tidyverse”. The tidyverse is a collection of R packages designed for working with data. The tidyverse packages share a common design philosophy, grammar, and data structures. Tidyverse packages “play well together”. The tidyverse enables you to spend less time cleaning data so that you can focus more on analyzing, visualizing, and modeling data.

Let’s learn how to install the tidyverse packages. The most common “core” tidyverse packages are:

  • readr , for data import.
  • ggplot2 , for data visualization.
  • dplyr , for data manipulation.
  • tidyr , for data tidying.
  • purrr , for functional programming.
  • tibble , for tibbles, a modern re-imagining of dataframes.
  • stringr , for string manipulation.
  • forcats , for working with factors (categorical data).

To install packages in R we use the built-in install.packages() function. We could install the packages listed above one-by-one, but fortunately the creators of the tidyverse provide a way to install all these packages from a single command. Type the following command in the Console and hit the enter key.

The install.packages() command only needs to be used to download and install packages for the first time.

7. Load the tidyverse Packages into Memory

After a package is installed on a computer’s hard drive, the library() command is used to load a package into memory:

Loading the package into memory with library() makes the functionality of a given package available for use in the current R session. It is common for R users to have hundreds of R packages installed on their hard drive, so it would be inefficient to load all packages at once. Instead, we specify the R packages needed for a particular project or task.

Fortunately, the core tidyverse packages can be loaded into memory with a single command. This is how the command and the output looks in the console:

The Attaching packages section of the output specifies the packages and their versions loaded into memory. The Conflicts section specifies any function names included in the packages that we just loaded to memory that share the same name as a function already loaded into memory. Using the example above, now if we call the filter() function, R will use the code specified for this function from the dplyr package. These conflicts are generally not a problem, but it’s worth reading the output message to be sure.

If we need to check which packages we loaded, we can refer to the Packages tab in the window at the bottom right of the console.

Packages

We can search for packages, and checking the box next to a package loads it (the code appears in the console).

Alternatively, entering this code into the console will display all packages currently loaded into memory:

Which returns:

Another useful function for returning the names of packages currently loaded into memory is search() :

We’ve learned how to install and load packages. But what if we’d like to learn more about a package that we’ve installed? That’s easy! Clicking the package name in the Packages tab takes us to the Help tab for the selected package. Here’s what we see if we click the tidyr package:

Package Help

Alternatively, we can type this command into the console and achieve the same result:

The help page for a package provides quick access to documentation for each function included in a package. From the main help page for a package you can also access “vignettes” when they are available. Vignettes provide brief introductions, tutorials, or other reference information about a package, or how to use specific functions in a package.

Which results in this list of available options:

From there, we can select a particular vignette to view:

Now we see the Pivot vignette is displayed in the Help tab. This is one example of why RStudio is a powerful tool for programming in R. We can access function and package documentation and tutorials without leaving RStudio!

As we learned in the last section, we can get help on a function by clicking the package name in Packages and then click on a function name to see the help file. Here we see the pivot_longer() function from the tidyr package is at the top of this list:

Tidyr Functions

We can achieve the same results in the Console with any of these function calls:

Note that the specific Help tab for the pivot_longer() function (or any function we’re interested in) may not be the default result if the package that contains the function is not loaded into memory yet. In general it’s best to ensure a specific package is loaded before seeking help on a function.

RStudio offers a powerful feature to keep you organized; Projects . It is important to stay organized when you work on multiple analyses. Projects from RStudio allow you to keep all of your important work in one place, including code scripts, plots, figures, results, and datasets.

Create a new project by navigating to the File tab in RStudio and select New Project... . Then specify if you would like to create the project in a new directory, or in an existing directory. Here we select “New Directory”:

Create Project

RStudio offers dedicated project types if you are working on an R package, or a Shiny Web Application. Here we select “New Project”, which creates an R project:

New Project

Next, we give our project a name. “Create project as a subdirectory of:” is showing where the folder will live on the computer. If we approve of the location select “Create Project”, if we do not, select “Browse” and choose the location on the computer where this project folder should live.

Name Project

Now in RStudio we see the name of the project is indicated in the upper-right corner of the screen. We also see the .Rproj file in the Files tab. Any files we add to, or generate-within, this project will appear in the Files tab.

Project Overview

RStudio Projects are useful when you need to share your work with colleagues. You can send your project file (ending in .Rproj) along with all supporting files, which will make it easier for your colleagues to recreate the working environment and reproduce the results.

This tip comes from our 23 RStudio Tips, Tricks, and Shortcuts blog post, but it’s so important that we are sharing it here as well!

Practice good housekeeping to avoid unforeseen challenges down the road. If you create an R object worth saving, capture the R code that generated the object in an R script file. Save the R script, but don’t save the environment, or workspace, where the object was created.

To prevent RStudio from saving your workspace, open Preferences > General and un-select the option to restore .RData into workspace at startup. Be sure to specify that you never want to save your workspace, like this:

Never Save Your Workspace

Now, each time you open RStudio, you will begin with an empty session. None of the code generated from your previous sessions will be remembered. The R script and datasets can be used to recreate the environment from scratch.

Other experts agree that not saving your workspace is best practice when using RStudio.

As we worked through this tutorial, we wrote code in the Console . As our projects become more complex, we write longer blocks of code. If we want to save our work, it is necessary to organize our code into a script. This allows us to keep track of our work on a project, write clean code with plenty of notes, reproduce our work, and share it with others.

In RStudio, we can write scripts in the text editor window at the top left of the interface:

R Script

We can also use the keyboard shortcut Ctrl + Shift + N. When we save a script, it has the file extension .R. As an example, we’ll create a new script that includes this code to generate a scatterplot:

To save our script we navigate to the File menu tab and select Save . Or we enter the following command:

  • Mac OS X: Cmd + S
  • Windows/Linux: Ctrl + S

To run a single line of code we typed into our script, we can either click Run at the top right of the script, or use the following keyboard commands when our cursor is on the line we want to run:

  • Mac OS X: Cmd + Enter
  • Windows/Linux: Ctrl + Enter

In this case, we’ll need to highlight multiple lines of code to generate the scatterplot. To highlight and run all lines of code in a script enter:

  • Mac OS X: Cmd + A + Enter
  • Windows/Linux: Ctrl + A + Enter

Let’s check out the result when we run the lines of code specified above:

R Script

Side note: this scatterplot is generated using data from the mpg dataset that is included in the ggplot2 package. The dataset contains fuel economy data from 1999 to 2008, for 38 popular models of cars.

In this plot, the engine displacement (i.e. size) is depicted on the x-axis (horizontal axis). The y-axis (vertical axis) depicts the fuel efficiency in miles-per-gallon. In general, fuel economy decreases with the increase in engine size. This plot was generated with the tidyverse package ggplot2 . This package is very popular for data visualization in R.

Want to learn more about the mpg dataset from the ggplot2 package that we mentioned in the last example? Do this with the following command:

From there you can take a look at the first six rows of data with the head() function:

Obtain summary statistics with the summary() function:

Or open the help page in the Help tab, like this:

Finally, there are many datasets built-in to R that are ready to work with. Built-in datasets are handy for practicing new R skills without searching for data. View available datasets with this command:

When writing an R script, it’s good practice to specify packages to load at the top of the script:

As we write R scripts, it’s also good practice add comments to explain our code (# like this). R ignores lines of code that begin with #. It’s common to share code with colleagues and collaborators. Ensuring they understand our methods will be very important. But more importantly, thorough notes are helpful to your future-self, so that you can understand your methods when you revisit the script in the future!

Here’s an example of what comments look like with our scatterplot code:

The comments used in the example above are fine for providing brief notes about our R script, but this format is not suitable for authoring reports where we need to summarize results and findings. We can author nicely formatted reports in RStudio using R Markdown files.

R Markdown is an open-source tool for producing reproducible reports in R. R Markdown enables us to keep all of our code, results, and writing, in one place. With R Markdown we have the option to export our work to numerous formats including PDF, Microsoft Word, a slideshow , or an html document for use in a website.

If you would like to learn R Markdown, check out these Dataquest blog posts:

  • Getting Started with R Markdown — Guide and Cheatsheet

R Markdown Tips, Tricks, and Shortcuts

RStudio now offers a cloud-based version of RStudio Desktop called RStudio Cloud . RStudio Cloud allows you to code in RStudio without installing software, you only need a web browser. Almost everything we’ve learned in this tutorial applies to RStudio Cloud!

Work in RStudio Cloud is organized into projects similar to the desktop version. RStudio Cloud enables you to specify the version of R you wish to use for each project. This is great if you are revisiting an older project built around a previous version of R.

RStudio Cloud also makes it easy and secure to share projects with colleagues, and ensures that the working environment is fully reproducible every time the project is accessed.

The layout of RStudio Cloud is very similar to RStudio Desktop:

cloud

The best way to learn RStudio is to apply what we’ve covered in this tutorial. Jump in on your own and familiarize yourself with RStudio! Create your own projects, save your work, and share your results. We can’t emphasize the importance of this enough.

Not sure where to start? Check out the additional resources listed below!

If you enjoyed this tutorial, come learn with us at Dataquest! If you are new to R and RStudio, we recommend starting with the Dataquest Introduction to Data Analysis in R course. This is the first course in the Dataquest Data Analyst in R path.

For more advanced RStudio tips check out the Dataquest blog post 23 RStudio Tips, Tricks, and Shortcuts .

Learn how to load and clean data with tidyverse tools in this Dataquest blog post .

RStudio has published numerous in-depth how to articles about using RStudio. Find them here .

There is an official RStudio Blog .

Learn R and the tidyverse with R for Data Science by Hadley Wickham. Solidify your knowledge by working through the exercises in RStudio and saving your work for future reference.

RStudio has published numerous cheatsheets for working with R, including a detailed cheatsheet on using RStudio ! Select cheatsheets can be accessed from within RStudio by selecting Help > Cheatsheets .

More learning resources

Tutorial: poisson regression in r.

Learn data skills 10x faster

Headshot

Join 1M+ learners

Enroll for free

  • Data Analyst (Python)
  • Gen AI (Python)
  • Business Analyst (Power BI)
  • Business Analyst (Tableau)
  • Machine Learning
  • Data Analyst (R)
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

Why are there two assignment operators, `<-` and `->` in R?

I know how to use <- and -> , and there are several writeups on the difference between equals assignment & arrow assignment, but I don't know when to prefer -> over <- .

It seems the community has coalesced around using <- for assignment.

Neither the google R style-guide , nor Hadley Wickam's tidyverse R style-guide even mention -> in the assignment section.

I'm curious about the design considerations that led the S/S-PLUS developers to put in the right arrow assign operator -> . In what setting(s) would using -> be considered more readable (or easier to type) versus <- or = ?

I'm not familiar with any other language that allows the right-assignment semantics. What languages inspired R in this regard?

I'm looking for answers that cite books / early design documents / user manuals / archived mailing lists or other references to establish what the S author/designer's intent was in putting in the forward-arrow assignment operator.

  • language-design

Haleemur Ali's user avatar

  • 1 Possibly related: softwareengineering.stackexchange.com/questions/98406/… –  MrFlick Commented Jul 26, 2018 at 21:01
  • 4 Shame, my previous comment vanished but I think it’s relevant: the “good writeups” that you linked are inaccurate and misleading . A correct explanation of the differences can be found on Reddit: reddit.com/r/Rlanguage/comments/47c3z7/… — If somebody has an issue with this comment, please let me know why , rather than just flagging it. –  Konrad Rudolph Commented Jul 27, 2018 at 16:01
  • so many points in so little time in this thread, where was this posted ? –  moodymudskipper Commented Jul 27, 2018 at 18:59
  • @Moody_Mudskipper, posted here originally, not moved to SO from a sub-site. I bet a lot of people wonder why R/S is so quirky. I couldn't find any references myself, but thankfully Ben's answer cites Chambers, the primary author of S. –  Haleemur Ali Commented Jul 27, 2018 at 20:07
  • I though it wwas linked from a blog or reddit or something, I was just curious –  moodymudskipper Commented Jul 27, 2018 at 20:16

4 Answers 4

From the answer to an exercise in The New S Language (Becker, Chambers and Wilks 1988), via Google Books:

When you type a long expression only to remember at the end that it would be a good idea to save the result, a right-hand arrow allows you to perform an assignment without retyping the line.

This suggests that S users were working directly in the console, without line-editing capabilities that are available in most modern REPL /interactive environments ...

Some archaeology: I poked around in foundational sources on Google Books. There are three relevant books:

  • the Brown Book: S: An Interactive Environment for Data Analysis and Graphics R. A. Becker, J. M. Chambers (CRC Press, 1984)
  • Extending the S System , Becker and Chambers (Taylor & Francis, 1985)

the Blue Book: The New S Language Becker, Chambers and Wilks (Wadsworth and Brooks/Cole 1988, but reissued in 2018!! by CRC Press)

  • The Brown Book doesn't mention -> in the main text:

enter image description here

but it does in the appendix:

enter image description here

  • Extending the S System mentions -> in the main text:

enter image description here

I can't search very much of the book, so -> might also be mentioned in the appendix somewhere.

  • The Blue Book refers to the right-arrow, but actually seems to have a typo:

enter image description here

I interpret the underlined red passages as supporting that there's a typo in the first underlined line, which should be -> rather than ← ...

Here's the screenshot of the exercise answer referred to above:

enter image description here

If you want a copy of the 1985 book you can get it for $34.41 - or $1070.99 (but with free shipping!) ...

enter image description here

  • Thank you for this answer. This design makes perfect sense for interactive-consoles where lines could not be edited. Of course I hadn't even considered that S was developed in the 70s & the available contemporary hardware & software before posting this question. Incidentally, I wonder who would pay $1,070.99 for a copy of Becker: Extending The S System –  Haleemur Ali Commented Jul 27, 2018 at 0:01
  • I'm also wondering if the choice to use <- was influenced by the need to have -> . The forward arrow solved a real problem for people working on consoles that didn't have an up arrow to browse the history & allow editing. –  Haleemur Ali Commented Jul 27, 2018 at 0:23
  • 3 I've heard/believe it's documented elsewhere that the keyboards in use at Bell Labs at that times had a dedicated left-arrow key ... –  Ben Bolker Commented Jul 27, 2018 at 3:16
  • 2 Informative (+1). You (or Google Books) have a minor typo, using "Chamber" twice when it should be "Chambers" –  John Coleman Commented Jul 27, 2018 at 11:43

I think it is just a matter of personal preference.

Although -> predated magrittr pipes, one recent use case is that -> can be used to keep the flow from left to right in such pipes:

On the other hand, given that <- is mostly used you might want to use <- even in that case.

The argument for <- is that it starts the line off with the value being set which is sort of like the purpose of the statement, particularly if the result variable is well named, whereas the right hand side is the mechanics and so is detail subservient to the result -- and one likes to start off with an overview and then only get into the detail later. Below we are defining parameter k . That it is 3 or whether it is defined by a constant as it is here or a complex expression seems incidental to the purpose of the statement.

Personally, I never use -> .

G. Grothendieck's user avatar

  • 2 One very minor disadvantage of <- is that its internal implementation is a little weird: str(quote(a->b)) gives language b <- a , i.e. the parser actually flips the expression around when parsing it ... –  Ben Bolker Commented Jul 26, 2018 at 21:43
  • 2 Another point is that <- makes it easier keep track of object names. If you're writing expressions that end in -> for assignment, your object names will be horizontally scattered, where as consistently using <- means each object name can be predictably located. –  Mako212 Commented Jul 26, 2018 at 22:53
  • 1 Worth to note that the Tidyverse style guide says to not use -> when using pipes. –  JAD Commented Jul 27, 2018 at 6:50
  • 2 @Mako212 Spot on. It’s in fact actively harmful for code quality. Make your side-effects (= assignment) visible, don’t hide them at the end of the line. –  Konrad Rudolph Commented Jul 27, 2018 at 9:34

I can’t speculate on R’s reasons for allowing left-to-right assignment. And it’s certainly true that most programming languages (nearly all, in fact) perform only right-to-left assignment.

That said, R isn’t entirely on its own.

I'm not familiar with any other language that allows the right-assignment semantics.

I can think of at least three other (families of) languages that allow it:

Assembly languages often perform left-to-right assignment; for instance, the influential AT&T syntax writes assignment like this:

This assigns the value 1 to the EAX register. (On the other hand, Intel’s x86 syntax performs right-to-left assignment.)

TI-BASIC’s STO (“store”) operation is written like this:

COBOL uses multiple forms of left-to-right assignment:

I’m however doubtful whether any of these languages served as inspiration for R’s assignment syntax. By contrast, the APL programming language uses arrow-assignment, and it’s generally accepted that S (and thus indirectly R) took inspiration from that ; but APL only performs right-to-left assignment ( var ← value ).

Konrad Rudolph's user avatar

  • 1 Also POP-2. POP-2 uses an explicit stack; so this would be legal: str.len; f(n+1); 'A' ->Z ->Y ->X which would assign str.len to X, assuming f() returned a single result. More commonly, a function returned multiple results, as in Lua. –  Mischa Commented Sep 3, 2018 at 0:26

R is meant to have a syntax which is fairly natural for expressing mathematics. It is interesting to note that -> is actually common notation in describing some elementary row operations on matrices. For example, s*R_i -> R_i would be used to denote the operation of replacing row i by s times row i. Something like 2*A[1,] -> A[1,] is perfectly valid R. I don't know if these considerations had anything to do with the design decision, but it does show that it is a reasonable choice for a mathematical language.

John Coleman's user avatar

  • This is interesting but R doesn’t actually strike me as a language that tried to make matrix operation syntax natural. Compared to Matlab, R’s matrix syntax is positively convoluted. –  Konrad Rudolph Commented Jul 27, 2018 at 9:42
  • 1 @KonradRudolph I agree that its syntax isn't optimized for matrices ( %*% ??), but it is still true that R is a mathematical programming language which is easy for mathematicians to use. All 3 of the original main developers of S then R (Chambers, Ihaka, and Gentleman) had doctorates in mathematics or statistics. For people with such training, the established use of arrows in linear algebra would make both <- and -> natural choices for assignment. My background is also in math, and I liked <- when I first saw it. And even though I don't use -> , it always struck me as a nice touch. –  John Coleman Commented Jul 27, 2018 at 11:37

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged r language-design s or ask your own question .

  • The Overflow Blog
  • Where developers feel AI coding tools are working—and where they’re missing...
  • Masked self-attention: How LLMs learn relationships between tokens
  • Featured on Meta
  • User activation: Learnings and opportunities
  • Preventing unauthorized automated access to the network
  • Should low-scoring meta questions no longer be hidden on the Meta.SO home...
  • Announcing the new Staging Ground Reviewer Stats Widget
  • Feedback Requested: How do you use the tagged questions page?

Hot Network Questions

  • What is the simplest formula for calculating the circumference of a circle?
  • tan 3θ = cot(−θ)
  • Neil Tyson: gravity is the same every where on the geoid
  • Azure SQL Database CLR functions are working
  • Java class subset of C++ std::list with efficient std::list::sort()
  • Does the Rogue's Evasion cost a reaction?
  • Why does Voyager use consumable hydrazine instead of reaction wheels that only rotate when moving the spacecraft?
  • White (king and 2 bishops) vs Black (king and 1 knight). White to play and mate in 2
  • Does a passenger jet detect excessive weight in the tail after it's just landed?
  • Can we solve the Sorites paradox with probability?
  • Waiting girl's face
  • Do pilots have to produce their pilot license to police when asked?
  • Where is the best place to get bows in TotK?
  • Writing dental notations
  • How should I ask permission for using tables or figures from published papers, in my own paper?
  • In John 3:16, what is the significance of Jesus' distinction between the terms 'world' and 'everyone who believes' within the context?
  • How do you tell someone to offer something to everyone in a room by taking it physically to everyone in the room so everyone can have it?
  • Are there individual protons and neutrons in a nucleus?
  • Purpose of sleeve on sledge hammer handle
  • Five Hundred Cigarettes
  • Is it actually really easy to state conjectures, which are open and on the first view really hard to prove?
  • Given the optimal ate pairing e(A,B)=y is to possible to determine I and J such as e(I,J)=2y or even e(I,J)=3y?
  • Why would elves care what happens to Middle Earth?
  • Matter made of neutral charges does not radiate?

r code for assignment

IMAGES

  1. R Code Assignment Help with Upto 50% OFF by Top Academic Experts

    r code for assignment

  2. 4

    r code for assignment

  3. The Complete Guide to R Programming Assignment Help for Beginners and

    r code for assignment

  4. R Programming: Week-1: Assignment-1: Demonstration

    r code for assignment

  5. Need to write an r-code for my assignment. Replace

    r code for assignment

  6. Need to write an r-code for my assignment. Replace

    r code for assignment

VIDEO

  1. ⏩ AIOU Code 1345 Solved Asignment No.2 Spring 2024 |Subject: Principles of Commerce |Level: FA/ ICom

  2. Online Daycare Centre

  3. AIOU || Course Code || 1973 || Spring 2024 || Assignment No#1

  4. AIOU Code 343 Solved Assignment No.4 Spring 2024||Rais Aiou studio

  5. D.EL.ED 2nd sem assignment || paper code S2.5 || proficiency in English

  6. AIOU || Course Code || 451 || Spring 2024 || Assignment No#1

COMMENTS

  1. Assignment Operators in R (3 Examples)

    On this page you'll learn how to apply the different assignment operators in the R programming language. The content of the article is structured as follows: 1) Example 1: Why You Should Use <- Instead of = in R. 2) Example 2: When <- is Really Different Compared to =. 3) Example 3: The Difference Between <- and <<-.

  2. r

    @ClarkThomborson The semantics are fundamentally different because in R assignment is a regular operation which is performed via a function call to an assignment function. However, this is not the case for = in an argument list. In an argument list, = is an arbitrary separator token which is no longer present after parsing. After parsing f(x = 1), R sees (essentially) call("f", 1).

  3. Assignment Operators in R

    R provides two operators for assignment: <-and =. Understanding their proper use is crucial for writing clear and readable R code. Using the <-Operator For Assignments. The <-operator is the preferred choice for assigning values to variables in R. It clearly distinguishes assignment from argument specification in function calls.

  4. R: Assignment Operators

    Details. There are three different assignment operators: two of them have leftwards and rightwards forms. The operators <-and = assign into the environment in which they are evaluated. The operator <-can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of ...

  5. Use = or <- for assignment? (Revolutions)

    Many people are more used to using = for assignment, and it's one less keystroke if you want to save on typing. On the other hand, many R traditionalists prefer <- for clarity, and if you plan to share or publish your code, other might find code using = for assignment hard to read. Personally, I prefer the arrow notation, with a space on either ...

  6. assignOps: Assignment Operators

    Details. There are three different assignment operators: two of them have leftwards and rightwards forms. The operators <-and = assign into the environment in which they are evaluated. The operator <-can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of ...

  7. Difference between assignment operators in R

    Here I provide a simple explanation to the subtle difference between <- and = in R. First, let's look at an example. x <- rnorm (100) y <- 2*x + rnorm (100) lm (formula=y~x) The above code uses both <- and = symbols, but the work they do are different. <- in the first two lines are used as assignment operator while = in the third line does not ...

  8. R Operators [Arithmetic, Logical, ... With Examples]

    Assignment operators in R The assignment operators in R allows you to assign data to a named object in order to store the data. Assignment operator in R ... You can know more about this assignment operator in our post about functions in R. In the following code block you will find some examples of these operators. x <- 3 x = 26 rnorm(n = 10) 3 ...

  9. Assignment & Evaluation · UC Business Analytics R Programming Guide

    The original assignment operator in R was <-and has continued to be the preferred among R users. The = assignment operator was added in 2001 primarily because it is the accepted assignment operator in many other languages and beginners to R coming from other languages were so prone to use it. However, R uses = to associate function arguments with values (i.e. f(x = 3) explicitly means to call ...

  10. Difference between assignment operators in R

    The above code uses both <-and = symbols, but the work they do are different.<-in the first two lines are used as assignment operator while = in the third line does not serves as assignment operator but an operator that specifies a named parameter formula for lm function.In other words, <-evaluates the the expression on its right side (rnorm(100)) and assign the evaluated value to the symbol ...

  11. R Operators (With Examples)

    The <<-operator is used for assigning to variables in the parent environments (more like global assignments). The rightward assignments, although available, are rarely used. x <- 5 x x <- 9 x 10 -> x x. Output [1] 5 [1] 9 [1] 10. Check out these examples to learn more: Add Two Vectors; Take Input From User; R Multiplication Table

  12. How to Use the assign() Function in R (3 Examples)

    The assign() function in R can be used to assign values to variables.. This function uses the following basic syntax: assign(x, value) where: x: A variable name, given as a character string.; value: The value(s) to be assigned to x.; The following examples show how to use this function in practice.

  13. R Basics Cheat Sheet

    Getting Started with R Cheat Sheet. This cheat sheet will cover an overview of getting started with R. Use it as a handy, high-level reference for a quick start with R. For more detailed R Cheat Sheets, follow the highlighted cheat sheets below. R is one of the most popular programming languages in data science and is widely used across various ...

  14. Learn R

    In R the code will be > age <- 25. Here the first greater than sign > indicates the R prompt. We write code after that. <- or arrow is assignment operator in R. It assigns the value at its right to the variable at its left. Here it is assigning 25 to variable age or simply it is storing 25 number in age variable or box.

  15. What is the R assignment operator

    The development version of R now allows some assignments to be written C- or Java-style, using the = operator. This increases compatibility with S-Plus (as well as with C, Java, and many other languages). All the previously allowed assignment operators (<-, :=, _, and <<-) remain fully in effect. It seems the := function is no longer present ...

  16. PDF An Introduction to R

    An Introduction to R Notes on R: A Programming Environment for Data Analysis and Graphics Version 4.4.1 (2024-06-14) W. N. Venables, D. M. Smith

  17. 6 Life-Altering RStudio Keyboard Shortcuts

    YouTube Tutorial. 6 Keyboard Shortcuts (that will change your life) Let's speed up common activities with these 6 super-useful keyboard shortcuts. (Click image to play tutorial) 1: Commenting & Uncommenting Code. [Ctrl + Shift + C] I use this all the time to turn text into commented text. Works with multiple lines too.

  18. Tutorial: Getting Started with R and RStudio

    Getting Started with RStudio. RStudio is an open-source tool for programming in R. RStudio is a flexible tool that helps you create readable analyses, and keeps your code, images, comments, and plots together in one place. It's worth knowing about the capabilities of RStudio for data analysis and programming in R.

  19. How can I assign a value using if-else conditions in R

    So your code could look like this. mydata <- cbind(a, ifelse(a>10, "double", "single")) (Specified in comments below that if a=10, then "double") Share. ... conditional assignment of values. 0. specifying conditions as a variable to assign values in R. 1. Conditional if/else statement in R. 0.

  20. Why are there two assignment operators, `<-` and `->` in R?

    2. Another point is that <- makes it easier keep track of object names. If you're writing expressions that end in -> for assignment, your object names will be horizontally scattered, where as consistently using <- means each object name can be predictably located. - Mako212. Jul 26, 2018 at 22:53.