R Programming

R Programming

A Language for Statistical analysis - Part 2

We went through what is R, Data types in R with examples in the previous blog. In this blog. We will look at variables and their scopes, essential methods, input and output and output methods.

Variables in R

A variable is used to store a particular data type and a memory is allocated for the same. A single variable stores data belonging to a certain datatype. The variables in the R language are not declared with a data type. Instead, they take the data type of the R-object assigned to them.

Variable declaration and initialization in R language:

Ways to assign values to variables:

  • Using '=' operator.

  • Using '->' operator

  • Using '<-' operator


//using '=' operator

var = "Quasar"

print (var)



//using '->' operator

"Quasar" -> var1  

print (var1)



//using '<-' operator

var2 <- "Quasar"

print (var2)



//output

"Quasar"

"Quasar"

"Quasar"

Scope of Variables:

-Global Variables: These variables remain throughout the program. This variable can be accessed from any region of the code. This type of variable is usually declared outside all the functions and can be accessed by all the functions within the program. The lifespan of the variable is same, as the program. To declare the global variable, the keyword Global is used.

-Local Variables: These variables are available in the specific part of the program where they are declared, which is inside a function or control statement. Local variables are declared inside an action block.


global_var = "Quasar Community"



m = function()

{

        var = 10

        print(global_var)

        print(var)

}



//output

"Quasar Community"

10

As we can the global variable is accessed by the function and also throughout the program. Local variable is declared inside the function and executed successfully.

Important functions for variables

class() function

This function determines the data type of a particular variable. The variable is passed as an argument and it returns the data type.

Syntax:

class(varaibale_Name)


var= "Quasar"

class(var)



//output

"character"

Is() function

This function is used to know all the defined variables in the program, This comes in handy if someone wants to see the number of variables in a large program.

Syntax:

Is()


var= "Quasar"

var1= "Community"



print(Is())



//output

"var" "var1"

rm() function

This inbuilt function is used to clear or delete a variable within the program. This is useful for clearing the memory space allocated for the variable, thus providing extra space for new variables. The name of the variable is passed as an argument to the built-in function.

Syntax:

rm(variable_name)


var= "Quasar"

var1= "Community"



rm(var)

print(var)



//output

Error

Input Statements

Programmers need input from the users to process efficiently. The data is collected and operated and the output is provided.

In R the input is obtained by two methods:

  • Using readline() function

  • Using scan() function

Using readline() function :

The readline() function obtains input in string format. So we should convert the obtained input to various required datatypes. To convert the obtained data to other datatypes there are various functions:

  1. as.integer(n); - converts to integer

  2. as.numeric(n); - converts to numeric type ex: float,double,etc

  3. as.complex(n); - converts the data to complex number

  4. as.Date(n); - converts the data to date

syntax:

var=readline();

var->as.integer(var)


var = readline();

var=as.integer(var);



print(var)



//output

300 - entered by the user

300 - printed by the compiler

There are many different types of variations in the function. We can take a look at the same in the next blog. Stay tuned for the upcoming blogs.