CodeInfoNet

Learn programming on the go!

Home » Data frames in R » Page 2

Data frames in R

ii. Using ‘$’ sign to access our column data.

This statement already explained the use of $ sign. It is used to access the column data. In our few of the previous articles, we have already seen how to access a column data from a matrix or a vector. This can be considered as an improvised way of doing the same thing.

Ex. Lets say we want to access the column Genre from our data frame MovieRatings So, we will write as-

dataframeName$columnName
i.e MovieRatings$Genre

As simple as it can be.

iii. Mathematical operations using data frames

We already know how to access the columns so what remains is adding the arithmetic operator between our desired columns to get the result.

Ex. What if we want to multiple two columns from same data frame. How to do it? Lets see in below example

DataFrame$Column1 * DataFrame$Column2

MovieRatings$Audience.Ratings * MovieRatings$Rotten.Tomatoes.Ratings
iv. Adding a column to data frame

Yes you read it right. We can add a column to our original data frame which actually is not a part of our data frame. We just need to specify the column name and access it in the way mentioned above.

Ex. Dataframe$NewColumnName

Execute it and we have a new column in our data frame. We already know how to assign a value to variable. Let’s do it on our data frame.

We will add a new column as MyColumn and we will insert the multiplication result into that column as shown below

MovieRatings$MyColumn <- MovieRatings$Audience.Ratings * MovieRatings$Rotten.Tomatoes.Ratings

Sumit Rajguru

Back to top