R - factor()函数将我的整数递增1

时间:2016-03-09 05:53:11

标签: r

factor()函数将我的零增加为1和1增加到2。这是一个简单的例子:

# Create some integers
ints <- as.integer(c(1,0,1))
ints
[1] 1 0 1

# Convert these to factors
f_ints <- factor(ints)
f_ints 
[1] 1 0 1
Levels: 0 1

# Great! Let's just check one more thing...
str(f_ints)
Factor w/ 2 levels "0","1": 2 1 2

# Woah, whats up with those twos? 

# Screw this, let's go back to integers
ints_again <- as.integer(f_ints)
ints_again
[1] 2 1 2

这种行为的原因是什么?我认为this问题和this问题可能会解释这一点,但它们并不相同。

1 个答案:

答案 0 :(得分:0)

在转换为整数之前转换为字符,如:

ints_again <- as.integer(as.character(f_ints))