替换data.frame的值 [英] Replacing values of data.frame

查看:257
本文介绍了替换data.frame的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上发现了多个帖子,但是我仍然无法使用?按照我老师的要求,按-1表示。这是我的代码:

I found multiple post on internet however, I'm still not able to remplace the "?" value by "-1" as asked by my teacher. This is my code:

>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返回以下输出:

and when I run the code, the mammogram.frame[which(mammogram.frame=="?")]<-"-1" return the following output:

> mammogram.frame[which(mammogram.frame=="?")]<-"-1"
Error in `[<-.data.frame`(`*tmp*`, which(mammogram.frame == "?"), value = "-1") : 
  new columns would leave holes after existing columns

我也尝试过下面的代码,但是它将数据转换成别的东西...

I also tried the code below but it transform my data in something else...

> 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, ...)

我的意思是说,当我运行>乳房X线照片时,它并没有给出与开始相同的视觉效果。框架 ..

I mean by that that it doesn't give my the same visual as the beginning when I run >mammogram.frame..

任何想法将?值设为-1,因为当我执行 < - NA

Any idea to passe the "?" value to -1? because it works when I do >mammogram.frame[which(mammogram.frame=="?")] <- NA

谢谢!

推荐答案

加载时请尝试以下更改:

Try the following change when loading:

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,
                             stringsAsFactors = F)

然后执行:

mammogram.frame[mammogram.frame=="?"] = "-1"

结果:

> head(mammogram.frame)
  Birads Age Shape Margin Density Severity
1      5  67     3      5       3        1
2      4  43     1      1      -1        1
3      5  58     4      5       3        1
4      4  28     1      1       3        0
5      5  74     1      5      -1        1
6      4  65     1     -1       3        0

这篇关于替换data.frame的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆