{"id":75541,"date":"2020-01-16T12:15:58","date_gmt":"2020-01-16T06:45:58","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75541"},"modified":"2020-01-16T12:15:58","modified_gmt":"2020-01-16T06:45:58","slug":"r-arguments","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/","title":{"rendered":"Arguments in R &#8211; Give more Power to your R Functions"},"content":{"rendered":"<p><strong>This TechVidvan tutorial is designed to give you a practical understanding of how to deal with arguments in R programming.<\/strong><\/p>\n<p>Functions are handy features in any programming language. They make life easier and reduce redundancy in programs. Yet, without the arguments passed to functions their usefulness is reduced to trivial.<\/p>\n<p>In this R tutorial, we will talk about the role R arguments play in programming. We will learn how they are passed to functions and how they enhance the working of functions in R. So, let get going!<\/p>\n<h2>What are Arguments in R Programming?<\/h2>\n<p>Arguments are <strong>inputs<\/strong> that a function requires. They are named while defining a function. Arguments are optional, you only need to define them if the function requires any. A function can have multiple arguments.<\/p>\n<p>The names of the arguments are optional in a function call. When calling a function, you only need to give their <strong>values<\/strong> in the order they are in, in the function\u2019s <strong>definition<\/strong>.<\/p>\n<p>We can also provide default values for arguments in the function definition. The function uses the <strong>default value<\/strong> of the arguments in case they are not provided in the function call.<\/p>\n<p>To start working with R arguments you must have a clear understanding of <a href=\"https:\/\/techvidvan.com\/tutorials\/r-functions\/\"><strong>functions in R<\/strong><\/a><\/p>\n<h3>A Function with an Argument<\/h3>\n<p>We can pass an argument to a function when we <strong>call<\/strong> that function. We just need to give the value of the argument inside the parenthesis after the function\u2019s name. The following is the example of a function with a single argument. The function takes a numeric input and checks whether it is divisible by 3 or not.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">div &lt;- function(int){\n  if(int%%3==0){\n    return(\"the number is divisible by 3\")\n  } else {\n    return(\"the number is not divisible by 3\")\n  }\n}\ndiv(6)\ndiv(7)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-single-argument-div.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75551\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-single-argument-div.png\" alt=\"single argument div() - R arguments \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>Functions with Multiple Arguments<\/h3>\n<p>Functions can have <strong>multiple arguments<\/strong>. While calling the function, we can either give the values to the arguments with their names or simply give them in the order they are in the definition of the function.<\/p>\n<p>In the above example, we check whether a number is divisible by 3 or not. Imagine that we want to check whether it is divisible by another number instead. We can do this by providing the second number as a second argument. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">div &lt;- function(int, divisor){\n  if(int%%divisor==0){\n    return(paste(int,\"is divisible by\",divisor,sep=\" \"))\n  } else {\n    return(paste(int,\"is not divisible by\",divisor,sep=\" \"))\n  }\n}\ndiv(6,3)\ndiv(32,4)\ndiv(9,2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-multiple-arguments-div.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75552\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-multiple-arguments-div.png\" alt=\"multiple R arguments div()\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>Arguments with Default Values<\/h3>\n<p>We can provide default values for arguments in the function\u2019s definition. This value is used in case a new value is not provided in the function call.<\/p>\n<p>We can provide a <a href=\"https:\/\/cran.r-project.org\/doc\/manuals\/r-release\/R-intro.html#Named-arguments-and-defaults\">default<\/a> value for the divisor argument in case the divisor is not provided in the call. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">div &lt;- function(int, divisor=3){\n  if(int%%divisor==0){\n    return(paste(int,\"is divisible by\",divisor,sep=\" \"))\n  } else {\n    return(paste(int,\"is not divisible by\",divisor,sep=\" \"))\n  }\n}\ndiv(6,2)\ndiv(9)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-default-value-for-arguments-div.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75553\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-default-value-for-arguments-div.png\" alt=\"default value for R arguments div()\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>Using the dots operator<\/h3>\n<p>The <strong><code>\u2026<\/code><\/strong>operator, technically known as the <strong>ellipsis<\/strong>, allows a function to take arguments that are not predefined in its definition. The ellipsis is useful when we don\u2019t know how many arguments a function may take. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cate &lt;- function(...){\n  l &lt;- list(...)\n  paste(l,collapse=\" \")\n}\ncate(9L,  5i, FALSE, \"hello\", \"these\", \"are\", \"separate\", \"arguments\", 3, TRUE)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-...operator-cate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75554\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-...operator-cate.png\" alt=\"dots operator cate() arguments in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>In the above example, we use the ellipsis operator to take a variable number of values. We convert them all to characters and then concatenate them into a single string.<\/p>\n<h3>Passing Functions as Arguments<\/h3>\n<h4>Passing default functions<\/h4>\n<p>We can also pass functions as arguments to other functions. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">proc &lt;- function(x,FUN){\n  return(FUN(x))\n}\nproc(c(5,6,7,8),sum)\nproc(c(2,3,4,5),mean)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-passing-functions-proc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75555\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-passing-functions-proc.png\" alt=\"passing functions proc() - R arguments\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h4>Passing user-defined functions<\/h4>\n<p>We can even pass user-defined functions as arguments to other functions<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">proc &lt;- function(x,FUN){\n  return(FUN(x))\n}\naddfive &lt;- function(a){\n  return(a+5)\n}\nproc(c(1,2,3,4,5),addfive)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-passing-user-defined-functions-proc-addfive.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75556\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-passing-user-defined-functions-proc-addfive.png\" alt=\"passing user-defined functions proc() addfive() - R arguments\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h4>Passing function with their arguments<\/h4>\n<p>We can use the <strong><code>\u2026<\/code><\/strong> operator along with other functions to pass arguments that they might need. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">proc &lt;- function(x,FUN,...){\n  return(FUN(...,x))\n}\nproc(c(\"these\",\"are\",\"separate\",\"elements\"),paste\n,collapse=\" \")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-passing-functions-with-...-operator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75557\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-in-r-passing-functions-with-...-operator.png\" alt=\"passing functions with dots operator - R arguments\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Note<\/strong>: Here the <strong><code>collapse<\/code><\/strong> argument is received by the ellipsis operator. As the paste function uses the <strong><code>collapse<\/code><\/strong> argument, R automatically passes the argument to it.<\/p>\n<h2>Summary<\/h2>\n<p>In this R tutorial, we learned about arguments in R functions. We learned how to use them and how to pass them. We also learned about setting default values for them as well. We looked at functions that can be passed as arguments to other functions.<\/p>\n<p>Arguments make complex problem solving possible with the use of functions in R programming.<\/p>\n<p>Any difficulty working with R arguments? Ask us and our <strong>TechVidvan<\/strong> experts will be happy to help you.<\/p>\n<p>Keep Learning ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This TechVidvan tutorial is designed to give you a practical understanding of how to deal with arguments in R programming. Functions are handy features in any programming language. They make life easier and reduce&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75562,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1391,1392,1393,1394,1395,1061],"class_list":["post-75541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-arguments-in-r","tag-r-arguments","tag-r-dot-arguments","tag-r-function-arguments","tag-r-language-basics","tag-r-programming-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Arguments in R - Give more Power to your R Functions - TechVidvan<\/title>\n<meta name=\"description\" content=\"R arguments are optional, you only need to define them if the function requires any.In this R tutorial, learn the role arguments play in R programming.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/r-arguments\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Arguments in R - Give more Power to your R Functions - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"R arguments are optional, you only need to define them if the function requires any.In this R tutorial, learn the role arguments play in R programming.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/r-arguments\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-16T06:45:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/arguments-in-R.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Arguments in R - Give more Power to your R Functions - TechVidvan","description":"R arguments are optional, you only need to define them if the function requires any.In this R tutorial, learn the role arguments play in R programming.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/","og_locale":"en_US","og_type":"article","og_title":"Arguments in R - Give more Power to your R Functions - TechVidvan","og_description":"R arguments are optional, you only need to define them if the function requires any.In this R tutorial, learn the role arguments play in R programming.","og_url":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-16T06:45:58+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/arguments-in-R.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Arguments in R &#8211; Give more Power to your R Functions","datePublished":"2020-01-16T06:45:58+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/"},"wordCount":666,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/arguments-in-R.jpg","keywords":["Arguments in R","R Arguments","R dot Arguments","R function arguments","R language basics","R programming tutorial"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/r-arguments\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/","url":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/","name":"Arguments in R - Give more Power to your R Functions - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/arguments-in-R.jpg","datePublished":"2020-01-16T06:45:58+00:00","description":"R arguments are optional, you only need to define them if the function requires any.In this R tutorial, learn the role arguments play in R programming.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/r-arguments\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/arguments-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/arguments-in-R.jpg","width":802,"height":420,"caption":"R arguments"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/r-arguments\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Arguments in R &#8211; Give more Power to your R Functions"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75541"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75541\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75562"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}