I have a matrix 'dd' defined as below: dd <- t(matrix(c(153.0216306, 1, 7.578366e-35, 13.3696538, 1, 5.114571e-04, 0.8476713, 1, 7.144239e-01, 1.2196050, 1, 5.388764e-01, 2.6349405, 1, 2.090719e-01, 6.0507714, 1, 2.780045e-02), nrow=3, ncol=6)) dimnames(dd)[[2]] <- c('# Chisq', 'DF', 'Pr(>Chisq)') dimnames(dd)[[1]] <- c('# Sex', '# Volume', '# Weight', '# Intensity', '# ISO', '# SEC') 'dd' displays as the following: # Chisq DF Pr(>Chisq) # Sex 153.0216306 1 7.578366e-35 # Volume 13.3696538 1 5.114571e-04 # Weight 0.8476713 1 7.144239e-01 # Intensity 1.2196050 1 5.388764e-01 # ISO 2.6349405 1 2.090719e-01 # SEC 6.0507714 1 2.780045e-02 I would like to display it as: # Chisq DF Pr(>Chisq) term 153.0216306 1 7.578366e-35 # Sex 13.3696538 1 5.114571e-04 # Volume 0.8476713 1 7.144239e-01 # Weight 1.2196050 1 5.388764e-01 # Intensity 2.6349405 1 2.090719e-01 # ISO 6.0507714 1 2.780045e-02 # SEC This is what I came up with (cc <- data.frame(data.frame(dd), term=dimnames(dd)[[1]])) X..Chisq DF Pr..Chisq. term # Sex 153.0216306 1 7.578366e-35 # Sex # Volume 13.3696538 1 5.114571e-04 # Volume # Weight 0.8476713 1 7.144239e-01 # Weight # Intensity 1.2196050 1 5.388764e-01 # Intensity # ISO 2.6349405 1 2.090719e-01 # ISO # SEC 6.0507714 1 2.780045e-02 # SEC But I'm not happy with the following two issues: 1) How to get rid of the row names? 2) The special characters of #, (, >,) in the column names are not displayed correctly. Any suggestions? Thanks, Gang
Not elegant, but it works:> term <- dimnames(dd)[[1]] > dd1 <- dd > dimnames(dd1)[[1]] <- rep("", 6) > dd2 <- capture.output(dd1) > cat(paste(dd2, " ", c("Term", term)), fill=48)# Chisq DF Pr(>Chisq) Term 153.0216306 1 7.578366e-35 # Sex 13.3696538 1 5.114571e-04 # Volume 0.8476713 1 7.144239e-01 # Weight 1.2196050 1 5.388764e-01 # Intensity 2.6349405 1 2.090719e-01 # ISO 6.0507714 1 2.780045e-02 # SEC David Carlson -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Gang Chen Sent: Thursday, July 3, 2014 2:56 PM To: r-help Subject: [R] Display a dataframe I have a matrix 'dd' defined as below: dd <- t(matrix(c(153.0216306, 1, 7.578366e-35, 13.3696538, 1, 5.114571e-04, 0.8476713, 1, 7.144239e-01, 1.2196050, 1, 5.388764e-01, 2.6349405, 1, 2.090719e-01, 6.0507714, 1, 2.780045e-02), nrow=3, ncol=6)) dimnames(dd)[[2]] <- c('# Chisq', 'DF', 'Pr(>Chisq)') dimnames(dd)[[1]] <- c('# Sex', '# Volume', '# Weight', '# Intensity', '# ISO', '# SEC') 'dd' displays as the following: # Chisq DF Pr(>Chisq) # Sex 153.0216306 1 7.578366e-35 # Volume 13.3696538 1 5.114571e-04 # Weight 0.8476713 1 7.144239e-01 # Intensity 1.2196050 1 5.388764e-01 # ISO 2.6349405 1 2.090719e-01 # SEC 6.0507714 1 2.780045e-02 I would like to display it as: # Chisq DF Pr(>Chisq) term 153.0216306 1 7.578366e-35 # Sex 13.3696538 1 5.114571e-04 # Volume 0.8476713 1 7.144239e-01 # Weight 1.2196050 1 5.388764e-01 # Intensity 2.6349405 1 2.090719e-01 # ISO 6.0507714 1 2.780045e-02 # SEC This is what I came up with (cc <- data.frame(data.frame(dd), term=dimnames(dd)[[1]])) X..Chisq DF Pr..Chisq. term # Sex 153.0216306 1 7.578366e-35 # Sex # Volume 13.3696538 1 5.114571e-04 # Volume # Weight 0.8476713 1 7.144239e-01 # Weight # Intensity 1.2196050 1 5.388764e-01 # Intensity # ISO 2.6349405 1 2.090719e-01 # ISO # SEC 6.0507714 1 2.780045e-02 # SEC But I'm not happy with the following two issues: 1) How to get rid of the row names? 2) The special characters of #, (, >,) in the column names are not displayed correctly. Any suggestions? Thanks, Gang ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi, May be this helps: nC <- max(nchar(row.names(dd))) ?term <- formatC(row.names(dd), width=-nC) #or ?term <- sprintf("%-11s", row.names(dd)) ? dd1 <- setNames(data.frame(unname(dd), term,stringsAsFactors=F), c(colnames(dd), formatC("term",width=-nC))) dd1 #????? # Chisq DF?? Pr(>Chisq) term?????? #1 153.0216306? 1 7.578366e-35 # Sex????? #2? 13.3696538? 1 5.114571e-04 # Volume?? #3?? 0.8476713? 1 7.144239e-01 # Weight?? #4?? 1.2196050? 1 5.388764e-01 # Intensity #5?? 2.6349405? 1 2.090719e-01 # ISO????? #6?? 6.0507714? 1 2.780045e-02 # SEC????? A.K. On Thursday, July 3, 2014 3:57 PM, Gang Chen <gangchen6 at gmail.com> wrote: I have a matrix 'dd' defined as below: dd <- t(matrix(c(153.0216306,? 1, 7.578366e-35, 13.3696538,? 1, 5.114571e-04, 0.8476713,? 1, 7.144239e-01, 1.2196050,? 1, 5.388764e-01, 2.6349405,? 1, 2.090719e-01, 6.0507714,? 1, 2.780045e-02), nrow=3, ncol=6)) dimnames(dd)[[2]] <- c('# Chisq', 'DF', 'Pr(>Chisq)') dimnames(dd)[[1]] <- c('# Sex', '# Volume', '# Weight', '# Intensity', '# ISO', '# SEC') 'dd' displays as the following: ? ? ? ? ? ? ? ? # Chisq DF? Pr(>Chisq) # Sex? ? ? 153.0216306? 1 7.578366e-35 # Volume? ? 13.3696538? 1 5.114571e-04 # Weight? ? ? 0.8476713? 1 7.144239e-01 # Intensity? 1.2196050? 1 5.388764e-01 # ISO? ? ? ? 2.6349405? 1 2.090719e-01 # SEC? ? ? ? 6.0507714? 1 2.780045e-02 I would like to display it as: # Chisq? ? ? ? ? ? ? DF? Pr(>Chisq)? ? ? ? ? ? ? ? ? ? ? ? term 153.0216306? 1 7.578366e-35? ? ? ? ? ? ? ? ? ? ? ? ? ? # Sex 13.3696538? 1 5.114571e-04? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Volume 0.8476713? 1 7.144239e-01? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Weight 1.2196050? 1 5.388764e-01? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Intensity 2.6349405? 1 2.090719e-01? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # ISO 6.0507714? 1 2.780045e-02? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # SEC This is what I came up with (cc <- data.frame(data.frame(dd), term=dimnames(dd)[[1]])) ? ? ? ? ? ? ? X..Chisq DF? Pr..Chisq.? ? ? ? term # Sex? ? ? 153.0216306? 1 7.578366e-35? ? ? # Sex # Volume? ? 13.3696538? 1 5.114571e-04? ? # Volume # Weight? ? ? 0.8476713? 1 7.144239e-01? ? # Weight # Intensity? 1.2196050? 1 5.388764e-01 # Intensity # ISO? ? ? ? 2.6349405? 1 2.090719e-01? ? ? # ISO # SEC? ? ? ? 6.0507714? 1 2.780045e-02? ? ? # SEC But I'm not happy with the following two issues: 1) How to get rid of the row names? 2) The special characters of #, (, >,) in the column names are not displayed correctly. Any suggestions? Thanks, Gang ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.