- Hands-On Geospatial Analysis with R and QGIS
- Shammunul Islam
- 318字
- 2021-06-10 18:44:23
Lists
Now, items jan_price and mar_price have four elements, whereas june_price has three elements. So, we can't use a data frame in this case to store all of these values in a single variable. Instead, we can use lists. Using lists, we can get almost all the advantages of a data frame in addition to its capacity for storing different sets of elements (columns in the case of data frames) with different lengths:
all_prices_list2 = list(items, jan_price, mar_price, june_price)
all_prices_list2
We can now see that all_prices_list2 has a different structure than that of a data frame:

Accessing list elements can be done by either using [] or [[]] where the former gives back a list and the latter gives back element(s) in its original data type. We can get the values of jan_price in the following way:
all_prices_list2[2]
Using [], we are returned with the second element of all_prices_list2 as a list again:

Note that, by using [], what we get back is another list and we can't use different mathematical operations on it directly.
class(all_prices_list2[2])
We can see, as follows, that the class of all_prices_list2 is a list:

We can get this data in original data types (that is, a numeric vector) by using [[]] instead of []:
all_prices_list2[[2]]
Now, we get the second element of the list as a vector:

We can see that it is numeric and we can check further to confirm that it is numeric:
class(all_prices_list2[[2]])
The following result confirms that it is indeed a numeric vector:

We can also create categorical variables with factor().
Suppose we have a numeric vector x and we want to convert it to a factor, we can do so by following the code as shown as follows:
x = c(1, 2, 3)
x = factor(x)
class(x)
- 繪制進(jìn)程圖:可視化D++語(yǔ)言(第1冊(cè))
- Cloud Analytics with Microsoft Azure
- Julia 1.0 Programming
- PHP開(kāi)發(fā)手冊(cè)
- 傳感器與新聞
- 走近大數(shù)據(jù)
- 計(jì)算智能算法及其生產(chǎn)調(diào)度應(yīng)用
- Java組件設(shè)計(jì)
- DynamoDB Applied Design Patterns
- Machine Learning in Java
- Mastering Microsoft Dynamics 365 Customer Engagement
- 三維動(dòng)畫(huà)制作(3ds max7.0)
- 軟件設(shè)計(jì)
- Android High Performance Programming
- 從零開(kāi)始學(xué)HTML+CSS