CodeInfoNet

Learn programming on the go!

Home » Posts

Posts

Introduction to Vector in R

In this article I am going to write about the vector in R. Main objective of this article to get familiar with the concept of vectors in R programming language.

We will touch down the following topics:

-What is Vector?

-Creating our first vector in R

In previous articles we understood the loop structures and conditional statements. Also we have seen the practical implementation of the law of large numbers. You can always read those articles to refresh your knowledge before proceeding further.

Now let’s see what we have in this article.

Continue reading

Law of Large Numbers

In this article we are going to assess the law of large numbers. We are going to do the practical implementation to check how it actually works.


Our previous article was about various loops and conditional statements. In case you have missed it, here is the link while loop, for loop, conditional statements in R

In this short article we will see how actually the law works.

First of all, it is important to know what the law of large numbers is.

Law of Large Numbers:

According to the law, the average of the results obtained from a large number of trials should be close to the expected value, and will tend to become closer as more trials are performed.

You can read the full Wikipedia article about it here.

Below is the mathematical representation of law of large numbers.

Law of Large Number
Law of Large Number

Now, to implement it in R programming, we are going to consider few things. We will use the rnorm() function to get the normally distributed random numbers.

Continue reading

Looping structures And Conditional statements

In this article we are going to work on various looping structures and conditional statements. We will see while loop, for loop and various forms of if statement.

In previous article we have seen the logical operators. If you have missed it, here is the link: logical operators in R

Here we will see how does the while loop and for loop and if statement work. In R programming while loop is not actually used very much extensively. R has other ways of doing the same operations that while loop does. But it is always good to have the knowledge of things that exists.

Hence, the topics will be covered in this article are:

  1. While loop
  2. For loop
  3. If – else statement

  1. While loop

While loop is used when we want to execute some part of the code for particular time or till the particular condition is satisfied. As we discussed earlier, while loop is not used very much extensively as it is used in other programming languages. Still it is better to have the knowledge of it.

Syntax:

                while(condition)

                {

                                Body of the Loop

               }

Ex.:

a <- 1

b <- 20

while (a < b) # Condition to be checked

   {
      # Body of the loop
         print("While loop test")
         # increment the value of a to match the condition
         a <- a + 1
   } 
While loop example
While loop

Continue reading

Logical operators in R

In this article we are going to learn about the logical operators. I will cover the types of logical operators and how we can use them in our real life coding.

In previous article we have seen how to use variables in R programming. In case you have missed it, here is a link for the article using variables in R.

Let us know if the article is appropriate or if it needs any improvements. Mail us at admin@codeinfo.net or you can fill the feedback form at the end of this article.

Let’s kick off the topic!!


Logical operators in R

We have seen already that R programming have a data type as Logical. Now let’s see which logical operators R provides and how does they work.

In R programming language there are some logical operators, we are going to look at 10 of those.

  1. Equal to ==
  2. Not Equal to !=
  3. Less than <
  4. Greater than >
  5. Less than equal to <=
  6. Greater than equal to >=
  7. And &
  8. Or |
  9. Not !
  10. isTRUE()
  1. Equal to ==

This logical operator checks if the two values are equal. If they are equal it returns TRUE as a result

Ex.

equal <- 10 == 5

print(equal)
  1. Not Equal to !=

This logical operator also checks if the two values are equal. But it returns TRUE as a result if they are not equal

Ex.

notEqual <- 10 != 5

print(notEqual)
  1. Less than <

The less than ‘<’ operator gives result as true if the first value is less than the second value

Ex.

lessThan <- 5 < 10

print(lessThan)
  1. Greater than >

This operator will give True result when first value is greater than the second value

Ex.

greaterThan <- 5 > 10

print(greaterThan)

The advice here is to execute the given code snippets to get the better idea of these operators. Also, try using following two operators

  1. Less than equal to <=
  2. Greater than equal to >=
Logical operators
Logical operators
  1. And &

And operator gives the result as true only if both the values in an expression are TRUE.

Otherwise it returns false

Ex.

  x <- TRUE & TRUE

  x 
  1. OR |

Or operator returns true as a result if any of the value is true. It returns false only of both the values are FALSE

Ex.

  y <- TRUE | FALSE

  y
  1. Not !

Not operator is used to perform the negation of any  result.

If the not operator is applied to the result returning TRUE, it will return the result as FALSE and vice versa

Ex.

  not <- !(FALSE)

  print(not)
  1. IsTRUE()

This function is used to check if the result is true

Ex.

x <- 10 == 10

isTRUE(x)
Not operator
Not operator

Your feedback is important for us. Let us know if this article is helpful.

[wpforms id=”22″]

Using Variables In R

Overview

This blog is specifically for understanding the use of variables in R programming. After reading this blog you will be able to assign values, perform multiple arithmetic operations as well as understanding how to assign character values to variables.


Quick Recap

In previous article we learnt the data types in R programming. In case you have missed it, here is a link for the article Data types in R.

We covered the data types such as Int, double, char, complex and logical.

Let us know if the article is appropriate or if it needs any improvements. Mail us at admin@codeinfo.net or you can fill the feedback form at the end of this article.

In this article I am going to tell you about how to use variables to perform different arithmetic operations and storing values.

Note: Before continuing I would like to ask you to simultaneously perform the steps that I have given, because only reading the article won’t help. You must do the hands on in order to learn quickly. “Practice makes man perfect!!”

Let’s get started quickly!

Variables in R

In simple terms, variables are the entities which can change depending on the conditions, expressions. We can store values in the variables and we can perform different operations on them. Learn more about variables here

Some of the examples of operations that we can perform on variables are such as:

1. Arithmetic operations

2. Logical operations etc.

Let’s look at arithmetic operations:

Arithmetic Operations

Arithmetic Operations such as Addition, Subtraction, Division, Multiplication can be performed very easily with the variables in R.

1. Addition

Declare variables and store any value into them as given below:

a <- 20

b <- 10

Now if we perform any operation we have 2 parts as expression and the result. Hence, we need a variable to store the result of the addition

Let’s save the result in variable ‘c’ such as:

c <- a + b

Note: It is not mandatory to give spaces between the variables in R. R programming language completely ignores the spaces and hence spaces doesn’t matter at all. But for better visibility of the code, in any programming language, it is always good to add spaces after every variable when you are assigning value or retrieving result (Just don’t disturb the syntax! LOL).

Now we need to see our result and in any programming language we generally print the result either in console or display on other options based on programming language platform.

In general you might have heard of the functions such as printf, print, console.log, response.write etc. and many more. These functions are used to print the result/output in most of the programming languages.

But in R, you don’t have to write any of such function.

Then how we are going to display the result??

Solution

To display the output of the operation we just need to execute the variable in which we are storing the value of the expression.

In our case, we are storing the value in variable c.

Hence type c and execute by pressing Ctrl + Enter (on windows).

Now we got result in console!

Similarly, we can perform various arithmetic operations as well.

Let’s try the following

2. Subtration

var1 <- 201

var2 <- 100

output <- a – b

output 

(o/p- 101

3. Multiplication

a <- 20

b <- 10

result <- a * b

result 

(o/p - 200)

4. Division

x <- 200

y <- 10

z <- a / b

z
(o/p - 20)

Now let’s see another exciting use of a variable.

Let’s calculate the square root of any value stored in variable x

x <- 49

result <- sqrt(x)

result

In variable result you will get the value 7 as a square root of 49. Don’t worry about ‘sqrt()’ function right now. I will explain it in upcoming articles related to functions.

Up till now, we performed operations on integer variables. We can also play with character type of variables in R. Let’s see how:

Below is the snapshot showing the basic arithmetic operations that we performed above with variables:

Arithmetic Operations on Variables
Arithmetic Operations
Operations on Character type of variables

You already know how to store the value in a variable. It goes in same way for character variables as well.

Ch1 <- “Welcome”

Ch2 <- “To”

Ch3 <- “CodeInfo.Net”

chResult <- paste(ch1, ch2, ch3)

chResult

Again don’t worry about the paste() function right now. Still for your information, the paste()function will concatenate the values of those three variables as specified in the function call and you will get the result as “Welcome To CodeInfo.Net”. Isn’t it amazing?? Yes. Indeed!!

Below snapshot shows how to assign values to character variable

Assign value to Character variables
Assign value to Character variables

That’s it for now!

If you like this article please let me know. Also if you think there is a scope of improvement, still feel free to give me feedback.

Keep coding.

Feedback

[wpforms id=”22″]

Data types in R

In this blog we will learn the basic data types in R programming. Before proceeding to data types, let’s do the recap of our previous article

We have already done the environment setup for using R. Even though if you have missed it, here is a link Getting started with R programming.

Above link will guide you through the environment setup and installations steps.

Now we are done with the environment setup, it is time for us to deep dive into the exciting world of R programming.


Before getting inside any programming languages we must know the basic data types associated with variables.

Basic data types are as given below:

  1. Int – Integer
  2. Char – Character
  3. Float- Floating point precision value
  4. Double – double precision floating point value

These are the basic data types. Now we are concerned about the data types in R. below is the detailed explanation of Data types in R and their basic implementation:

Data types in R:

There are basically 5 data types in R programming language:

  1. Int
  2. Double
  3. Char
  4. Complex
  5. Logical

Let’s see one by one.


Note: we are going to work in R studio for every tutorial. To execute the statement use “Ctrl + Enter”


  1. Int (Integer)

Int is nothing but the integer data type as we have seen earlier. Any numeric value (except fractional ex. 0.1) comes under the category of int.

Declaring an integer variable in R:

Let x be our variable.

  x <- 10L 

(less than ‘<’ followed by hyphen ‘-’ is assignment operator in R )

Press ctrl + enter to execute the above statement.

It will store 10 into the x variable. Now you might be thinking about the L part in the declaration statement. We will discuss it in some time. Below is the code snippet:

 #Integer

  a <- 2L

after executing the above statement(pressing ctrl+Enter) the code gets copied and execute in the console and we get the result in Global Environment.

  1. Double

Double just basically means the value with decimal point

   #Double

   b <- 39.5

Again you need to press ctrl+Enter to execute the statement and it will store the value into variable b as Double.

Now you might have observed that we didn’t use ‘L’ for double values. Reason for this is explained below

By default, R decides that in which format it is going to store the data. All arithmetic operations in R, are conducted in the double data type.

Hence, it is necessary to put ‘L’ in front of a value so that it will understand that the value is of Integer data type.

Next data type is quite interesting. If you love mathematics then you will definitely love this data type

  1. Complex

Complex data type is used to store the complex numbers.

#Complex

Complex <- 3+2i

Now after executing the statement 3+2i will be assigned to the variable Complex.

  1. Char

Char in R is used to store the character as well as series of multiple characters which we basically refer as string. So, in R we don’t really need another data type to store value of type String. Below is an example.

#Character
C <- ‘r’

C1 <- ‘R Programming’
  1. Logical

Logical data type is the one which stores values such as True or False.

You can use the syntax given below to store the logical values.

#Logical

Logical1 <- T

Logical2 <- F

Logical3 <- True

Logical4 <- False

Below is the snapshot of the implementation of all the data types we listed above.

Image: Data Types

Data types in R
Data Types In R

Image: Console

Console screen
Console Screen

Image: Global Environment

Global Environment screen
Global Environment

Note: Please make sure of the case if you are using T as True.

Using “Logical <- t” will give you another result which is not really important for us at this moment.


Now if you want to check the data type of variables you have declared above,

You can use the below function:

typeof(object)

Example:

  1. Integer
X <- 20L

typeof(X)
  1. Double
Y <- 20.5

typeof(Y)
  1. Complex
C <- 10+5i

typeof(C)
  1. Character
Char <- ‘Character’

typeof(Char)
  1. Logical
L <- T 
typeof(L)

L1 <- F
typeof(L1)

L3 <- True
typeof(L2)

L4 <- False
typeof(L4)

Here is the snapshot showing the use of typeof() function of R.

typeof() Function
typeof() Function

So, we have seen the data types and their implementation.

Also we have seen the use of typeof().

Keep in touch for more articles like this. If you like this article please let me know. Also if you think the article is not helpful, feel free to give feedback by filling up the form below. It will help me to come with more improved content.

Keep coding.

Feedback

[wpforms id=”22″ title=”false”]

RProgrammingLogo

Getting Started With R Programming

In this series of blogs, we are going to learn about the basics of R programming. Before starting, it is necessary to setup the environment for it.

For those who are new to programming, Environment is nothing but the medium with which we code. It consist of the editor, files, applications  which are necessary to do the coding as well as execution part.

Now, In this particular blog we are going to setup the environment for R Programming. But first we must know what is R Programming.

What Is R Programming?

R is a programming language and software environment for statistical analysis, graphics representation and reporting.

R is world’s most widely used statistics programming language. It’s the # 1 choice of data scientists and supported by a vibrant and talented community of contributors

Getting Started With R Programming – Environment Setup and Installation:

First of all you need to create and setup environment for development of R programming. For this to be done, go to the website https://cran.r-project.org/

Here you can select and download the version of R based on your operating system.

R Programming Version Selection
Selecting Specific Version of R

After clicking on any of the link, you will get the option to download the base for R.

Once you finished with downloading, install the package.

Installing the package gives you a console for development. But it is not enough as it is a console, it does not have any UI and it can make you go away from learning R Programming.

R Console

R Programming Console
R Console

It’s not very convenient, not very nice to work in console. Hence, we will install an IDE for it. R Studio is the best option that we have for development in R programming. We can get the IDE from https://www.rstudio.com/products/rstudio/download/ this link.

Once you finished with downloading the RStudio, Install the software.

The R Studio looks like this:

R Studio
R Studio

One more important thing is to set up how your R Studio IDE looks. For some people, the IDE needs to be dark, for some, it needs to be the default.

To make changes in your R Studio IDE, follow the steps given below,

  1. Go to Tools.
  2. You will see the menu named “Global options…”
  3. Click on it.
  4. A new window will appear. It contains all the customization options that you might not need all the way except some options.
  5. Click on Appearance. Here you can change the look and feel (Theme, Font size, Editor theme etc.)
  6. Select the options as per your need and comfort.
  7. I have selected the R Studio theme as Modern. My font is Lucida Console with size 10 font and Editor theme in  Tomorrow Night Blue.
  8. Below is the sample screen.
Adjust Look and Feel of your IDE

Now, we are all set with our IDE and finally we can start creating the first R program.

Click here to read about data types in R Programming.

Thank you for reading 🙂

Keep coding.

Free Time Utilization In Best Possible Way

What To Do In Your Free Time When Not Coding

This is my first blog that I am going to post. This article is just to clear the dilemma i.e what to do in free time. It is all about reading. Keep reading..!!

You might wonder sometimes, what to do in free time? Everyone gets confused at some point of time everyday. It is not about one day or two day. This question arises in our mind everyday. If we ask people about what they do in their meantime? We will surely get answers like I read books, I listen songs, I watch movies etc. but my question  is does it really help? Or does it make any sense? Or does it even increase our knowledge?

And the answer to this question is NO. A Big NO. Listening songs may lead to swing of mood, watching movies just wastes our time, and reading books can be helpful at times.

Solution

So I’ve the good and interesting remedy for this. If you are a technical geek, then you must read the articles posted on the sites like CsharpCorner, StackOverFlow, CodeProject, Asp.Net etc. Now some of you might have said that we do it everyday while solving errors. Yes !! everyone does that. But our point is what we really do in our meantime or when we get bored. It is not about solving our own errors. And struggling to do it makes us visit StackOverflow. We never spend time to learn from other’s mistake.

Learn from other’s mistake
Time to Learn
Time To Learn

I am not asking to read the similar things that you come across while working, rather I am appealing to read New Topics. There is lot more happening in the entire world (not in all aspects, I am talking about the technology only ☺. Lots of guys are working on lots of technologies and they encounter many different problems that we have never heard off. Even many of them are working on their own new ideas. By reading about these innovative ideas/ New Errors, we will get to know many things that we can not even keep count of.

And I bet that we will not get bored while we give our precious time for reading these articles. In fact we can not get bored. After all it is a human nature, that we get attracted toward every new thing we see for the first time, provided that we have some or little interest in it. Example,  I love programming and being the developer I will be curious if I see something new. But I won’t get that much excited if some political news is there because I am not interested in it.

Advantages

I can list down the advantages of visiting these sites often:

    1. We get to know many different thinngs
    1. We learn new techniques for the task that we were doing in different way
    1. Our mind will get freshed
    1. We will get stress free
    1. Time will get passed
    1. After reading we can continue to work on regular task in free state of mind
    1. We won’t realise how much time we have spent while reading
  1. And ofcourse it is Internet, so we will get deeper and deeper into it
Conclusion:

There is a basic proverb. Learn from other’s mistake.  And lots of people are making mistake each and every second. We just need to make use of those.

But remember it is just to refresh our mind. We are given a specific time for our project. 

Happy Coding

You need to add a widget, row, or prebuilt layout before you’ll see anything here. 🙂
Back to top