使用 R 語言更改 xtab 表格中列變數和行變數的名稱。
要更改 xtab 表格中列變數和行變數的名稱,我們可以使用 setNames 函式。
例如,如果我們有一個名為 XTAB 的 xtab 表格,並且我們想要更改列變數名為 C 和行變數名為 R,則可以使用以下命令:
dimnames(XTAB)<-setNames(dimnames(XTAB),c("R","C"))示例 1
以下程式碼片段建立了一個示例資料框:
Table1<-xtabs(~as.factor(letters[1:5])+letters[1:5]) Table1
建立了以下資料框
letters[1:5]
as.factor(letters[1:5]) a b c d e
a 1 0 0 0 0
b 0 1 0 0 0
c 0 0 1 0 0
d 0 0 0 1 0
e 0 0 0 0 1要在上面建立的資料框的 Table1 中更改列和行變數名稱,請將以下程式碼新增到上述程式碼片段中:
Table1<-xtabs(~as.factor(letters[1:5])+letters[1:5])
dimnames(Table1)<-setNames(dimnames(Table1),c("Rows","Columns"))
Table1輸出
如果您將以上所有給定的程式碼片段作為單個程式執行,它將生成以下輸出:
Columns Rows a b c d e a 1 0 0 0 0 b 0 1 0 0 0 c 0 0 1 0 0 d 0 0 0 1 0 e 0 0 0 0 1
示例 2
以下程式碼片段建立了一個示例資料框:
Cat1<-sample(c("Hot","Cold"),20,replace=TRUE)
Cat2<-sample(c("Low","Medium","High"),20,replace=TRUE)
dat1<-data.frame(Cat1,Cat2)
dat1建立了以下資料框
Cat1 Cat2 1 Cold High 2 Hot High 3 Hot High 4 Hot High 5 Cold Low 6 Hot Low 7 Hot Medium 8 Hot Low 9 Cold Medium 10 Hot High 11 Hot Low 12 Cold Low 13 Hot Low 14 Hot Low 15 Hot Medium 16 Cold Medium 17 Hot Medium 18 Hot Low 19 Hot High 20 Cold Medium
將以下程式碼新增到上述程式碼片段中:
Cat1<-sample(c("Hot","Cold"),20,replace=TRUE)
Cat2<-sample(c("Low","Medium","High"),20,replace=TRUE)
dat1<-data.frame(Cat1,Cat2)
Table2<-xtabs(~Cat1+Cat2,data=dat1)
Table2輸出
如果您將以上所有給定的程式碼片段作為單個程式執行,它將生成以下輸出:
Cat2 Cat1 High Low Medium Cold 1 2 3 Hot 5 6 3
要在上面建立的資料框的 Table2 中更改列和行變數名稱,請將以下程式碼新增到上述程式碼片段中:
Cat1<-sample(c("Hot","Cold"),20,replace=TRUE)
Cat2<-sample(c("Low","Medium","High"),20,replace=TRUE)
dat1<-data.frame(Cat1,Cat2)
Table2<-xtabs(~Cat1+Cat2,data=dat1)
dimnames(Table2)<-setNames(dimnames(Table2),c("Group1","Group2"))
Table2輸出
如果您將以上所有給定的程式碼片段作為單個程式執行,它將生成以下輸出:
Group2 Group1 High Low Medium Cold 1 2 3 Hot 5 6 3
示例 3
以下程式碼片段建立了一個示例資料框:
Level_1<-sample(c("Upper","Lower"),20,replace=TRUE)
Level_2<-sample(c("Super","top-notch"),20,replace=TRUE)
dat2<-data.frame(Level_1,Level_2)
dat2建立了以下資料框
Level_1 Level_2 1 Upper top-notch 2 Lower Super 3 Lower top-notch 4 Lower Super 5 Upper top-notch 6 Upper top-notch 7 Lower top-notch 8 Lower top-notch 9 Upper top-notch 10 Lower top-notch 11 Upper Super 12 Upper top-notch 13 Lower top-notch 14 Lower top-notch 15 Upper top-notch 16 Lower top-notch 17 Upper top-notch 18 Lower top-notch 19 Lower Super 20 Upper Super
將以下程式碼新增到上述程式碼片段中:
Level_1<-sample(c("Upper","Lower"),20,replace=TRUE)
Level_2<-sample(c("Super","top-notch"),20,replace=TRUE)
dat2<-data.frame(Level_1,Level_2)
Table3<-xtabs(~Level_1+Level_2,data=dat2)
Table3輸出
如果您將以上所有給定的程式碼片段作為單個程式執行,它將生成以下輸出:
Level_2 Level_1 Super top-notch Lower 3 8 Upper 2 7
要在上面建立的資料框的 Table3 中更改列和行變數名稱,請將以下程式碼新增到上述程式碼片段中:
Level_1<-sample(c("Upper","Lower"),20,replace=TRUE)
Level_2<-sample(c("Super","top-notch"),20,replace=TRUE)
dat2<-data.frame(Level_1,Level_2)
Table3<-xtabs(~Level_1+Level_2,data=dat2)
dimnames(Table3)<-setNames(dimnames(Table3),c("Minor","Major"))
Table3輸出
如果您將以上所有給定的程式碼片段作為單個程式執行,它將生成以下輸出:
Major Minor Super top-notch Lower 3 8 Upper 2 7
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP