根据某些字符垂直对齐R中数据帧的列的元素,例如空间

问题描述 投票:-1回答:2

我想基于每列的第一个元素的最后一个数字垂直对齐R数据帧中列的元素。

我怎样才能做到这一点?

编辑:改述:在R中,使用R代码,我想创建将在R控制台中作为输出产生的代码,将垂直对齐左右,例如括号,取决于最长的列元素,在括号前左/右加1个额外的空格键。见下面的例子:

示例df:

ugly_not_aligned_column <- structure(list(structure(c(2L, 13L, 8L, 7L, 9L, 6L, 5L, 10L, 3L, 12L, 1L, 14L, 4L, 11L), .Label = c("14 (55)", "20 (56)", "25.1 (72)", "2.79 (75)", "34.4 (97)", "9.29 (110)", "4.6 (125)", "55.36 (155)", "601 (170)", "65 (183)", "72 (205)", "7.29 (208)", "80 (224)", "806 (225)"), class = "factor")), row.names = c(NA, -14L), class = "data.frame")

> EDIT: The arrangement of parenthesis are not aligned vertically in the last column because, the lenght of column elements differ e.g. 224 vs 56. 

1      20 (56)
2     80 (224)
3  55.36 (155)
4    4.6 (125)
5    601 (170)
6   9.29 (110)
7    34.4 (97)
8     65 (183)
9    25.1 (72)
10  7.29 (208)
11     14 (55)
12   806 (225)
13   2.79 (75)
14    72 (205)

编辑:垂直对齐的括号示例,例如: 20具有比柱元件26.67更短的长度,然而,括号在垂直方向上对齐。

1可能(46.67)2否(26.67)3是(26.67)4可能(13.33)5否(73.33)6是(13.33)7可能(20)8否(40)9是(40)

r vertical-alignment text-align
2个回答
1
投票

编辑:根据评论编辑。

此代码添加尾随空格,因此所有元素都具有相同的长度。

df <- structure(list(structure(c(2L, 13L, 8L, 7L, 9L, 6L, 5L, 10L, 3L, 12L, 1L, 14L, 4L, 11L), .Label = c("14 (55)", "20 (56)", "25.1 (72)", "2.79 (75)", "34.4 (97)", "9.29 (110)", "4.6 (125)", "55.36 (155)", "601 (170)", "65 (183)", "72 (205)", "7.29 (208)", "80 (224)", "806 (225)"), class = "factor")), row.names = c(NA, -14L), class = "data.frame")

library(tidyr)
library(dplyr)
colnames(df) = "text"
df %>% separate(text, c("number1", "number2"), " ")

打印输出(df):

   number1 number2
1       20    (56)
2       80   (224)
3    55.36   (155)
4      4.6   (125)
5      601   (170)
6     9.29   (110)
7     34.4    (97)
8       65   (183)
9     25.1    (72)
10    7.29   (208)
11      14    (55)
12     806   (225)
13    2.79    (75)
14      72   (205)

希望这可以帮助!


0
投票

我写了一个R函数,当有人想要将两列连接在一起时,他/她希望第一列的元素根据它们的最后一个字符/数字对齐,那么这个R函数就可以做到。

但是,这个R函数做了一些与要求不同的东西 - 当这些元素已经从两个不同的df列连接时,对齐列的元素。尽管如此,我在这里发布了与我有类似问题的其他人的书面R函数。

df <- list(structure(list(ID_no = c(2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 
    4L), Variable = c("practice", "", "", "usefullness", "", "", 
    "stress", "", ""), Levels = structure(c(3L, 4L, 5L, 3L, 4L, 5L, 
    3L, 4L, 5L), .Label = c("female", "male", "maybe", "no", "yes"
    ), class = "factor"), Frequency = c(7, 4, 4, 2, 11, 2, 3, 6, 
    6), Percentage = c(46.67, 26.67, 26.67, 13.33, 73.33, 13.33, 
    20, 40, 40), y_place = c(76.5, 39.5, 13, 92.5, 49.5, 6.5, 90, 
    60, 20)), .Names = c("ID_no", "Variable", "Levels", "Frequency", 
    "Percentage", "y_place"), row.names = 3:11, class = "data.frame"))


   ID_no    Variable Levels Frequency Percentage y_place
3      2    practice  maybe         7      46.67    76.5
4      2                 no         4      26.67    39.5
5      2                yes         4      26.67    13.0
6      3 usefullness  maybe         2      13.33    92.5
7      3                 no        11      73.33    49.5
8      3                yes         2      13.33     6.5
9      4      stress  maybe         3      20.00    90.0
10     4                 no         6      40.00    60.0
11     4                yes         6      40.00    20.0


Aligned_Column_Join <- function(df, column_1, column_2) {

# Producing some spaces in order to make nice looking printed datasets
      df_col1      <- df[,column_1]
  count_chars      <- nchar(as.character(df_col1))
    max_count      <- max( count_chars )
       spaces      <- unlist( lapply( max_count - count_chars, function(x) paste0(if (x==0) { "" } else { rep(" ", x) }, collapse="") ))
df_aligned_col1    <- paste0( spaces, df_col1 ) 

     df_col2       <- df[,column_2]
  count_chars      <- nchar(as.character(df_col2))
    max_count      <- max( count_chars )
       spaces      <- unlist(lapply( max_count - count_chars, function(x) paste0(if (x==0) { "" } else { rep(" ", x) }, collapse="") ) )
df_aligned_col2    <- paste0(spaces, df_col2 ) 


    # Frequency and percentage columns made together
    aligned_elements <-    unname(as.data.frame(
                           paste(df_aligned_col1, " (",  
                           df_aligned_col2, ")" ))) 


    aligned_elements
}

命令:

Aligned_Column_Join(df,3,5)

输出:

1 maybe  ( 46.67 )
2    no  ( 26.67 )
3   yes  ( 26.67 )
4 maybe  ( 13.33 )
5    no  ( 73.33 )
6   yes  ( 13.33 )
7 maybe  (    20 )
8    no  (    40 )
9   yes  (    40 )

命令:

Aligned_Column_Join(df,4,5)

输出:

1  7  ( 46.67 )
2  4  ( 26.67 )
3  4  ( 26.67 )
4  2  ( 13.33 )
5 11  ( 73.33 )
6  2  ( 13.33 )
7  3  (    20 )
8  6  (    40 )
9  6  (    40 )
© www.soinside.com 2019 - 2024. All rights reserved.