在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ R語言向量
R語言列表
R語言隨機(jī)森林
R語言矩陣
R語言邏輯回歸
R語言數(shù)據(jù)幀
R語言數(shù)據(jù)重塑
R語言概述
R語言包
R語言字符串
R語言CSV文件
R語言運(yùn)算符
為什么使用R語言做統(tǒng)計(jì)?
R語言Web數(shù)據(jù)
R語言二進(jìn)制文件
R語言XML文件
R語言JSON文件
R語言因子
R語言容易學(xué)習(xí)嗎?
R語言基礎(chǔ)語法
R語言向量
R語言教程
R語言正態(tài)分布
R語言平均值,中位數(shù)和眾數(shù)
R語言變量
R語言條形圖
R語言決策樹
R語言開發(fā)環(huán)境安裝配置
R語言數(shù)組
R語言數(shù)據(jù)類型
R語言非線性最小二乘法
R語言直方圖
R語言卡方檢驗(yàn)
R語言泊松回歸
R語言決策結(jié)構(gòu)
R語言盒形圖(箱形圖)
R語言協(xié)方差分析
R語言二項(xiàng)分布
R語言餅狀圖
R語言循環(huán)
R語言散點(diǎn)圖
R語言線性回歸
R語言時間序列分析
R語言線形圖
R語言在現(xiàn)實(shí)中的應(yīng)用
R語言生存分析
R語言多元(多重)回歸
R語言函數(shù)
R語言Excel文件
R語言連接數(shù)據(jù)庫(MySQL)

R語言向量

向量是最基本的R數(shù)據(jù)對象,有六種類型的原子向量。它們分別是邏輯,整數(shù),雙重,復(fù)雜,字符和原始。

創(chuàng)建矢量

1.單元素矢量

即使在R中只寫入一個值,它也會被認(rèn)為是一個長度為1的向量,屬于上述向量類型之一。

# Atomic vector of type character.
print("abc");

# Atomic vector of type double.
print(12.5)

# Atomic vector of type integer.
print(63L)

# Atomic vector of type logical.
print(TRUE)

# Atomic vector of type complex.
print(2+3i)

# Atomic vector of type raw.
print(charToRaw('hello'))

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1] "abc"
[1] 12.5
[1] 63
[1] TRUE
[1] 2+3i
[1] 68 65 6c 6c 6f

2.多元素向量

使用冒號運(yùn)算符與數(shù)字?jǐn)?shù)據(jù) -

# Creating a sequence from 5 to 13.
v <- 5:13
print(v)

# Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v)

# If the final element specified does not belong to the sequence then it is discarded.
v <- 3.8:11.4
print(v)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1]  5  6  7  8  9 10 11 12 13
[1]  6.6  7.6  8.6  9.6 10.6 11.6 12.6
[1]  3.8  4.8  5.8  6.8  7.8  8.8  9.8 10.8

使用序列(Seq.)運(yùn)算符

# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by = 0.4))

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0

使用c()函數(shù)

如果其中一個元素是字符,則非字符值被強(qiáng)制為字符類型。

# The logical and numeric values are converted to characters.
s <- c('apple','red',5,TRUE)
print(s)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1] "apple" "red"   "5"     "TRUE"

訪問向量元素

使用索引訪問向量的元素。[]括號用于指定索引。索引從位置1開始。在索引中賦予負(fù)值會從結(jié)果中刪除該元素。TRUE,FALSE01也可用于索引。

# Accessing vector elements using position.
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]
print(u)

# Accessing vector elements using logical indexing.
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)

# Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]
print(x)

# Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)]
print(y)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1] "Mon" "Tue" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"
`

向量操作

向量算術(shù)

可以將相同長度的兩個向量相加,減去,相乘或相除,給出結(jié)果作為向量輸出。如下示例 -

# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)

# Vector addition.
add.result <- v1+v2
print(add.result)

# Vector substraction.
sub.result <- v1-v2
print(sub.result)

# Vector multiplication.
multi.result <- v1*v2
print(multi.result)

# Vector division.
divi.result <- v1/v2
print(divi.result)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1]  7 19  4 13  1 13
[1] -1 -3  4 -3 -1  9
[1] 12 88  0 40  0 22
[1] 0.7500000 0.7272727       Inf 0.6250000 0.0000000 5.5000000

向量元素回收

如果我們對兩個不等長度的向量應(yīng)用算術(shù)運(yùn)算,則將循環(huán)使用較短向量的元素來完成操作。

v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)

add.result <- v1+v2
print(add.result)

sub.result <- v1-v2
print(sub.result)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1]  7 19  8 16  4 22
[1] -1 -3  0 -6 -4  0

向量元素排序

可以使用sort()函數(shù)對向量中的元素進(jìn)行排序。

v <- c(3,8,4,5,0,11, -9, 304)

# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result)

# Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

# Sorting character vectors.
v <- c("Red","Blue","yellow","violet")
sort.result <- sort(v)
print(sort.result)

# Sorting character vectors in reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

當(dāng)我們執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果 -

[1]  -9   0   3   4   5   8  11 304
[1] 304  11   8   5   4   3   0  -9
[1] "Blue"   "Red"    "violet" "yellow"
[1] "yellow" "violet" "Red"    "Blue"

上一篇:R語言運(yùn)算符下一篇:R語言教程