## Search this blog

Showing posts with label R Variables. Show all posts
Showing posts with label R Variables. Show all posts

## Monday 13 August 2018

### R Decision Making - IF...ELSE Statement in R Programming

We can write a simple IF statement, IF...ELSE statements or nested IF statements as per our requirement. If you know the basics of any other programming language then after this tutorial you'll get an idea about writing conditional statements in R programming.

Flow Chart of IF...ELSE is like this

Basic Syntax of IF-ELSE is like this in R

```IF (Condition 1){
Statement to execute (if condition 1 is true)
}ELSE IF (Condition 2){
Statement to execute (if condition 2 is true)
}ELSE
Statement to execute (if condition 1& 2 both are false)
```

For a better understanding of decision-making, statements see this example

```> #Declare Variables
> a <- 20
> b <- 40
> c <- 5
>
> #Check conditions
> if(a>b && a>c){
+ print("Largest Value is A")
+
+ }else if(b>a && b>c){
+ print("Largest Value is B")
+
+ }else print("Largest Value is C-")
[1] "Largest Value is B"
>
```

This is how IF…ELSE works in R programming.

Cheers ðŸ™‚ Happy Learning

## Saturday 28 July 2018

### R Variables and Constants - Variable Assignment, Search & Delete

Variables are the name given to a piece of data or information. Like many other programming languages, we use R variables to store data. R supports numeric, string, boolean and many other data types and we do not declare a variable with the data type, instead of that we assign a value to the variable and on basis of value R automatically sets the data type of variable.

A variable name consists of characters, numbers, and the special character (dot(.) and underscore(_) only) and can not start with any number.

See an example of variable naming in R programming

```#Variables Naming in R Programming

#Valid Varibale Name
varone <- 20
print(varone)

#Valid Variable Name
.var_two <-30
print(.var_two)

#Invalid Variable Name
12months <- 12

#Invalid Variable Name (as dot is followed by number)
.1Var <- "Ashish"
```

The output on R Console

## Variable Assignment in R

In R programming, the value of a variable can be assigned using the left arrow, right arrow or equal to the operator and the data type of a variable can be changed multiple times in a program that depends on its value.

```# Assignment using  using left arrow
var1 <- 5
print(var1)

# Assignment using  using right arrow
"Ashish Awasthi" -> var2
print(var2)

# Assignment using equal to operator
var3 = FALSE
print(var3)
```

## Data type of a Variable

This is how we can check the data type of any variable

```# Declare Different types of Variables
var1 <- 5
print(var1)
"Ashish Awasthi" -> var2
print(var2)
var3 = FALSE
print(var3)

# Check class of var1
class(var1)

# Check class of var2
class(var2)

# Check class of var3
class(var3)
```

## Searching Variable

We can find all variable available in the workspace using ls() function, and we can also use pattern in ls function to find the specific variable.

See an example here

```#Declare some variables
var <- 10
var1 <- "Ashish Awasthi"
var2 <- 20.5
test_var <- TRUE

#Print all variables present in workspace
ls()

#Search specific variable
ls(pattern="test")
```

The output on R Console

## Deleting a Variable

If you want to delete a variable that is no longer needed then there is rm() function, that removes variable from the workspace.

See an example

```#Declare some variables
var <- 10
var1 <- "Ashish Awasthi"
var2 <- 20.5
test_var <- TRUE

#Delete a variable
rm(var2)

#Now print that variable
print(var2)
```

The output on R Console

## Constants in R

As the name suggests constant means, an entity that value can not be changed. Constants can be numeric, character, boolean etc. All numbers are numeric constants, we can check it's using typeof() function.

```#Numeric Constants
typeof(10)

#Character Constants
typeof("Ashish Awasthi")

#Buil-in Constants
print(pi)
typeof(pi)

print(LETTERS)
typeof(LETTERS)

print(letters)
typeof(letters)

print(month.name)
typeof(month.name)
```

Cheers :) Happy Learning