我有一个类似下面的数据,我想将此变量转换为应该等于varible中的no.of类的列。
数据:
variable
1 red
2 red
3 black
4 red
5 black
6 green
7 black
8 red
9 green
预期产出:
variable red black green
1 red 1 0 0
2 red 1 0 0
3 black 0 1 0
4 red 1 0 0
5 black 0 1 0
6 green 0 0 1
7 black 0 1 0
8 red 1 0 0
9 green 0 0 1
试过:
data$red = ifelse(data$variable == "red",1,0)
data$black = ifelse(data$variable == "black",1,0)
data$green = ifelse(data$variable == "green",1,0)
通过使用上面的代码,我实现了我想要的。但是如果我在一个变量中有超过10个类,我不想写代码10次。那么,有没有最好的方法一次完成这个。
答案 0 :(得分:0)
df <- read.table(file = "clipboard", sep = "\t", header=T)
df <-data.frame(variable= c("red","red","black","red","black","green",
"black","red","green"))
df
variable
1 red
2 red
3 black
4 red
5 black
6 green
7 black
8 red
9 green
class(df$variable)
[1] "factor"
model.matrix(~ . + 0, data=df, contrasts.arg = lapply(df, contrasts, contrasts=FALSE))
variableblack variablegreen variablered
1 0 0 1
2 0 0 1
3 1 0 0
4 0 0 1
5 1 0 0
6 0 1 0
7 1 0 0
8 0 0 1
9 0 1 0