Search code examples
rvertical-alignmenttext-align

Align vertically the elements of a column of a dataframe in R based on some character e.g. space


I want to align vertically the elements of a column in an R dataframe based on the last digit of the first element of each column.

How can I do that?

EDIT: Rephrasing: In R, using R code, I want to create code that will result as output in R console, will vertically align the left and right e.g. parentheses, depending on the longest in length column element, plus 1 extra space pad left/right before parentheses. See the below example:

Example 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)

EDIT: Example of parenthesis that are vertically aligned e.g. 20 has a shorter lenght form the column element 26.67, howevr, parenthesis are aligned vertically.

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 )


Solution

  • EDIT: Edited based on comment.

    This code adds trailing spaces, so all elements have the same length.

    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"), " ")
    

    Output of print(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)
    

    Hope this helps!