1

我有一个数据框,其中一列是字符串:

> head(a$type)
[1] Sell Sell Sell Buy  Buy  Buy 
Levels: Buy Sell

当我append使用它时,它会将所有内容转换为 int:

> head(append(a$type, "Buy"))
[1] "2" "2" "2" "1" "1" "1"

为什么会发生这种情况,我该如何预防?

4

1 回答 1

2

您的a$type变量实际上是一个因素。要将因子转换为字符,请使用

a$type = as.character(a$type)

然后您的附加命令应该可以工作。

于 2013-07-19T21:17:13.480 回答