欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語(yǔ)言中cbind、rbind和merge函數(shù)的使用與區(qū)別

 更新時(shí)間:2021年03月11日 16:48:17   作者:游騎小兵  
這篇文章主要介紹了R語(yǔ)言中cbind、rbind和merge函數(shù)的使用與區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

cbind: 根據(jù)列進(jìn)行合并,即疊加所有列,m列的矩陣與n列的矩陣cbind()最后變成m+n列,合并前提:cbind(a, c)中矩陣a、c的行數(shù)必需相符

rbind: 根據(jù)行進(jìn)行合并,就是行的疊加,m行的矩陣與n行的矩陣rbind()最后變成m+n行,合并前提:rbind(a, c)中矩陣a、c的列數(shù)必需相符

> a <- matrix(1:12, 3, 4)
> print(a)
   [,1] [,2] [,3] [,4]
[1,]  1  4  7  10
[2,]  2  5  8  11
[3,]  3  6  9  12
> 
> b <- matrix(-1:-12, 3, 4)
> print(b)
   [,1] [,2] [,3] [,4]
[1,]  -1  -4  -7 -10
[2,]  -2  -5  -8 -11
[3,]  -3  -6  -9 -12
> 
> x=cbind(a,b)
> print(x)
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]  1  4  7  10  -1  -4  -7 -10
[2,]  2  5  8  11  -2  -5  -8 -11
[3,]  3  6  9  12  -3  -6  -9 -12
> 
> y=rbind(a,b)
> print(y)
   [,1] [,2] [,3] [,4]
[1,]  1  4  7  10
[2,]  2  5  8  11
[3,]  3  6  9  12
[4,]  -1  -4  -7 -10
[5,]  -2  -5  -8 -11
[6,]  -3  -6  -9 -12
> 
> 
> c <- matrix(-1:-20, 4, 5)
> print(c)
   [,1] [,2] [,3] [,4] [,5]
[1,]  -1  -5  -9 -13 -17
[2,]  -2  -6 -10 -14 -18
[3,]  -3  -7 -11 -15 -19
[4,]  -4  -8 -12 -16 -20
> 
> x2=cbind(a,c)
Error in cbind(a, c) : 矩陣的行數(shù)必需相符(見(jiàn)arg2)
> print(x2)
   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]  1  4  7  10  -1  -4  -7 -10 -13
[2,]  2  5  8  11  -2  -5  -8 -11 -14
[3,]  3  6  9  12  -3  -6  -9 -12 -15
> 
> y2=rbind(a,c)
Error in rbind(a, c) : 矩陣的列數(shù)必需相符(見(jiàn)arg2)
> print(y2)
Error in print(y2) : 找不到對(duì)象'y2'
> 

merge函數(shù)

兩個(gè)數(shù)據(jù)框擁有相同的時(shí)間或觀測(cè)值,但這些列卻不盡相同。處理的辦法就是使用
merge(x, y ,by.x = ,by.y = ,all = ) 函數(shù)。

#merge/合并
ID<-c(1,2,3,4)
name<-c(“A”,”B”,”C”,”D”)
score<-c(60,70,80,90)
student1<-data.frame(ID,name)
student2<-data.frame(ID,score)
total_student1<-merge(student1,student2,by=”ID”)
total_student1

當(dāng)我們需要將相同的觀測(cè)對(duì)象得出的不同類型變量合并時(shí),則采用cbind,也就是合并columm。

到此這篇關(guān)于R語(yǔ)言中cbind、rbind和merge函數(shù)的使用與區(qū)別的文章就介紹到這了,更多相關(guān)R語(yǔ)言 cbind、rbind和merge內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論