R Variables, Constants and Reserved Words

Facebooktwitterredditpinterestlinkedinmailby feather

R Variables

Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store data.

A variable in R is used to store an atomic vector, group of atomic vectors or a combination of many R-objects.

  • A valid variable name can be a combination of numbers, letters, and the dot or underline character.
  • A valid variable name must start with a letter or a dot and if it starts with a dot, it cannot be followed by a number.
  • Reserved words in R cannot be used as a variable name. 

Variable Name

Validity

total

Valid

Sum

Valid

.this.is.ok

Valid

this _is_acceptable

Valid

tot@l

Invalid

var_name%

Invalid

_var_name

Invalid

5um

Invalid

TRUE

Invalid

We can separate the variables by the types of values they contain:

Boolean Variables: This is the simplest type of variable. It contains a single bit, and indicate a binary result (0 and 1, yes and no, or true and false).

Examples of Boolean variables are:

Integer variables: Numbers with no floating point are called integers. In R programming, sometimes it is difficult to declare a single integer. In most cases, try to do so will actually declare a numeric value.

Numeric Variables: Numeric variables are used to store numbers. It can contain floating point numbers.

Examples of Numeric variables are:

Characters Variables:  Character variables are used to store non-numeric data. Unlike other programming languages, there are a no differences between characters and strings in R.  Characters are assigned using quotation mark. R does not care if you use double or single quotes. Examples are:

String Variables:  String variables are those variables which contain one or more characters.  Examples are:

How to Create, Rename and Recode variables in R

In R, it is simple task to create a new variable or to convert an old variable into a new one.

Let’s understand these all operations of variable through the given example:

First of all create a data set:

Here, the name of dataset is df. When you run the above command, the output will be:

Now we will create a new variable called totalcost as shown below:

Let’s see the dataset again:

Now, if we want to rename and recode a variable “costs” to “costs_rs” then:

Now the data set is:

We can also delete the variable through the NULL command, if we want to delete the costs variable then:

Then the dataset will be changed:

Now, let’s see that how to recode a variable “patients”. Here we use ifelse(), but you can use other functions as well:

df$patients <- ifelse(df$patients==200, 100, ifelse(df$patients==250, 300, NA))

Let’s see the dataset again:

R Constants

Constants are the entities whose value cannot be changed. Basically there are two types of constants:

2. Numeric Constants: numeric constants are the numbers which can be integer, double or complex. You can check the type of constant through the typeof() function. Numeric constant suffix with L are the integer type and suffix with i are called complex type.

Example:

2. Character Constant: Character constant can be declared using either single quote (‘ ’) or double quote (“ ”).

Built-in Constants: Some of the built-in constants of R along with their values are shown below:

R Reserved Words

The set of words that have special meaning and cannot be used as an identifier (variable name, function name, etc.) are called Reserved words.

Following is the list of reserved words in R:

Reserved Words

Purpose

else

Conditional statements

repeat

Loop

While

Loop

function

User defined function

for

Loop

in

Loop

break

Loop

If

Conditional statements

TRUE

Logical constant

FALSE

Logical constant

NULL

Represents the absence of a value or an undefined value

Inf

Infinity

NaN

“Not a number”, e.g. when 0 is dived by 0

NA

Not Available

This list of reserved words can be viewed by the command help (reserved) or ?reserved.

Note: Since R is a case sensitive language therefore TRUE and True are not the same. Here, TRUE is a reserved word and True can be a variable name.

Reference:

https://www.datamentor.io/r-programming/variable-constant/
https://intellipaat.com/tutorial/r-programming/basic-syntax-data-types-and-variables/

Facebooktwitterredditpinterestlinkedinmailby feather