統計分析:使用 R 入門 / R / 向量
TRUE或FALSE[1])。在本主題中,我們將使用“datasets”包提供的幾個示例向量,其中包含美國各州的資料(請參閱?state)。R 本身就是一個基於向量的程式;事實上,我們在之前的計算中使用過的數字也被視為只有一個元素的向量。這意味著 R 中的大多數基本函式在被賦予向量作為引數時都會表現得合理,如下所示。state.area #a NUMERIC vector giving the area of US states, in square miles
state.name #a CHARACTER vector (note the quote marks) of state names
sq.km <- state.area*2.59 #Arithmetic works on numeric vectors, e.g. convert sq miles to sq km
sq.km #... the new vector has the calculation applied to each element in turn
sqrt(sq.km) #Many mathematical functions also apply to each element in turn
range(state.area) #But some functions return different length vectors (here, just the max & min).
length(state.area) #and some, like this useful one, just return a single value.[1] 51609 589757 113909 53104 158693 104247 5009 2057 58560 58876 6450 83557 56400
[14] 36291 56290 82264 40395 48523 33215 10577 8257 58216 84068 47716 69686 147138 [27] 77227 110540 9304 7836 121666 49576 52586 70665 41222 69919 96981 45333 1214 [40] 31055 77047 42244 267339 84916 9609 40815 68192 24181 56154 97914 > state.name # 一個 CHARACTER 向量(注意引號),包含各州名稱
[1] "Alabama" "Alaska" "Arizona" "Arkansas" [5] "California" "Colorado" "Connecticut" "Delaware" [9] "Florida" "Georgia" "Hawaii" "Idaho"
[13] "伊利諾伊州" "印第安納州" "愛荷華州" "堪薩斯州" [17] "肯塔基州" "路易斯安那州" "緬因州" "馬里蘭州" [21] "馬薩諸塞州" "密歇根州" "明尼蘇達州" "密西西比州" [25] "密蘇里州" "蒙大拿州" "內布拉斯加州" "內華達州" [29] "新罕布什爾州" "新澤西州" "新墨西哥州" "紐約州" [33] "北卡羅來納州" "北達科他州" "俄亥俄州" "俄克拉荷馬州" [37] "俄勒岡州" "賓夕法尼亞州" "最小的州" "南卡羅來納州" [41] "南達科他州" "田納西州" "德克薩斯州" "猶他州" [45] "佛蒙特州" "弗吉尼亞州" "華盛頓州" "西弗吉尼亞州" [49] "威斯康星州" "懷俄明州" > sq.km <- state.area*2.59 # 標準算術運算適用於數值向量,例如將平方英里轉換為平方公里 > sq.km # ... 給出另一個向量,其中對每個元素都執行了計算
[1] 133667.31 1527470.63 295024.31 137539.36 411014.87 269999.73 12973.31 5327.63 [9] 151670.40 152488.84 16705.50 216412.63 146076.00 93993.69 145791.10 213063.76
[17] 104623.05 125674.57 86026.85 27394.43 21385.63 150779.44 217736.12 123584.44 [25] 180486.74 381087.42 200017.93 286298.60 24097.36 20295.24 315114.94 128401.84 [33] 136197.74 183022.35 106764.98 181090.21 251180.79 117412.47 3144.26 80432.45 [41] 199551.73 109411.96 692408.01 219932.44 24887.31 105710.85 176617.28 62628.79 [49] 145438.86 253597.26 > sqrt(sq.km) # 許多數學函式也適用於每個元素
[1] 365.60540 1235.90883 543.16140 370.86299 641.10441 519.61498 113.90044 72.99062 [9] 389.44884 390.49819 129.24976 465.20171 382.19890 306.58390 381.82601 461.58830
[17] 323.45487 354.50609 293.30334 165.51263 146.23826 388.30328 466.62203 351.54579 [25] 424.83731 617.32278 447.23364 535.06878 155.23324 142.46136 561.35100 358.33202 [33] 369.04978 427.81111 326.74911 425.54695 501.17940 342.65503 56.07370 283.60615 [41] 446.71213 330.77479 832.11058 468.96955 157.75712 325.13205 420.25859 250.25745 [49] 381.36447 503.58441 > range(state.area) # 但是有些函式會返回不同長度的向量(這裡,只有最大值和最小值)。 [1] 1214 589757 > length(state.area) # 有些函式,像這個有用的函式,只返回單個值。 [1] 50
c(),之所以這樣命名是因為它concatenates(連線)物件在一起。但是,如果您想建立由數字的規則序列組成的向量(例如 2,4,6,8,10,12 或 1,1,2,2,1,1,2,2),您可以使用幾個替代函式,包括seq()、rep() 和: 運算子。c("one", "two", "three", "pi") #Make a character vector
c(1,2,3,pi) #Make a numeric vector
seq(1,3) #Create a sequence of numbers
1:3 #A shortcut for the same thing (but less flexible)
i <- 1:3 #You can store a vector
i
i <- c(i,pi) #To add more elements, you must assign again, e.g. using c()
i
i <- c(i, "text") #A vector cannot contain different data types, so ...
i #... R converts all elements to the same type
i+1 #The numbers are now strings of text: arithmetic is impossible
rep(1, 10) #The "rep" function repeats its first argument
rep(3:1,10) #The first argument can also be a vector
huge.vector <- 0:(10^7) #R can easily cope with very big vectors
#huge.vector #VERY BAD IDEA TO UNCOMMENT THIS, unless you want to print out 10 million numbers
rm(huge.vector) #"rm" removes objects. Deleting huge unused objects is sensible[1] "one" "two" "three" "pi" > c(1,2,3,pi) # 建立一個數值向量 [1] 1.000000 2.000000 3.000000 3.141593 > seq(1,3) # 建立一個數字序列 [1] 1 2 3 > 1:3 # 同樣的操作的簡寫(但不太靈活) [1] 1 2 3 > i <- 1:3 # 可以儲存一個向量 > i [1] 1 2 3 > i <- c(i,pi) # 要新增更多元素,必須再次賦值,例如使用 c() > i [1] 1.000000 2.000000 3.000000 3.141593 > i <- c(i, "text") # 向量不能包含不同資料型別,所以 ... > i # ... R 將所有元素轉換為相同型別 [1] "1" "2" "3" "3.14159265358979" "text" > i+1 # 數字現在是文字字串:算術運算不可能 Error in i + 1 : non-numeric argument to binary operator > rep(1, 10) # "rep" 函式重複其第一個引數
[1] 1 1 1 1 1 1 1 1 1 1
> rep(3:1,10) # 第一個引數也可以是一個向量
[1] 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1 3 2 1
> huge.vector <- 0:(10^7) # R 可以輕鬆處理非常大的向量 > #huge.vector # 除非您想打印出 1000 萬個數字,否則不要取消此註釋 > rm(huge.vector) # "rm" 刪除物件。刪除大型未使用的物件是有意義的
- ↑ 這些是 R 中的特殊詞語,不能用作物件的名稱。物件
T和F是TRUE和FALSE的臨時快捷方式,但如果您使用它們,請注意:因為 T 和 F 只是普通的物件名稱,您可以透過覆蓋它們來更改它們的含義。