根据某些字符,例如,垂直对齐 R 中数据框的列的元素space
Align vertically the elements of a column of a dataframe in R based on some character e.g. space
我想根据每列第一个元素的最后一位数字垂直对齐 R 数据框中的列元素。
我该怎么做?
编辑:改写:在 R 中,使用 R 代码,我想创建将在 R 控制台中输出的代码,将左右垂直对齐,例如括号,取决于长度最长的列元素,在括号前加上 1 个额外的 space 填充 left/right。请参阅以下示例:
示例 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 )
EDIT: Edited based on comment.
此代码添加了尾随空格,因此所有元素的长度都相同。
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)
希望对您有所帮助!
我写了一个 R 函数,当有人想要将两列连接在一起时,他/她希望第一列的元素对齐
基于他们的最后一个字符/数字,那么这个 R 函数可以做到这一点。
但是,此 R 函数执行的操作与所要求的有所不同 - 当这些元素已经从两个不同的 df 列连接时对齐列的元素。不过,我post这里是为与我有类似问题的其他人编写的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 )
我想根据每列第一个元素的最后一位数字垂直对齐 R 数据框中的列元素。
我该怎么做?
编辑:改写:在 R 中,使用 R 代码,我想创建将在 R 控制台中输出的代码,将左右垂直对齐,例如括号,取决于长度最长的列元素,在括号前加上 1 个额外的 space 填充 left/right。请参阅以下示例:
示例 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 )
EDIT: Edited based on comment.
此代码添加了尾随空格,因此所有元素的长度都相同。
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)
希望对您有所帮助!
我写了一个 R 函数,当有人想要将两列连接在一起时,他/她希望第一列的元素对齐 基于他们的最后一个字符/数字,那么这个 R 函数可以做到这一点。
但是,此 R 函数执行的操作与所要求的有所不同 - 当这些元素已经从两个不同的 df 列连接时对齐列的元素。不过,我post这里是为与我有类似问题的其他人编写的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 )