{"id":75632,"date":"2020-01-21T12:45:07","date_gmt":"2020-01-21T07:15:07","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75632"},"modified":"2020-01-21T12:45:07","modified_gmt":"2020-01-21T07:15:07","slug":"r-numeric-and-character-functions","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/","title":{"rendered":"Numeric and Character Functions in R &#8211; Explore Inbuilt Functions with Examples"},"content":{"rendered":"<p>In this R tutorial, we will learn about some built-in numeric functions and character functions that R provides. We will see their syntaxes and usages. We will also look at examples of these numeric and character functions in R.<\/p>\n<h2>What are Numeric Functions?<\/h2>\n<p>There are functions in R, that perform operations on specific data types. The functions that take a numeric value or vector as input or return them as outputs are called <strong>numeric functions<\/strong>.<\/p>\n<p>In this tutorial, we are going to be looking at the following numeric functions:<\/p>\n<ol>\n<li>is.numeric() and as.numeric() functions<\/li>\n<li>abs() function<\/li>\n<li>ceiling() and floor() functions<\/li>\n<li>exp() function<\/li>\n<li>log() function<\/li>\n<li>round() function<\/li>\n<li>sqrt() function<\/li>\n<li>trigonometric functions<\/li>\n<li>factorial() function<\/li>\n<li>sign() function<\/li>\n<\/ol>\n<h3>1. is.numeric() and as.numeric() Functions<\/h3>\n<p>The <strong><code>is.numeric()<\/code><\/strong> function takes an object as an input argument and returns <strong><code>TRUE<\/code><\/strong> if the elements are of numeric type. For example:<\/p>\n<p><em>But to implement functions thoroughly you must have clear understanding of <a href=\"https:\/\/techvidvan.com\/tutorials\/r-vector\/\"><strong>vectors in R<\/strong><\/a>.<\/em><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">numvec &lt;- c(11,23,52,12,4,34,27,45)\nis.numeric(numvec)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-is.numeric.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75641\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-is.numeric.png\" alt=\"is.numeric() - numeric and character functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p>The <strong><code>as.numeric()<\/code><\/strong> function <strong>coerces<\/strong> the input values to the <a href=\"https:\/\/techvidvan.com\/tutorials\/r-data-types\/\"><strong>numeric data type<\/strong><\/a>. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">intvec &lt;- c(1L,2L,3L,4L)\ncharvec &lt;- c(\"a\",\"b\",\"c\",\"d\")\nlogvec &lt;- c(T,F,T,F,F,T,T,T)\ncompvec &lt;- c(12+3i,6i,5+2i,4+9i)\nas.numeric(intvec)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] 1 2 3 4<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">as.numeric(charvec)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] NA NA NA NA<br \/>\n<span style=\"color: #ff0000\">Warning message:<\/span><br \/>\n<span style=\"color: #ff0000\">NAs introduced by coercion<\/span><\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">as.numeric(logvec)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] 1 0 1 0 0 1 1 1<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">as.numeric(compvec)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p>[1] 12 0 5 4<\/p>\n<p><span style=\"color: #ff0000\">Warning message:<\/span><br \/>\n<span style=\"color: #ff0000\">imaginary parts discarded in coercion<\/span><\/p>\n<\/div>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-as.numeric.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75642\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-as.numeric.png\" alt=\"as.numeric() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Note<\/strong>: When characters are coerced into numeric data type, the result is <strong><code>NA<\/code><\/strong>. When complex values are coerced into numeric, their imaginary parts are removed.<\/p>\n<h3>2. abs() Function<\/h3>\n<p>The <strong><code>abs()<\/code><\/strong> function returns the absolute value for the input numeric value. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">abs(-3.14)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">abs(c(14.4,-55.5,34.76,45.4,-4.34,-5.56,-45.13))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-abs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75643\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-abs.png\" alt=\"abs() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>3. ceiling() and floor() Functions<\/h3>\n<p>The <strong><code>ceiling()<\/code><\/strong> function returns the lowest number that is greater than the input numeric value. The <strong><code>floor()<\/code><\/strong> function returns the largest number that is smaller than the input value. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">ceiling(c(14.4,-55.5,34.76,45.4,-4.34,-5.56,-45.13))<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">floor(c(14.4,-55.5,34.76,45.4,-4.34,-5.56,-45.13))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-ceiling-and-floor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75645\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-ceiling-and-floor.png\" alt=\"numeric and character functions in r - ceiling() and floor()\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>4. exp() Function<\/h3>\n<p>The <strong><code>exp()<\/code><\/strong> function returns the exponential value \u2018e\u2019 raised to the power of the input numeric value. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">exp(5)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-exp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75646\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-exp.png\" alt=\"exp() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>5. log() Function<\/h3>\n<p>The <strong><code>log()<\/code><\/strong> function returns the logarithmic value for the input numeric values. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">log(14)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">log(14,base=2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-log.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75647\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-log.png\" alt=\"log() - numeric and character functions in r \" width=\"1300\" height=\"739\" \/><\/a><\/strong><\/p>\n<h3>6. round() Function<\/h3>\n<p>The <strong><code>round()<\/code><\/strong> function rounds the given input numeric values to the number of specified digits. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">round(4.55231,digits=2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-round.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75648\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-round.png\" alt=\"round() - numeric and character functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>7. sqrt() Function<\/h3>\n<p>The <strong><code>sqrt()<\/code><\/strong> function returns the square root of the input numeric value. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sqrt(225)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sqrt(9)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sqrt(37)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-sqrt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75649\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-sqrt.png\" alt=\"sqrt() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>8. Trigonometric Functions<\/h3>\n<p>The trigonometric functions like <strong><code>cos()<\/code><\/strong>, <strong><code>sin()<\/code><\/strong>, <strong><code>tan()<\/code><\/strong>, etc. return the values of the trigonometric functions for the given input values. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sin(34)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cos(65)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">tan(23)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-trigonometric-functions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75650\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-trigonometric-functions.png\" alt=\"trigonometric functions - numeric and character functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>9. factorial() Function<\/h3>\n<p>The <strong><code>factorial()<\/code><\/strong> function returns the factorial values of the given input numeric value. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">factorial(5)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">factorial(13)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-factorial.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75651\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-factorial.png\" alt=\"factorial() - numeric functions in R\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>10. sign() Function<\/h3>\n<p>The <strong><code>sign()<\/code><\/strong> function returns <strong><code>-1<\/code><\/strong> if the input argument is negative in nature and <strong><code>+1<\/code><\/strong> if the number is positive. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sign(-5)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sign(10)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-sign.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75652\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-sign.png\" alt=\"sign() - numeric functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h2>What are Character Functions?<\/h2>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/r-vector-functions\/\"><strong>Vector Functions<\/strong><\/a> in R that take a character value or vector as input or return them as output are called <strong>character functions<\/strong>.<\/p>\n<p>Here are a few character functions that we are going to study today:<\/p>\n<ol>\n<li>is.character() and as.character() functions<\/li>\n<li>substr() function<\/li>\n<li>strsplit() function<\/li>\n<li>toupper() and tolower() functions<\/li>\n<li>paste() function<\/li>\n<li>nchar() function<\/li>\n<li>sub() function<\/li>\n<li>abbreviate() function<\/li>\n<li>trimws() function<\/li>\n<li>grep() function<\/li>\n<\/ol>\n<h3>1. is.character() and as.character() Functions<\/h3>\n<p>The <strong><code>is.character()<\/code><\/strong> function returns <strong><code>TRUE<\/code><\/strong> if the input value or object is of the character data type. The <strong><code>as.character()<\/code><\/strong> function converts the input values into the character data type. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">is.character(\"hello\")<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">is.character(c(\"this\",\"is\",\"a\",\"character\",\"vector\"))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-is.character.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75653\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-is.character.png\" alt=\"is.character() - character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">as.character(1234)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">is.character(c(1,2,3,4,5,5))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-as.character.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75654\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-as.character.png\" alt=\"as.character() - numeric and character functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>2. substr() Function<\/h3>\n<p>The <strong><code>substr()<\/code><\/strong> function extracts a substring from a given string based on the <strong><code>start<\/code><\/strong> and <strong><code>stop<\/code><\/strong> arguments. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">substr(\"this is a string\",start=3,stop=12)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-substr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75656\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-substr.png\" alt=\"substr() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><em>Any queries in R numeric and character functions article till now? Ask <strong>TechVidvan<\/strong> experts in comments.<\/em><\/p>\n<h3>3. strsplit() Function<\/h3>\n<p>The <strong><code>strsplit()<\/code><\/strong> function splits a string or vector of strings based on a regular expression or a fixed string. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">strsplit(\"this is a string\",split=\" \")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-strsplit-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75657\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-strsplit-.png\" alt=\"strsplit() - character functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>4. toupper() and tolower() Functions<\/h3>\n<p>The <strong><code>toupper()<\/code><\/strong> function in <a href=\"https:\/\/www.r-project.org\/\">R<\/a> converts the input string or strings to the upper-case. The <strong><code>tolower()<\/code><\/strong> converts them to the lower-case. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">strings &lt;- c(\"these\",\"strings\",\"are\",\"in\",\"lower-case\")\ntoupper(strings2)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">strings2 &lt;- c(\"THIS\",\"IS\",\"A\",\"CHARACTER\",\"VECTOR\")\ntolower(strings)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-toupper-and-tolower.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75658\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-toupper-and-tolower.png\" alt=\"toupper() and tolower() - character functions in r\" width=\"1299\" height=\"742\" \/><\/a><\/strong><\/p>\n<h3>5. paste() Function<\/h3>\n<p>The <strong><code>paste()<\/code><\/strong> function converts vectors into characters (if needed), and <strong>concatenates<\/strong> them into a character string or character vector. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">x1_5 &lt;- paste(\"x\",1:5,sep=\"_\")\nx1_5<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">coll_strings &lt;- paste(strings,collapse=\" \")\n&gt; coll_strings<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-paste.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75659\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-paste.png\" alt=\"paste() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<p><strong>Note<\/strong>: In the first example, the <strong><code>paste()<\/code><\/strong> function coerces the numeric values to characters and concatenates them to x with the <strong><code>sep<\/code><\/strong> between them. In the second example, the <strong><code>paste()<\/code><\/strong> function collapses the elements into a single string with the <strong><code>collapse<\/code><\/strong> argument between them.<\/p>\n<h3>6. nchar() Function<\/h3>\n<p>The <strong><code>nchar()<\/code><\/strong> function counts the number of characters in a string. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">nchar(coll_strings)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-nchar.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75660\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-nchar.png\" alt=\"nchar() - character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>7. sub() Function<\/h3>\n<p>The <strong><code>sub()<\/code><\/strong> function replaces the <strong>first instance<\/strong> of a substring from a given string. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">strings<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">sub(\"lower-case\",\"a vector\",strings)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-sub.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75661\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-sub.png\" alt=\"sub() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>8. abbreviate() Function<\/h3>\n<p>The <strong><code>abbreviate()<\/code><\/strong> function abbreviates the strings inside the given character vectors. We can specify the minimum length of the abbreviations and also whether or not to use dots for each abbreviation. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">countries &lt;- c(\"greenland\",\"united kingdom\",\"united states\",\"india\",\"japan\")\n&gt; countries_abb &lt;- abbreviate(countries)\n&gt; countries_abb<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-abbreviate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75662\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-abbreviate.png\" alt=\"abbreviate() - numeric and character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>9. trimws() Function<\/h3>\n<p>The <strong><code>trimws()<\/code><\/strong> function removes leading or trailing whitespaces in a string. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">stringws &lt;- \" hello \"\n&gt; stringws &lt;- trimws(stringws)\n&gt; stringws<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-trimws.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75663\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-trimws.png\" alt=\" trimws() -character functions in r\" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h3>10. grep() Function<\/h3>\n<p>The <strong><code>grep()<\/code><\/strong> function searches for a specific pattern of string in each element of a character vector. It returns the <strong>index<\/strong> of the elements containing the target string or pattern. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">strings<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">grep(\"a\",strings)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-grep.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75664\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/numeric-and-character-functions-in-r-grep.png\" alt=\"grep() - numeric and character functions in r \" width=\"1299\" height=\"741\" \/><\/a><\/strong><\/p>\n<h2>Summary<\/h2>\n<p>Today, we learned about various useful <a href=\"https:\/\/techvidvan.com\/tutorials\/r-functions\/\"><strong>R functions<\/strong><\/a> that take inputs or return outputs as numeric or character values or vectors. These functions are important for calculations or string manipulations. They help us in performing operations on numeric or character variables in datasets.<\/p>\n<p>After reading this <strong>TechVidvan<\/strong> tutorial, you should be able to use the above-listed functions. All you need is practice.<\/p>\n<p>Any difficulty while practicing numeric and character functions in R programming?<\/p>\n<p>Ask our <strong>TechVidvan<\/strong> experts.<\/p>\n<p>Keep practicing!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this R tutorial, we will learn about some built-in numeric functions and character functions that R provides. We will see their syntaxes and usages. We will also look at examples of these numeric&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75673,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1403,1404,1405,1406,1407,1408,1409,1410],"class_list":["post-75632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-grep-r","tag-log-r","tag-numeric-and-character-functions-in-r","tag-r-built-in-functions","tag-r-paste","tag-r-round","tag-r-sign","tag-r-strsplit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Numeric and Character Functions in R - Explore Inbuilt Functions with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn some built-in numeric functions and character functions in R like log(), round(), grep(), sign(), paste() etc with examples, syntaxes and usages.\" \/>\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-numeric-and-character-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Numeric and Character Functions in R - Explore Inbuilt Functions with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn some built-in numeric functions and character functions in R like log(), round(), grep(), sign(), paste() etc with examples, syntaxes and usages.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/\" \/>\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-21T07:15:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/numeric-and-character-functions-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=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Numeric and Character Functions in R - Explore Inbuilt Functions with Examples - TechVidvan","description":"Learn some built-in numeric functions and character functions in R like log(), round(), grep(), sign(), paste() etc with examples, syntaxes and usages.","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-numeric-and-character-functions\/","og_locale":"en_US","og_type":"article","og_title":"Numeric and Character Functions in R - Explore Inbuilt Functions with Examples - TechVidvan","og_description":"Learn some built-in numeric functions and character functions in R like log(), round(), grep(), sign(), paste() etc with examples, syntaxes and usages.","og_url":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-21T07:15:07+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/numeric-and-character-functions-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":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Numeric and Character Functions in R &#8211; Explore Inbuilt Functions with Examples","datePublished":"2020-01-21T07:15:07+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/"},"wordCount":894,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/numeric-and-character-functions-in-R.jpg","keywords":["grep() R","log() R","Numeric and Character Functions in R","R Built-in Functions","R paste()","R round()","R sign()","R strsplit()"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/","url":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/","name":"Numeric and Character Functions in R - Explore Inbuilt Functions with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/numeric-and-character-functions-in-R.jpg","datePublished":"2020-01-21T07:15:07+00:00","description":"Learn some built-in numeric functions and character functions in R like log(), round(), grep(), sign(), paste() etc with examples, syntaxes and usages.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/numeric-and-character-functions-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/numeric-and-character-functions-in-R.jpg","width":802,"height":420,"caption":"numeric and character functions in R"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/r-numeric-and-character-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Numeric and Character Functions in R &#8211; Explore Inbuilt Functions with Examples"}]},{"@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\/75632","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=75632"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75632\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75673"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}