- Hands-On Geospatial Analysis with R and QGIS
- Shammunul Islam
- 359字
- 2021-06-10 18:44:23
Vectors
Vectors are used to store single or multiple values of similar data types in a variable and are considered to be one-dimensional arrays. That means that the x variable we just defined is a vector. If we want to create a vector with multiple numeric values, we assign as before with one additional rule: we put all the values inside c() and separate all the values with , except the last value. Let's look at an example:
val = c(1, 2, 3, 4, 5, 6)
What happens if we mix different data types such as both numerics and characters? It works! (A variable's name is arbitrarily named as val, but you can name your variable anything that you feel appropriate, anything!) Except in some cases, such as variable names, shouldn't start with any special character:
x = c(1, 2.0, 3.0, 4, 5, "Hello", "OK")
What we have just learned about storing data of the same types doesn't seem to be true then, right? Well, not exactly. What R does behind the scenes is that it tries to convert all the values mentioned for the x variable to the same type. As it can't convert Hello and OK to numeric types, for conformity it converts all the numeric values 1, 2.0, 3.0, 4, and 5 to character values: that is, "1", "2.0", "3.0", "4", and "5", and adds two more values, "Hello" and "OK", and assigns all these character values to x. We can check the class (data type) of a variable in R with class(variable_name), and let's confirm that x is indeed a character variable:
class(x)
We will see that the R window will show the following output:
[1] "character"
We can also label vectors or give names to different values according to our need. Suppose we want to assign temperature values recorded at different times to a variable with a recorded time as a label. We can do so using this code:
temperature = c(morning = 20, before_noon = 23, after_noon = 25, evening = 22, night = 18)
- GNU-Linux Rapid Embedded Programming
- 控制與決策系統(tǒng)仿真
- 反饋系統(tǒng):多學科視角(原書第2版)
- 基于神經(jīng)網(wǎng)絡的監(jiān)督和半監(jiān)督學習方法與遙感圖像智能解譯
- Citrix? XenDesktop? 7 Cookbook
- Photoshop CS5圖像處理入門、進階與提高
- Machine Learning Algorithms(Second Edition)
- 數(shù)字多媒體技術基礎
- 樂高創(chuàng)意機器人教程(中級 上冊 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實踐指導叢書)
- PHP求職寶典
- 精通ROS機器人編程(原書第2版)
- 菜鳥起飛五筆打字高手
- 巧學活用Linux
- 工業(yè)機器人基礎
- 歐姆龍CP1系列PLC原理與應用