0
我在互联网上发现了多个帖子,但是我仍然无法将“?”如我的老师所问,值为“-1”。这是我的代码:替换data.frame的值
>library(e1071);
>mammogram <- data.frame
>mammogram.frame = read.table("https://archive.ics.uci.edu/ml/machine-learning-databases/mammographic-masses/mammographic_masses.data",
sep=",",
col.names=c("Birads","Age","Shape","Margin","Density","Severity"),
fill=TRUE,
strip.white=TRUE)
>mammogram.frame[which(mammogram.frame=="?")]<-"-1"
>mammogram.frame
>summary(mammogram.frame)
>svm.model <- svm(Density~.,
data=mammogram.frame,
type="C-classification",
cost=1.0,
kernel="polynomial",
degree=2.0)
>svm.model
,当我运行的代码中,mammogram.frame[which(mammogram.frame=="?")]<-"-1"
返回以下的输出:
> mammogram.frame[which(mammogram.frame=="?")]<-"-1"
Error in `[<-.data.frame`(`*tmp*`, which(mammogram.frame == "?"), value = "-1") :
new columns would leave holes after existing columns
我也尝试下面的代码,但它在别的变换我的数据?
> mammogram.frame <- as.character(mammogram.frame)
> mammogram.frame[mammogram.frame == "?"] <- "-1"
> mammogram.frame
[1] "c(6, 5, 6, 5, 6, 5, 5, ...)
> mammogram.frame <- as.factor(mammogram.frame)
> mammogram.frame
[1] c(6, 5, 6, 5, 6, 5, 5, ...)
我的意思是说,它并没有给我相同的视觉作为一开始的时候我跑>mammogram.frame
..
任何想法通过“?”值为-1?因为它在我的工作>mammogram.frame[which(mammogram.frame=="?")] <- NA
谢谢!