CodeInfoNet

Learn programming on the go!

Home » Posts

Posts

dataframe

Data frames in R

Data frames in R is a most essential part in this learning journey of our R programming series.

In this article we will be learning basics of data frames. At the end of this article, you will be fully aware of –

  1. What is a data frame? & How data frames are different than vectors?
  2. What operations can be performed on data frames? & What are the usages of data frames

We have already seen how to import the data into R. If you have missed it or you want to quickly make a revision, you can always refer our previous article on How to import a .csv file in R.

Let’s get started with our journey of data frames.

What is a data frame? & How data frames are different than vectors?

Data frames are the two dimensional version of the list. They are the most useful storage structure in data analysis. To make it further more simple, we can think of data frames as a Excel or spreadsheet of R. Also, it stores data in similar format.

Data frame groups vectors together into a two-dimensional table. Each vector becomes a column in the table. Lets see what operations we can perform on data frames.

What operations can be performed on data frames? And What are the usages of data frames

To learn about data frames in more proper way, we need to import data into our project.

We have MovieRatings data in a csv file. This file can be downloaded from CodeInfo’s Git repository.

setwd("H:\\CodeInfo.Net\\Github\\RProgramming")
MovieRatings <- read.csv("MovieRatings.csv")
MovieRatings

Lets get started with various operations that we can perform on data frames in R.

i. Checking if it is a data frame or not!

This is the most important and simple thing that we can do with data frames. And why not, its good to be assured of using our resources first. Here is the syntax to check our available data-

is.data.frame(MovieRatings)

So you might have understood that it is the same question as our bulleted point. We just need to write it in a format which compiler can understand.

Grace Hopper SubSea Cable

Google Has Announced Grace Hopper Subsea Cable

Yesterday on 28th July, 2020 Google announced that they have installed Grace Hopper subsea cable network.

This is very interesting news and a topic to discuss on. Because these days we all are connected with the term internet so eternally that any news which refers to the word INTERNET, automatically, we get attracted towards it. Of course, internet is part of our daily lives and a basic need for some young people.

So, let’s discuss here about the latest Grace Hopper subsea cables.

Is it going to impact your lives? If yes, do you know how?

First of all, Why the name “Grace Hopper”?

Grace Hopper was an American computer scientist and United States Navy rear admiral. She is also known as the creator of famous High-level programming language COBOL.

She was also one of the employees who developed The UNIVAC I(UNIVersal Automatic Computer I). This was the first general purpose electronic digital computer design for business application.

In World War II, she was one of many women who volunteered to serve in the WAVES.

So, Google decided to name this subsea cable network to honour her contribution in computing world as well as in the field of defence.

Before knowing about the Grace Hopper SubSea cable project, let’s have a short summary of how Internet works.

How Internet Works

98% of the Internet traffic around the world is carried through the subsea cables. (How many of you thought internet works on broadband/dialup connection? LoL). Subsea cables is a vast network of cables crisscrossing the ocean.

This cable network makes it possible to share, search, send, and receive information around the world at the speed of light. As our lives are becoming highly digital i.e the ways we connect, work, play are becoming digital, so it is necessary to have a reliable connectivity and it has become the basic need nowadays.

Grace Hopper

This makes Google excited to announce this “Grace Hopper” cable network. It will run between the United States, the United Kingdom and Spain, providing better resilience for the network that underpins Google’s consumer and enterprise products.

Grace Hopper SubSea Cable
GraceHopperSubSeaCable

This is not the only cable network or the first cable network. This network is going to be the part of other three subsea cable networks that Google has already installed. They are CurieDunant and Equiano.

The subsea cables allow Google to plan effectively for the future capacity needs of their customers and users around the world, and add a layer of security beyond what’s available over the public internet.

The Grace Hopper cable will be one of the first new cables to connect the U.S. and U.K. since 2003, increasing capacity on this busy global crossroads and powering Google services like Meet, Gmail and Google Cloud.

It is also their first investment in a private subsea cable route to the U.K., and first-ever route to Spain. By making this Grace Hopper subsea cable with 16 fiber pairs (32 fibers) Google has made a significant upgrade to the internet infrastructure connecting the U.S. with Europe.

How will it be helpful?

The Grace Hopper subsea cable incorporates novel optical fiber switching that allows for increased reliability in global communications, which enables for better move traffic around outages.

Is this information helpful? Please let us know. Also, read our articles on programming. Here is the link to our Posts page.

Keep us posted if you have any query. Your advice is priceless. Please provide your feedback regarding the quality of explanation or the complexity of content (if any) using below feedback form.

Feel free to comment or reach us directly via mail. Also you can contact us by Facebooklinkedintwitterinstagram any of the platform you feel comfortable with.

Keep Learning. Keep Coding.

csv file

How to import a .Csv file in R

In this article we are going to learn about how to import a .Csv file in R programming. At the end of this article you will be able to import a .csv file in R without any issue.

Just to have a recap of what we did in our previous article, previous article was to learn about basic plotting technique in R programming. We made a use of matplot() function to plot our subsets. If you have missed it, here is a link “Subsetting & visualizing the subsets using matplot() function

Before starting to import files, we will just go through some of the features of R programming.

  • R is designed in such a way that there is a minimum work for a data scientist when it is related to formatting the data, collecting a data, visualizing and all other data related stuffs.
  • If we consider a functionality of importing file in other programming languages, we may need to write lots of lines of code then separate code for fetching the data storing it in data table, list, or other object…. And the task list is never ending.
  • But, In R programming, we just need to write 1-2 lines to import the data and store it in a variable.
  • Another amazing fact about R is, when we import the csv file into it, it automatically stores the data in tabular format. Also, it creates a data frame from the file

Let’s see the function we are talking about straight away:

read.csv() function:

This function is used to import a csv file into R. This function has many parameters but they are optional. It means even if we don’t specify these parameters, our file will be imported as it is. Only one condition, file should be in proper format.

There are 2 ways with which we can use the read. csv() function. We can use them as an when needed. Both ways are very simple to implement.

  1. Selecting & uploading a file when prompted.
  2. Get/Set working directory

Lets see one by one.

Visualizing Subsets in R

Subsetting and visualizing subsets with matplot

In this article we are going to learn about subsetting and visualizing subsets with matplot.

In Previous article we have learnt about “Matrix operations in R using Basketball data set”. if you have missed it, please go through it once to get familiar with the data set we are using. Here is the link: Matrix operations in R using basketball data set

First of all we will see the use of matplot() function

Matplot() function:

This function is basically used for plotting the columns of matrices. As we already know how to see whole information about any function in R. We will just type “?functionName”.

Ex. “?matplot”

When you execute this for help, you will be amazed that this function expects so many parameters. But guys, don’t be worried. we are not going to use this function often. But it is important that we should be aware of it just to be able to understand other related functions.

So to understand quickly, we will start implementing this function straight away.

In this article, we are going to use FieldGoals Matrix. As this function is used to plot the columns of a matrix, plotting FieldGoals matrices wouldn’t produce a great output. See why in below screenshot:

FieldGoals sample output

The matplot() function will plot only columns and that won’t make any sense and we need data for each player for each year. In short, we need to plot the rows of this matrix.

How to do it? Transpose? Yes!!

Matrix operations in R using Basketball data set

In this article we will learn about various matrix operations in R using Basketball data set.

Note:

For this particular blog and few other blogs we are going to use the data set prepared by www.superdatascience.com.

Before we start anything related to matrix operations, Let’s first get familiar with the data set we are going to use as an input data.

So, we will be using the Basketball data set.

You don’t necessarily need to be aware of all the stuffs related to basketball. This is just a data set containing the information about the players, their performance over the years and their stats.

Loading Data Set:

To load the data set into your work space, you need to go through few steps. Here is the article showing how to use the basketball data set. Click on below link:

How to get basketball data set?

Hopefully you have downloaded the data set into your work space.

Matrix Operations

So, in our previous articles we have already seen some operations on vectors such as adding two vectors, subtracting one vector from other, multiplication between the vectors.

Similarly, can we perform these operations on a matrix? The answer to this question is “Yes! Of course we can!!”

Are you excited to know what operations we can perform on matrices? Let’s see one by one:

After loading the data set into work space, you will see that there are multiple matrices such as Games, FieldGoals, Salaries, FieldGoalAttempts etc.

Below are some simple functions to make you people comfortable with the matrix operations with the use of this particular data set.

Getting Row/Column Names

Let’s see what row names and column names the Games matrices has.

We use below simple code:

Rownames(Games)
Colnames(Games) 

You can try these steps for any matrix for sure. As usual I would suggest you to try by your own.

We already know how to get the value of specific column in a specific row.

If you want to find out how many games a player has played in a particular year, you can use following syntax:

Games[rowname, columnname]
e.g. Games[“LeBronJames”, “2012”]

Matrix Division

Consider we want to find out how many field goals every player has scored in each game that they have played every year.

Let’s see how to do it:

We need to move to FieldGoals matrix. Write FieldGoals and execute to get the data in FieldGoals matrix

If we were doing it with any other programming language, we would need to loop through both of these matrices i.e Games and Fieldgoals.

But R has made it so simple that we just need to write a single line to get the expected result

We need to divide these two matrices to get number of scored goals.

Here is how : FieldGoals/Games

And to make it more readable you can round it to 1 decimal.

round(FieldGoals/Games,1)

Same operation can be performed to calculate the minutes played by each player every year.

I will keep this task for my readers.

Likewise, any operation such as adding matrices, subtracting one from another, multiplying and dividing matrices can be performed with matrices in R.

Hence, we started with matrix operations. Keep reading for simple and amazing articles like this.

Keep me posted if you have any query. Your advice is priceless. Please provide any feedback regarding the quality of explanation or the complexity of content using below feedback form.

Feel free to comment or reach us directly via mail. Also you can contact us by Facebooklinkedintwitterinstagram any of the platform you feel comfortable with.

Matrices in R

Matrices in R

Matrices in R is a very interesting topic in R programming. In this article we are going to have a look at matrices in R and various operations that can be performed on them.

Also we will see how can we name the matrix, how to fetch the specific value from the matrix.

We are going to have a look at how to get the entire row or entire column. First let’s see what is matrix and how we can name them.

“So, the matrix in R is the combination of multiple vectors.” This is the most basic definition that I could ask for.

But, the basic difference between vector and a matrix is that vector has one dimension and matrix has two dimensions.

Let’s consider we have below sample matrix which we are going to create by using rbind() function later in this article.

Let’s call this matrix as A

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20

What if you want to get the number 13 from the matrix A?

Similarly, what if I want to get the entire row or entire column from the matrix A? Is there any simple way available?

Because, In other programming languages, we might need to write a for loop for the entire row or entire column and fetch each element one by one.

But this is not the case in R programming. R provides very simple ways to access rows or columns or the single element from the entire matrix.

Let’s have a look at these ways:

Getting the specific element from matrix A

Consider we want to fetch the element 9 from matrix A.

What we have is the row number and the column number. These parameters are enough to get any element of matrix.

Hence,

A[2,4] will give me the expected element i.e 9.

We store it in another variable as:

Value <- A[2,4]

Getting the entire column from matrix A

Let’s say we need 4th column

A[,4] will give us the entire 4th column

Here we have specified just the column number.

And we already know that everything in a matrix is a vector. So we can easily store it in a variable as:

Column4 <- A[,4]

Getting the entire row from matrix A

Suppose we want to get the 2nd row from the matrix. As we have seen how to get the entire column just by specifying the column number, similarly specifying the row number gives us the entire row from matrices

Row2 <- A[2,]

Hence, we have seen the matrix, how to access the single element.

Also we have seen how to get the entire row or column.

Now, let’s have a look at how to create matrices by using multiple techniques.

  1. matrix() function
  2. rbind() function
  3. cbind() function

1. matrix() function

Although we do not use this function often to create vectors, but it is good to know the things.

matrix() function creates the vector in a simple way but it is not convenient. Let’s see why:

We create a data first to insert into the matrix.

myData <- 1:20		#creates a sequence of numbers 1-20

> myData
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

Now we will use matrix function to create a matrix from the above vector

myFirstMatrix <- matrix(myData, 4,5) 

Here, we have specified three parameters.

One is dataset, number of rows and number of columns that we want in a new matrix

Now let’s understand how it generates the matrix

> myFirstMatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

It generates the matrix in such a way that values are entered row by row till the specified number of rows reached, it again starts from the first row in next column.

In real life scenario, it may result in unwanted matrix. There is an option which can be used with the matrix() function.

The only difference it creates is it generates matrix by entering values column by column.

Here is the example:

myFirstMatrix <- matrix(myData, 4,5, byrow = True)

> myFirstMatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20

Have a look at both the resulting matrices. The number of columns and the number of rows hasn’t changed at all.

But the order in which these matrices are bound is different.

We cannot rely on this function.

2. rbind() function

rbind() is a built in function of R. As a name suggests, it binds the rows in a matrix.

To be more specific, we can say that rbind() function binds the vectors row by row to form a matrix.

Let’s create our own matrix with the rbind() function.

First we need data to enter in our matrix so create it first

data1 <- c("Welcome","To", "www.codeinfo.net")
data2 <- c("Have a", "Great", "Day!!")
data3 <- c(1,10, 20)

So executing these lines will create vectors of type character and numerical.

rbindMatrix <- rbind(data1, data2, data3)

rbindMatrix

Output:
> rbindMatrix
      [,1]      [,2]    [,3]              
data1 "Welcome" "To"    "www.codeinfo.net"
data2 "Have a"  "Great" "Day!!"           
data3 "1"       "10"    "20"              

so we have created a matrix with rbind() function.

But you might be wondering that the value in data3 is numeric then why matrix showing them as a character?

The conclusion here is matrix doesn’t contain the data of different data types. It has considered the numbers as characters.

3. cbind() function

As we are aware that rbind() binds the rows so can you guess what cbind() will do?

Yes!! You are absolutely right.

It will bind the columns to create the matrix.

Let’s create another matrix with cbind() function

Here is the data first:

c1 <- 1:10
c2 <- 11:20

cbindMatrix <- cbind(c1,c2)
cbindMatrix

> cbindMatrix
      c1 c2
 [1,]  1 11
 [2,]  2 12
 [3,]  3 13
 [4,]  4 14
 [5,]  5 15
 [6,]  6 16
 [7,]  7 17
 [8,]  8 18
 [9,]  9 19
[10,] 10 20

So we have created a matrix by binding two columns.

Hence we have seen the matrix and multiple ways to create it.

Below is the one minute video of matrix operation we performed in this article.

The code is available for the series of articles present on www.codeinfo.net on Github.

Here is the Link: R programming Tutorials

Hence we know the basics of matrices in R. Keep reading for more articles on matrix operations.

Visual Studio 2019

Visual Studio 2019 Preview What’s New

Microsoft has released a new version of visual studio as Visual Studio 2019 Preview!!


Here is everything you need to know about VS 2019. Visual studio 2017 is already making all of us crazy.

In fact, we are yet to explore it fully and we have another version of our favorite IDE coming up soon.

We are going to see what’s new in the visual studio 2019 and how it is different from visual studio 2017. Also, we will see from where we can download it.

First let’s understand what is Visual Studio 2019 (Preview)

With Visual studio 2019 preview we are going to get the early access to newest features of visual studio which are yet to be released.

Now you may think if the features are yet to be released then why they (Microsoft) have given us the early access.

The reason behind it is, by trying Preview, you can take advantage of new builds and updates that ship frequently

Also, Microsoft has said in a special note:

“Please note that the preview version is continuously evolving and certain features may not work as expected. This version is also not licensed to build shipping applications.”

Now what’s new in the Visual Studio 2019?

There are lots of minor and major improvements in functionality as well as in the UI.

Let’s see what Microsoft has stated:

  1. Start window
  2. UI refresh
  3. Search improvements
  4. Document health & code cleanup
  5. Debugger search & stepping improvement
Continue reading
functions in R

Functions in R programming

functions in R
What is function in R

Function in any programming language is an important part which is responsible for the smooth execution of the code. This article will help you to get familiar with functions in R programming. Also we will get in touch with the concept of packages in R.


This article is mostly focused on the theoretical part. We already have implemented functions in many of our previous articles. Still I will recommend my readers to try out each function to get used to this concept.

In my previous post, I had written about the multiple ways of creating the vector. Click on the link given below to read about it.

Creating vectors in R

What is a function?

Basic definition of a function is it is a set of code which is executed in order to get the specific result. Once the function has been defined, it can be called as many times as required.

In other words, we can define a function as blender which takes fruits as an input, it does some processing on them and gives juice as an output.

Continue reading

Create Vector In R

This article is all about creating vector and multiple ways of doing it. At this stage we all know that everything in R programming is a vector. Even a single character or a single numerical value is nothing but a vector. Let’s see how to create the vector.

In last article I have introduced you to the concept of vector. If you have missed it, you can read it here Introduction to vector in R.

There are multiple ways of creating the vector as listed below:

  1. Create vector using c()
  2. Create vector using seq()

Note: c() and seq() are the functions in R. As of now you don’t have to worry about the functions. We are going to get into functions and packages in next article.

Let’s see these functions one by one.

Continue reading

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

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

How to get Basketball Data set?

How to get Basketball Data set?

This article shows you from where to get the basketball dataset and how to load it for our learning purpose.

The Dataset is solely prepared by www.superdatascience .com. We’re only referencing it in our website and using it for learning purpose.

Note:

Get Basketball Data set

Follow below steps :

  1. Go to www.superdatascience.com/rcourse
  2. Scroll down to until you see Section 4. Matrices option
  3. Click on Basketball Dataset (2nd option)
  4. A zip file will be downloaded automatically.
  5. Unzip it to your preferred location
  6. You will see s4-BasketballData.R file. Open it in RStudio or in the editor you are using
  7. Do not worry about the code or the script written in the file. We will be there to learn all of it in our upcoming lessons.
  8. Select all content and execute it.

So by following all these steps you have successfully prepared the data set

Let’s get back to article: matrix operations using basketball data set

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