

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Questions & Answers
- How to change the position of axes titles to top for X-variable and to right for Y-variable in R?
- How to change the name of single column using setNames in R?
- Updating a MySQL table row column by appending a value from user defined variable?
- How to convert a data frame into two column data frame with values and column name as variable in R?
- How to change the column names and row names of a data frame in R?
- How to convert data frame into two column data frame with values excluding NAs and column name as variable in R?
- Set user-defined variable with table name in MySQL prepare statement?
- Find the column name of least value in each row of an R dataframe.
- How to create a column with binary variable based on a condition of other variable in an R data frame?
- How to print a variable name in C?
- Change the column name from a MySQL table with Student record?
- How to find the row sum for each column by row name in an R matrix?
- How to load a JavaScript function using the variable name?
- How can I change the name of an existing column from a MySQL table?
- How to fix the coefficient of an independent variable in R?