R返回数字而不是字符串

igo*_*jrr 3 r

为什么c(...)返回数字而不是下面示例中的字符串?

> Location$NamePrint
[1] Burundi
273 Levels: Afghanistan Africa ...

> ParentLocation$NamePrint
[1] Eastern Africa
273 Levels: Afghanistan Africa ...

> c(Location$NamePrint, ParentLocation$NamePrint)
[1] 36 71
Run Code Online (Sandbox Code Playgroud)

这些数字是关卡中字符串的位置?

我的目标是使用这两个元素(它们的字符串值)创建一个向量 c(Location$NamePrint, ParentLocation$NamePrint)

tyl*_*uRp 7

因为它是一个factor.例如:

x <- as.factor("a")
c(x)

# [1] 1
Run Code Online (Sandbox Code Playgroud)

要解决这个问题,我们可以对待x as.character:

x <- as.character("a")
c(x)

# [1] "a"
Run Code Online (Sandbox Code Playgroud)

正如@joran所提到的,这也有一个方便的功能forcats,forcats::fct_c().

有关?c其他信息,请参阅并阅读详细信息部分:

请注意,因子仅通过其内部整数代码处理; 一个提案已被使用:

x <- as.factor("a")
y <- as.factor("b")

c.factor <- function(..., recursive=TRUE) unlist(list(...), recursive=recursive)

c.factor(x, y)

# [1] a b
# Levels: a b
Run Code Online (Sandbox Code Playgroud)

  • 选项:从字符回到因子的往返或使用方便的`forcats :: fct_c()`函数. (3认同)