Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8721387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:12:49+00:00 2026-06-13T07:12:49+00:00

I would like to print a data frame where the columns are center aligned.

  • 0

I would like to print a data frame where the columns are center aligned. Below is what I have I tried, I thought printing the data frame test1 would result in the columns being aligned in the center but this is not the case. Any thoughts on how I can do this?

test=data.frame(x=c(1,2,3),y=c(5,6,7))
names(test)=c('Variable 1','Variable 2')
test[,1]=as.character(test[,1])
test[,2]=as.character(test[,2])
test1=format(test,justify='centre')
print(test,row.names=FALSE,quote=FALSE)
 Variable 1 Variable 2
          1          5
          2          6
          3          7
print(test1,row.names=FALSE,quote=FALSE)
 Variable 1 Variable 2
          1          5
          2          6
          3          7
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:12:50+00:00Added an answer on June 13, 2026 at 7:12 am

    The problem is that in order for this to work as you expect, the “width” argument needs to also be specified.

    Here’s an example:

    test.1 <- data.frame(Variable.1 = as.character(c(1,2,3)), 
                         Variable.2 = as.character(c(5,6,7)))
    
    # Identify the width of the widest column by column name
    name.width <- max(sapply(names(test.1), nchar))
    format(test.1, width = name.width, justify = "centre")
    #   Variable.1 Variable.2
    # 1     1          5     
    # 2     2          6     
    # 3     3          7  
    

    But, how does this approach work with columns where the variable names are different lengths? Not so well.

    test.2 <- data.frame(A.Really.Long.Variable.Name = as.character(c(1,2,3)), 
                         Short.Name = as.character(c(5,6,7)))
    
    name.width <- max(sapply(names(test.2), nchar))
    format(test.2, width = name.width, justify = "centre")
    #   A.Really.Long.Variable.Name                  Short.Name
    # 1              1                           5             
    # 2              2                           6             
    # 3              3                           7             
    

    There is, of course, a workaround: change the “width” of each variable name to be equal lengths by padding them with spaces (using format())

    orig.names <- names(test.2) # in case you want to restore the original names
    names(test.2) <- format(names(test.2), width = name.width, justify = "centre")
    format(test.2, width = name.width, justify = "centre")
    #   A.Really.Long.Variable.Name         Short.Name         
    # 1              1                           5             
    # 2              2                           6             
    # 3              3                           7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The data frame has n columns and I would like to get n plots,
I have a container class which I would like to use to print all
I'd like to write some code that would take a given data frame, check
I have a large data frame (named z ) that looks like this: RPos
I have a dataframe called data where I would like to rescale the values
I have the following question: I have data frame which looks like this. I
I have a large .txt with data in bad formats. I would like to
I have plotted the following data and added a loess smoother. I would like
I would like to compare two lists (two rows of a data frame) and
I have a data frame like this: df = data.frame(a=1:3, b=2:4, c=3:5) I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.