We will be using the same dataset used in my previous post
(https://sritechstudio.com/demographic-analysis-using-dataframes/)
Now let us work on some basic and essential operations in Dataframe
- Subsetting :
Subsetting in R is a useful indexing feature for accessing object elements. It can be used to select and filter variables and observations. You can use brackets to select rows and columns from your dataframe.
- If you want to print 1 to 10 rows for all the columns, please refer the syntax written in line 20 from the below screenshot
As we can see the output in console pane, it prints all the columnar data for 10 rows.
- If I want 5th Row and 100 Row with all columnar data, then write the syntax in this way.
This printed 5th row and 100th row
2.The second way to drop the columns is using drop function
Drop (): As we have seen one method to drop the columns is using subsets concept and we can also drop the columns using drop Function.
Here this is printing second columnar data.
Try giving the drop value as TRUE and see the results.
3.Now will work on some Arithmetic operations:
Here iam Multiplying Birthrate and internet users, as we know Multiplying the both values will not make any sense but to understand that we can perform Arithmetic operation in Dataframes using Dollar ($) symbol.
4.Now let us work on some interesting Things:
As our dataset contains 5 columns (Country code, Country name, Birthrate, Internet users & Income group)
If you want new Column to add to this dataframe
I will be adding the new column population
Here the Population is added with $ symbol to stats data, we can see the Population column in console pane.
How do we remove the Column???
Here is the solution to remove column
Now the Population column is removed.
4. Filtering Dataframes
Why filtering Dataframes is required?
Let us take a small example: from the dataset, if I want to Print the usage of Internet users less than 2Million
In this kind of scenarios, we use Filtering concept in data frames.
Will quickly jump into one example:
Here these are the countries, the internet users are less than 2 million.
Let us work another scenario: if I want the data where the Birthdate is greater than 40 million and Internet users less than 2 Million:
Here in this example, we can see the output in console pane, there are only three countries whose birthrate is greater than 40 Million and Internet user’s usage less than 2 Million.
We will Visualize the same scenario in my next post… Keep Coding and Exploring 👍