CodeInfoNet

Learn programming on the go!

Home » Data frames in R » Page 3

Data frames in R

v. Remove column

If we can add any external column, then there must be any way to remove it as well. And its simple. We assign a Null value to it.

Ex. MovieRatings$MyColumn <- NULL
vi. Filtering Data Frame

It is very much important feature of a data frame. Filtering is necessary to reduce or minimize the volume of a result set.

Also, it helps us to see only desired result and ignore all other stuff.

Ex. What if we don’t want to see the entire list of movies and want to see the list of only those movies which are released before year 2009?

In such scenarios, filter plays the vital role. Let’s see how it works and how we can filter our result.

We can do it by just performing simple comparison as we do in other programming languages.

MovieRatings$Year.of.release < 2009

But, executing above line will give us result in “TRUE TRUE TRUE FALSE” format.

So, we store our filter expression in another veriable as,

filter1 <- MovieRatings$Year.of.release < 2009

And pass this variable to MovieRatings dataframe. Also, we will store the result in the variable MoviesBefore2009.

MoviesBefore2009 <- MovieRatings[filter1,]
MoviesBefore2009

Now executing these lines will give you the list of movies which were released before 2009.

Below image shows all of the above operations implemented using R studio

Data frame in R
Data Frame Operations

There can be so many combinations which we can try to get used to it.

As I have already advised my readers so many times, that to get used to this amazing data science or data analysis language;we need to practice these basics more often.

The full script can be downloaded from our github repository. Also, the sample data set that we’ve used is available to download and use. Here is the url: Dataframe operations in R

Please let us know your view on this series n basics of R programming.

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.

Sumit Rajguru

Back to top