R Programing to change the name of column variable and row variable in xtab table.


To change the name of column variable and row variable in xtab table, we can use setNames function.

For Example, if we have a xtab table called XTAB and we want to change the column variable name C and row variable name R then we can use the below command −

dimnames(XTAB)<-setNames(dimnames(XTAB),c("R","C"))

Example 1

Following snippet creates a sample data frame −

Table1<-xtabs(~as.factor(letters[1:5])+letters[1:5])
Table1

The following dataframe is created

           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

To change column and row variable name in Table1 on the above created data frame, add the following code to the above snippet −

Table1<-xtabs(~as.factor(letters[1:5])+letters[1:5])
dimnames(Table1)<-setNames(dimnames(Table1),c("Rows","Columns"))
Table1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

Example 2

Following snippet creates a sample data frame −

Cat1<-sample(c("Hot","Cold"),20,replace=TRUE)
Cat2<-sample(c("Low","Medium","High"),20,replace=TRUE)
dat1<-data.frame(Cat1,Cat2)
dat1

The following dataframe is created

  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

Add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  Cat2
Cat1 High Low Medium
Cold   1   2   3
Hot    5   6   3

To change column and row variable name in Table2 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  Group2
Group1 High Low Medium
Cold     1    2   3
Hot      5    6   3

Example 3

Following snippet creates a sample data frame −

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

The following dataframe is created

 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

Add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    Level_2
Level_1 Super top-notch
Lower      3      8
Upper      2      7

To change column and row variable name in Table3 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     Major
Minor Super top-notch
Lower    3    8
Upper    2    7

Updated on: 02-Nov-2021

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements