我的文本片段分为三列。我想在R控制台的KWIC concordance format中显示这三列,以进行快速的“可视化分析”。为此,第一列需要右对齐,中间一列居中,第三列要左对齐。
以下是一些示例数据来演示该问题:
concordance1 <- c("Well it's not that easy as you can tell I was trying to work out how this", "is working", "but I can't say I understand yet")
concordance2 <- c("they've just", "been having", "more and more trouble")
concordance3 <- c(" sorry I wasn't really engaging Um I", "was thinking", "back to like youth club days...")
data <- as.data.frame(rbind(concordance1, concordance2, concordance3))
如果我仅打印数据,则所有三列都左对齐,并且R在几行上显示一致行,因此完全难以辨认。
data
到目前为止,小标题显示是我所能找到的最好的显示。它将表格的宽度调整为我的控制台显示(请参见屏幕截图),这是一个改进。这是一个开始,但是为了快速分析这些一致性,我需要能够在小标题显示中右对齐第一列。
对于任何可能实现的提示,我深表感谢。谢谢!
答案 0 :(得分:1)
您可以尝试以下方法:
print(mapply(format, data, justify=c("right", "centre", "left")), quote=F)
编辑:
要将左列截断为指定的最大宽度,可以将其包装在函数中,如下所示:
format.kwic <- function(data, width=20) {
trunc <- function(x, n=20) {
x <- as.character(x)
w <- nchar(x)
ifelse(w > n, paste0("\U2026", substring(x, w-n, w)), x)
}
data[,1] <- trunc(data[,1], n=width)
mapply(format, data, justify=c("right", "centre", "left"))
}
print(format.kwic(data), quote=F)
输出:
## V1 V2 V3
## [1,] … to work out how this is working but I can't say I understand yet
## [2,] they've just been having more and more trouble
## [3,] … really engaging Um I was thinking back to like youth club days...