以逗号分隔的单个字符获取因子水平

时间:2019-04-01 04:14:23

标签: r csv

我有一个数据帧列是因素的情况。 levels(df$col1)将使我得到的级别包含双引号。

> levels(risk_register_data$Division)
[1] "Commercial"    "Group Finance"

> as.character(levels(risk_register_data$Division))
[1] "Commercial"    "Group Finance"

> csv_char <- as.character(levels(risk_register_data$Division))
> csv_char
[1] "Commercial"    "Group Finance"
> paste("Levels are: ", csv_char)
[1] "Levels are: Commercial"    "Levels are: Group Finance"

我想要以这种方式输出,以便可以将其用作ggplot的标题,注释或文本:

> csv_char
> [1] "Levels are: Commercial, Group Finance"

任何建议,如何完成?

1 个答案:

答案 0 :(得分:1)

@ Maurits-Evers 也建议我们可以使用toString

示例

lev <- paste0("Levels are: ", toString(levels(iris$Species)))
plot(iris[c(1, 4)], col=iris$Species, main="Iris", sub=lev, cex.sub=.8)

enter image description here