R 如何将预测值转换为二进制变量并保存到CSV

R 如何将预测值转换为二进制变量并保存到CSV,r,R,我对测试数据建立了一个决策树模型,然后用它来预测测试数据集中的值 dtpredict<-predict(ct1, testdat, type="class") 我想写一个csv,看起来像: id, Class_1, Class_2, Class_3, Class_4, Class_5, Class_6, Class_7, Class_8, Class_9 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 2, 0, 1, 0, 0, 0, 0, 0, 0, 0 3, 0, 0,

我对测试数据建立了一个决策树模型,然后用它来预测测试数据集中的值

dtpredict<-predict(ct1, testdat, type="class")
我想写一个csv,看起来像:

id, Class_1, Class_2, Class_3, Class_4, Class_5, Class_6, Class_7, Class_8, Class_9
1, 0, 1, 0, 0, 0, 0, 0, 0, 0
2, 0, 1, 0, 0, 0, 0, 0, 0, 0
3, 0, 0, 0, 0, 0, 1, 0, 0, 0
4, 0, 1, 0, 0, 0, 0, 0, 0, 0
5, 0, 0, 0, 0, 0, 0, 0, 1, 0
6, 0, 1, 0, 0, 0, 0, 0, 0, 0

呃,010101的逻辑是什么?如果是这样,它们在您的示例中没有多大意义,那么它们都是类1(与您的示例dtpredict不对应)。如果它们是逻辑的

# if dtpredict is a factor vector, where the values are the classes
# and the names are the boolean values:
values = as.numeric(as.character(names(dtpredict)))
classes = as.character(dtpredict)
x = data.frame(id=names(classes))
for(class in sort(unique(classes)){
     x[ , class] = as.numeric(sapply(classes, FUN=function(p) p==class])
}
write.csv(x, 'blah.csv')

有一个名为dummies的软件包,它做得很好

install.packages("dummies")
library(dummies)

x <- factor(c("Class_2", "Class_2", "Class_6", "Class_2", "Class_8", "Class_2"),
            levels = paste("Class", 1:9, sep="_"))

dummy(x, drop = FALSE)

     xClass_1 xClass_2 xClass_3 xClass_4 xClass_5 xClass_6 xClass_7 xClass_8 xClass_9
[1,]        0        1        0        0        0        0        0        0        0
[2,]        0        1        0        0        0        0        0        0        0
[3,]        0        0        0        0        0        1        0        0        0
[4,]        0        1        0        0        0        0        0        0        0
[5,]        0        0        0        0        0        0        0        1        0
[6,]        0        1        0        0        0        0        0        0        0

你的数据存储怎么样?它是data.frame还是data.table?查看
write.table
write.csv()
base R函数。感谢Dominic,这很有效。我必须删除x:colnames(d)啊,好的,我会进行编辑。谢谢你的反馈,很高兴它起作用了。别忘了接受答案,因为这对你的销售代表也有好处!:)嗨,希拉里,对不起,新来的这个世界。。。如果你再看我的帖子,似乎有人纠正了它,使0和1与我之前的输出一致。。。所以0和1表示对象是否在该类中。我试过你的代码,但没用。我希望看到它运行,因为它看起来比像多米尼克的作品那样在事后删除x更优雅。所以回答你的问题,我相信dt预测是一个因子向量?但不确定?嘿,威廉,没问题。好的,所以它们是逻辑的-但它们被困在因子变量中。我想我认为类是名称,逻辑是值,但它可能会被切换。也许试试这个更新的代码?如果代码不起作用,错误消息总是很方便的。
install.packages("dummies")
library(dummies)

x <- factor(c("Class_2", "Class_2", "Class_6", "Class_2", "Class_8", "Class_2"),
            levels = paste("Class", 1:9, sep="_"))

dummy(x, drop = FALSE)

     xClass_1 xClass_2 xClass_3 xClass_4 xClass_5 xClass_6 xClass_7 xClass_8 xClass_9
[1,]        0        1        0        0        0        0        0        0        0
[2,]        0        1        0        0        0        0        0        0        0
[3,]        0        0        0        0        0        1        0        0        0
[4,]        0        1        0        0        0        0        0        0        0
[5,]        0        0        0        0        0        0        0        1        0
[6,]        0        1        0        0        0        0        0        0        0
d <- dummy(x,drop = FALSE)
colnames(d) <- sub("x", "", colnames(d))
write.csv(d, "somefile.csv", row.names = FALSE)