我在R中生成以下虚拟变量时遇到问题,以下是数据,
city
1 A
2 A
3 A
4 B
5 B
6 B
7 C
8 C
9 C
然后我想为数据添加一个新列,东部城市:A和C,西部城市:B
city region
1 A east
2 A east
3 A east
4 B west
5 B west
6 B west
7 C east
8 C east
9 C east
并将region变量更改为虚拟变量
city region
1 A 1
2 A 1
3 A 1
4 B 0
5 B 0
6 B 0
7 C 1
8 C 1
9 C 1
如何通过简单的命令执行此操作?非常感谢。
答案 0 :(得分:4)
不确定锻炼的目的。无论如何,如果你需要进行双向转换,还有另一种选择。有不同的方法来做到这一点。但作为参考。
# the data
df <- structure(list(city = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L), .Label = c("A", "B", "C"), class = "factor")), .Names = "city", class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9"))
# the operation
df$region <- with(df, ifelse(city %in% c("A","C"), "east", "west"))
df$region <- with(df, ifelse(region == "east", 1, 0))
答案 1 :(得分:2)
你也可以这样做:
indx <- df$city %in% c('A', 'C')
df$region <- indx +0
df$region
#[1] 1 1 1 0 0 0 1 1 1
df$region1 <- c('west', 'east')[indx+1]
df$region1
#[1] "east" "east" "east" "west" "west" "west" "east" "east" "east"
答案 2 :(得分:1)
使用此选项添加列:
my.dataframe$region <- a.vector
编辑:
首先,您可以将所有内容设置为东:
x$region <- rep("east")
然后将一个城市切换到西边:
x$region <- ifelse(x$city == "B", "west", "east")