Hands-On Geospatial Analysis with R and QGIS
上QQ阅读APP看书,第一时间看更新

Data frames

Data frames are like matrices, except for 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 a data frame. First, define a variable with the names of different food items:

items = c("potato", "rice", "oil")

We can define a data frame using data.frame as follows:

all_prices3 = data.frame(items, jan_price, mar_price, june_price) 
all_prices3

The data frame all_prices3 looks like the following:

Accessing elements in a data frame can 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 a data 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 number before, and the column index indicated by the number after. 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 row using rbind(). Now we define a new numerical vector that contains the price of the pen vector for January, March, and June, and we can add this row using rbind():

pen = c(3, 4, 3.5)
all_prices4 = rbind(all_prices4, pen)
all_prices4

Now we see from the following output that a new observation is added as a new row:

We can add a column using cbind(). Now, suppose we also have information on the prices of potato, rice, oil, and pen for August as given in the vector aug_price:

aug_price = c(22, 24, 31, 5)

We can now use cbind() to add aug_price as a new column to all_prices4:

all_prices4 = cbind(all_prices4, aug_price)
all_prices4

Now all_prices4 has a new column aug_price added to it: