Basic Syntax in R

There are two ways to write code in RStudio:- first, in the command prompt and, second, in the R script file. We will go through both one by one.

Hello World Program in R

  • Launch RStudio
  • In the command prompt window, type the following and press Enter
>print(“Hello, World!”)
Output:
[1] "Hello, World!"

R Programs are usually written in R scripts and then executed in the console window

  • Create a new R script file
  • Write the following code, select the block of code you want to run, and then press Ctrl+Enter or click on ‘Run’
mystring = “Hello, World!”
print(mystring)
Output:
[1] "Hello, World!"

Variables in R Programming

A variable is a name given to a memory location, which is used to store values in a computer program. Variables in R programming can be used to store numbers (real and complex), words, matrices, and even tables. R is a dynamically programmed language which means that unlike other programming languages, we do not have to declare the data type of a variable before we can use it in our program.
For a variable to be valid, it should follow these rules

  • It should contain letters, numbers, and only dot or underscore characters.
  • It should not start with a number (eg:- 2iota)
  • It should not start with a dot followed by a number (eg:- .2iota)
  • It should not start with an underscore (eg:- _iota)
  • It should not be a reserved keyword.

Wish to get certified in R! Learn R Programming from top R Programming experts and excel in your career with Intellipaat’s R Programming certification!

Reserved Keywords in R

Following are the reserved keywords in R
forinrepeatwhilefunction
ifelsenextbreakTRUE
FALSENULLInfNaNNA
NA_integer_NA_real_NA_complex_NA_character_

NA:– Not Available is used to represent missing values.
NULL:- It represents a missing or an undefined value.
NaN:– It is a short form for Not a Number(eg:- 0/0).
TRUE/FALSE: – These are used to represent Logical values.
Inf :– It denotes Infinity(eg:- 1/0).
If else, repeat, while, function, for, in, next, and break:– These are Used as looping statements, conditional statements, and functions.
…  :- It is used to pass argument settings from one function to another.
NA_integer_, NA_real_, NA_complex_, and  NA_ character _:- These represent missing values of other atomic types.
 For example:

  • x = 15 implicitly assigns a numeric data type to the variable ‘x’.
  • mystring = “Hello, World!”

Check out the top R Programming Interview Questions to learn what is expected from R professionals!

Constants in R

The entities whose values are fixed are called constants.
There are two types of constants in R:

  • Numeric Constants: All numeric values such as integer, double, or complex fall under this category. Numeric constants followed by ‘L’ and ‘i’ are considered as integer and complex respectively. And, numeric constants preceded by 0x/0X are treated as hexadecimal numbers.
  • Character Constants: These constants are represented by single (‘) or double (“) quotes called delimiters.

Data Types in R Programming

The fundamental/atomic data types in R programming are as follows:

  • Numeric
  • Integer
  • Complex
  • Character
  • Logical

Elements of these R programming data types are often combined to form data structures.

Visit our R programming Community to get answers to all your queries!

Numeric Data Type

In R, if we assign any decimal value to a variable it becomes a variable of a numeric data type.
For example, the statement below assigns a numeric data type to the variable “x”.

x = 45.6

And, the following statement is used to print the data type of the variable “x”:

class(x)
Output:- [1] "numeric"

Integer Data Type

To create an integer variable in R, we need to call the (as.Integer) function while assigning value to a variable.
For example:-

e = as.integer(3)
class(e)
Output: [1] "integer"

Another way of creating an integer variable is by using the L keyword as follows:

x = 5L
class(x)
Output: [1] "integer"

 

Are you interested in learning R Programming from experts? Enroll in our R Programming Course in Bangalore now!

Complex Data Type

The values containing the imaginary number ‘i’ (iota) are called complex values.
The following code gives an error when run:

sqrt(−1)
Output:[1] NaN
Warning message:
In sqrt(−1) : NaNs produced

To overcome this error, we coerce the value (−1) into a complex value and denote it as ‘i’.

sqrt(as.complex(−1)
Output:[1] 0+1i

For example:

z = 2 + 3i

Watch this R Programming for Beginners Video Tutorials

Character Data Type

This data type is used to represent strings.
For example:

str1 = "Sam"
class(str1)
Output: [1] "character"

We can also use the as.character() function to convert objects into character values.
For example:

x = as.character(55.7)
print(x)
Output:[1] "55.7"
class(x)
Output:[1] "character"

 

Looking for R ProgrammingTraining Course? Enroll now!

Logical Data Type

A logical data type stores either of the two values: TRUE -/FALSE. A logical value is often generated when two values are compared.
For example:-

x = 3
y = 5
a = x > y
a
Output:
FALSE

Three standard logical operations,i.e., AND(&), OR(|), and NOT(!) yield a variable of the logical data type.
For example:-

x= TRUE; y = FALSE
x & y
Output:
[1] FALSE
x | y
Output:
[1] TRUE
!x
Output:
[1] FALSE

In this tutorial, we covered the basic syntax to writing a program in R, along with learning what a variable is and how to define a variable we also discussed how to store data with different data types in r programming. Next, we are going to talk about R Programming Operators.

Recommended Videos

Leave a Reply

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