【问题标题】:What are the standard tools to align printed strings in R?在 R 中对齐打印字符串的标准工具是什么?
【发布时间】:2017-01-24 22:56:19
【问题描述】:

这是一系列cat 声明

words = c("Hello","bar","ROCKnROLL","R","Supercalifragilisticexpialidocious")
for (i in 1:length(words))
{
    cat(paste0(words[i], "\t: ",nchar(words[i]),"\n"))
}

Hello   : 5
bar : 3
ROCKnROLL   : 9
R   : 1
Supercalifragilisticexpialidocious  : 34

我该怎么做才能像这样对齐它们

Hello                               : 5
bar                                 : 3
ROCKnROLL                           : 9
R                                   : 1
Supercalifragilisticexpialidocious  : 34

或者像这样

                             Hello  : 5
                               bar  : 3
                         ROCKnROLL  : 9
                                 R  : 1
Supercalifragilisticexpialidocious  : 34

或者最终会这样

             Hello                  : 5
              bar                   : 3
           ROCKnROLL                : 9
               R                    : 1
Supercalifragilisticexpialidocious  : 34

【问题讨论】:

    标签: r string printing alignment cat


    【解决方案1】:

    试试这个:

    words = c("Hello","bar","ROCKnROLL","R","Supercalifragilisticexpialidocious")
    for (i in 1:length(words)) {
      print(sprintf("%-40s:%d", words[i], nchar(words[i])))
    }
    

    带输出:

    [1] "Hello                                   :5"
    [1] "bar                                     :3"
    [1] "ROCKnROLL                               :9"
    [1] "R                                       :1"
    [1] "Supercalifragilisticexpialidocious      :34"
    

    【讨论】:

    • for() 循环是不必要的。 sprintf("%-40s:%d", words, nchar(words))
    【解决方案2】:

    这基本上是一个固定宽度的文件。 base R没有这个功能(只有read.fwf),但是gdata::write.fwf可以用

    ## create a data frame to write
    words = c("Hello","bar","ROCKnROLL","R","Supercalifragilisticexpialidocious")
    dat <- data.frame(words = words, sep = ':', nchar = nchar(words))
    
    ## write.fwf without a file name will print to the console
    (co <- capture.output(gdata::write.fwf(dat, width = c(max(nchar(words)), 1, 3), justify = 'r'))[-1])
    # [1] "                             Hello :   5" "                               bar :   3"
    # [3] "                         ROCKnROLL :   9" "                                 R :   1"
    # [5] "Supercalifragilisticexpialidocious :  34"
    
    ## cat it
    cat(co, sep = '\n')
    
    #                              Hello :   5
    #                                bar :   3
    #                          ROCKnROLL :   9
    #                                  R :   1
    # Supercalifragilisticexpialidocious :  34
    

    您可以更改对齐方式以获取其他对齐方式

    cat(capture.output(gdata::write.fwf(dat, width = c(max(nchar(words)), 1, 3), justify = 'c'))[-1], sep = '\n')
    #               Hello                :   5
    #                bar                 :   3
    #             ROCKnROLL              :   9
    #                 R                  :   1
    # Supercalifragilisticexpialidocious :  34
    
    cat(capture.output(gdata::write.fwf(dat, width = c(max(nchar(words)), 1, 3), justify = 'l'))[-1], sep = '\n')
    # Hello                              :   5
    # bar                                :   3
    # ROCKnROLL                          :   9
    # R                                  :   1
    # Supercalifragilisticexpialidocious :  34
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      相关资源
      最近更新 更多