{"id":909,"date":"2023-02-10T14:54:06","date_gmt":"2023-02-10T22:54:06","guid":{"rendered":"https:\/\/www.kenwalger.com\/blog\/?p=909"},"modified":"2023-02-14T13:53:51","modified_gmt":"2023-02-14T21:53:51","slug":"assignment-operators-r","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/uncategorized\/assignment-operators-r\/","title":{"rendered":"Assignment Operators in R \u2013 Which One to Use and Where"},"content":{"rendered":"<p>Whenever you start learning a new programming language, you must get accustomed to the language&#8217;s syntax. One of the first operators you&#8217;d expect to come across is the assignment operator for the language. Assignment operators are used to, well, <em>assign<\/em> values to variables. The <a href=\"https:\/\/www.amazon.com\/gp\/product\/013454692X\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=013454692X&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=6555c31988678e040dc34fe5109624ea\" target=\"_blank\" rel=\"noopener\">R language<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=013454692X\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> has a few different ways to assign values. Let&#8217;s take a quick look and them and some of their differences and use cases.<\/p>\n<h5>Assignment Operators<\/h5>\n<p>R has five common assignment operators:<\/p>\n<ul>\n<li>&lt;-<\/li>\n<li>-&gt;<\/li>\n<li>&lt;&lt;-<\/li>\n<li>-&gt;&gt;<\/li>\n<li>=<\/li>\n<\/ul>\n<p>Many style guides and traditionalists prefer the left arrow operator, <code>&lt;-<\/code>. Why use that when it&#8217;s an extra keystroke?\u00a0<code>&lt;-<\/code> always means assignment. The equal sign is overloaded a bit taking on the roles of an assignment operator, function argument binding, or depending on the context, case statement.<\/p>\n<h5>Equal or &#8220;arrow&#8221; as an Assignment Operator?<\/h5>\n<p>In R, both the equal and arrow symbols work to assign values. Therefore, the following statements have the same effect of assigning a value on the right to the variable on the left:<\/p>\n<p><code>x = 42<\/code><\/p>\n<p><code>x &lt;- 42<\/code><\/p>\n<p>There is also a right arrow, <code>-&gt;<\/code> which assigns the value on the left, to a variable on the right:<\/p>\n<p><code>42 -&gt; x<\/code><\/p>\n<p>All three assign the\u00a0<em>value<\/em> of forty-two to the\u00a0<em>variable<\/em>\u00a0<code>x<\/code>.<\/p>\n<p>So what&#8217;s the difference? Are these assignment operators interchangeable? Mostly, yes. The difference comes into play, however, when working with functions.<\/p>\n<p>The equal sign can also work as an operator for function parameters.<\/p>\n<p>x &lt;- 42<br \/>\ny &lt;- 18<br \/>\nfunction(value = x-y)<\/p>\n<h5>History of the &lt;- Operator<\/h5>\n<p>Where did the arrow as an assignment operator originate? As you may know, the R language has its origins in the <a href=\"https:\/\/en.wikipedia.org\/wiki\/S_(programming_language)\">S language<\/a>. S was originally influenced, in part, by <a href=\"https:\/\/en.wikipedia.org\/wiki\/APL_(programming_language)\">APL<\/a>. APL had its own <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01MSAR2TX\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01MSAR2TX&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=2361863fee7a60ae8effa29688b589c1\" target=\"_blank\" rel=\"noopener\">keyboards<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B01MSAR2TX\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> which included arrow symbols.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"910\" data-permalink=\"https:\/\/www.kenwalger.com\/blog\/uncategorized\/assignment-operators-r\/attachment\/apl_keyboard\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2018\/11\/APL_keyboard.png?fit=600%2C183&amp;ssl=1\" data-orig-size=\"600,183\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"APL_keyboard\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2018\/11\/APL_keyboard.png?fit=300%2C92&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2018\/11\/APL_keyboard.png?fit=600%2C183&amp;ssl=1\" class=\"aligncenter wp-image-910\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2018\/11\/APL_keyboard.png?resize=411%2C126&#038;ssl=1\" alt=\"APL Keyboard Layout - Assignment Operators next to the &quot;P&quot; key\" width=\"411\" height=\"126\" srcset=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2018\/11\/APL_keyboard.png?resize=300%2C92&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2018\/11\/APL_keyboard.png?w=600&amp;ssl=1 600w\" sizes=\"auto, (max-width: 411px) 85vw, 411px\" \/><\/p>\n<p>The S language also didn&#8217;t have == for equality testing, so that was left to the single equal sign. Therefore, variable assignment needed to be accomplished with a different symbol, and the arrow was chosen.<\/p>\n<h5>Conclusion<\/h5>\n<p>There are some differences of opinion as to which assignment operator to use when it comes to = vs &lt;-. Some believe that = is more clear.\u00a0The &lt;- operator maintains backward compatibility with S.\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/Rguide.xml\">Google&#8217;s R Style Guide<\/a> recommends using the &lt;- assignment operator, which seems to be a pretty decent reason as well. When all is said and done, though, it is like many things in programming, it depends on what your team does.<\/p>\n<p>R is a great language<img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=1461471370\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> for data analysis. If you&#8217;re interested in learning how to use R to explore data in a <a href=\"https:\/\/www.mongodb.com\">MongoDB<\/a> database, please check out <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/new-r-driver-option-mongodb-3-6\/\">this blog post<\/a> I wrote. There are many uses for it, and knowing a bit about the assignment operators and which one to use should help tremendously.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F909&#038;t=Assignment%20Operators%20in%20R%20%E2%80%93%20Which%20One%20to%20Use%20and%20Where&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F909&#038;p&#091;images&#093;&#091;0&#093;=%2F%2Fir-na.amazon-adsystem.com%2Fe%2Fir%3Ft%3Dkenwalgersite-20%26amp%3Bl%3Dam2%26amp%3Bo%3D1%26amp%3Ba%3D013454692X&#038;p&#091;title&#093;=Assignment%20Operators%20in%20R%20%E2%80%93%20Which%20One%20to%20Use%20and%20Where\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F909&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F909&#038;title=Assignment%20Operators%20in%20R%20%E2%80%93%20Which%20One%20to%20Use%20and%20Where\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F909&#038;title=Assignment%20Operators%20in%20R%20%E2%80%93%20Which%20One%20to%20Use%20and%20Where\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Assignment%20Operators%20in%20R%20%E2%80%93%20Which%20One%20to%20Use%20and%20Where&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F909\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>Whenever you start learning a new programming language, you must get accustomed to the language&#8217;s syntax. One of the first operators you&#8217;d expect to come across is the assignment operator for the language. Assignment operators are used to, well, assign values to variables. The R language has a few different ways to assign values. Let&#8217;s &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/uncategorized\/assignment-operators-r\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Assignment Operators in R \u2013 Which One to Use and Where&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"0","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1188,1],"tags":[1666],"yst_prominent_words":[1439,1433,1430,1437,1441,1434,1448,1431,1432,1442,1436,345,878,1438,1440,1435,1446,1447,625,1444],"class_list":["post-909","post","type-post","status-publish","format-standard","hentry","category-r-language","category-uncategorized","tag-assignment-operators","pmpro-has-access"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-eF","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":6,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"predecessor-version":[{"id":1194,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/909\/revisions\/1194"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=909"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}