Data frames are like matrices,exceptfor the one additional advantage that we can now have a mix of different element types in a data frame. For example, we can now store both numeric and character elements in this data structure. Now, we can also put the names of different food items along with their prices in different months to be stored in adata frame. First, define a variable with the names of different food items:
items = c("potato", "rice", "oil")
We can define adata frameusing data.frame as follows:
The data frame all_prices3 looks like the following:
Accessing elements in adata framecan be done by using either [[]] or $. To select all the values of mar_price or the second column, we can do either of the two methods provided as follows:
all_prices3$mar_price
This gives the values of the mar_price column of the all_prices3 data frame:
[1] 11 22 33
Similarly, there is the following:
all_prices3[["mar_price"]]
We now find the same output as we found by using the $ sign:
[1] 11 22 33
We can also use [] to access adata frame. In this case, we can utilize both the row and column dimensions to access an element (or elements) using the row index indicated by the numberbefore,and the column index indicated by the numberafter.For example, if we wanted to access the second row and third column of all_prices3, we would write this:
all_prices3[2, 3]
This gives the following output:
[1] 22
Here, for simplicity, we will drop items column from all_prices3 using-and rename the new variable as all_prices4 and we can define this value in a new vector pen as follows:
all_prices4 = all_prices3[-1]
all_prices4
We can now see that the items column is dropped from the all_prices4 data frame:
We can add a rowusingrbind(). Now we define a new numerical vector that contains the priceof thepen vector for January, March, and June, and we can add this row using rbind():
Now we see from the following output that a new observation is added as a new row:
We can add a columnusingcbind(). Now, suppose we also have information on the prices of potato, rice, oil,andpen for August as given in the vectoraug_price:
aug_price = c(22, 24, 31, 5)
We can now usecbind()to add aug_price as a new column to all_prices4: