我的数据集Smarket(来自库ISLR)有一个方向列
Direction
1 Up
2 Down
3 Down
4 Up
5 Down
6 Up
如何将其转换为二进制列,如
Direction
1 0
2 1
3 1
4 0
5 1
6 0
执行以下操作我什么都没得到。我该怎么办?
data <- Smarket
data$Direction <- as.factor(as.numeric(data$Direction))
答案 0 :(得分:1)
df1$binary <- as.integer( df1$Direction == 'Down' )
df1
# Direction binary
# 1 Up 0
# 2 Down 1
# 3 Down 1
# 4 Up 0
# 5 Down 1
# 6 Up 0
数据:
df1 <- structure(list(Direction = structure(c(2L, 1L, 1L, 2L, 1L, 2L),
.Label = c("Down", "Up"),
class = "factor")),
.Names = "Direction", class = "data.frame",
row.names = c(NA, -6L))